Common Style Mistakes, Part 1
Subject:   style rules
Date:   2003-06-14 16:59:40
From:   anonymous2
No need to fussily always use the ugly <?php and other default values. To make your code more transportable write an include file that sets all the php config directives to what your app needs. If your code can't change them it can check and spit out an error.

ini_set('short_open_tag', 1);
assert(ini_get('short_open_tag') == 1);

I think it's a good idea to do this for any significant PHP application, rather than rely on whatever happens to be in the php.ini file when you wrote the code, especially in a shared hosting environment.

As for chaining function calls, years of experience with Lisp and C -- both much older than PHP -- show that proliferating temporary variables is an even worse idea. The author wrote a deliberately ugly and obfuscated example, but with some indenting the code can be perfectly obvious. Anyone who can't follow a few nested function calls should not be writing important code anyway.

Try writing in a language that doesn't have functions that return values and you'll quickly see how tedious that can be.

The ternary operator condition ? expression : expression lends itself to misuse, but there's nothing inherently wrong with using it inside a function call or anywhere else. The order of evaluation is well-defined. Sometimes simply enclosing the whole thing in parentheses makes it clearer. Again indenting and clarity in writing go a long way.

Greg Jorgensen

Full Threads Newest First

Showing messages 1 through 1 of 1.

  • style rules
    2003-06-14 20:26:25  John Coggeshall | O'Reilly Author [View]

    Although your solution is valid, it's also costly to have to needlessly include an additional file every single request. By simply using the standard
    Regarding chaining function calls, yes my example was very obfuscated. However, I stand by the 3-parameter rule as a solid method of doing things. I do agree that as easily as there can be too many chained function calls, you can have too many temporary variables.. Also, although I cannot speak in regards to Lisp I must point out that in this case a comparison to C isn't valid. In C variables must be declared, etc. and perhaps in that situation can be more trouble than it's worth.. however in PHP, where variables can be created and assigned values without such hassle the impact is minimal.

    Finally, regarding the ternary operator.. I again disagree. :) Since my arguments against this parallel those I have already made regarding chaining function calls inappropriately, I won't repeat them again.

    Perhaps one style mistake that I should have made a point to bring up is that readability is extremely important. Without being able to quickly ascertain what is going on with a piece of code the chances of missing or not finding bugs is that much greater. If you claim that a piece of code is completely readable with 20 function calls chained together (as long as you indent it properly) then so be it :). However, as a general rule of thumb the techniques I have discussed in this column are extremely important and I stand by each of them :)

    Thanks for your feedback.