QuicksearchShow tagged entriesCategories |
Friday, October 24. 2008
Posted by Mark van der Velden
in PHP, PlanetPHP
Comments (6) Trackbacks (0) Defined tags for this entry: array fun, condition order, documentation, notices, php, planetphp, variable naming, warnings
Best practices, part oneBest practices are ways of solving problems in a good way, these practices change over time and can depend on versions. A lot of people who have their roots in PHP4 have habits that are no longer best practices. But not just them, a lot of developers don't apply best-practices rules. In this blog post I'd like to point out a few reminders or refreshing points for you to take in. Most you will probably know but some you might not know or didn't look for. If you know some nice additions, make a comment and I'll add it. Enjoy! file_exists vs is_file Using @ aka 'shutup' operator Notices are worse then Warnings Not checking what you use Array fun $last = $values[ count($values)-1 ]; unset($values[ count($values)-1 ]); foreach($values ...) { //... } While this just looks so much cleaner: Be sure to checkout all available array related functions for more handy-dandy work. Calling public (non-static) methods the static way PHP open and close tags Condition order (Remi also has a blog post about condition orders, with a comment of why you want to have the values in the wrong order. While it's true, I still don't agree with changing the order) Regex fun or avoid or
Variable naming // What is 'un' ? Judged by the contents I'm guessing a user name $un = 'John'; // What should data contain ? foreach($data as $k=>$v) { $db->add($v); } // Strip.. what? $output = strip($input); And now a way that is much nicer for the eyes: // Clearly, John is a first name $firstName = 'John'; // We are adding users it seems foreach($userData as $id=>$user) { $db->add($user); } // Obvious stripping $output = strip_uppercase($input); Do note that, a name should be short and obvious, no need to tell a story what it does, that is where docblocks come in.
Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
re: shutup operator: Personally, I think it gets used too much as a try/catch hack. Code can't break, but if that single line fails for whatever reason, the whole code breaks. When it's used, I usually notice that people use it to stop the code from breaking, and do the success/fail checking themselves. Of course you can override the error_handler, but that makes things even more slower, if you have to set/reset the error_handler every time you need a try/catch on a function that doesn't throw exceptions
re: condition order: I used to have the same question, but my former co-worker Daniel Berstein learned me that this is to prevent errors. Example: (false = $foo) versus: ($foo = false). The latter will parse perfectly, and is very hard to catch, just doesn't do what you want. The first one will throw an error, and will even be marked as a syntax errors in most IDE's. Since I learned that, I've been using it too. (For more info, see my blog with Daniel's comments: http://www.wolerized.com/blog/remi-woler/calling-kettle-black ). ~RW P.S. You use htmlentities() a bit too much. Even the > and < in your code examples get converted.
Hey Remi,
> re: 'shutup operator' Yes indeed. A shutup operator only suppresses, it doesn't prevent code from dying. If you have display_errors = Off, you get the same result, and the error get's logged = good. In some occasions (like when using getimagesize) I can somewhat understand, simply because there are no 100% valid ways of checking before requesting details, but it still doesn't justify suppressing the warning it issues. It did went bad.. it should throw the warning. > re: condition order I think the famous bunch of IDE's generate a 'Are you sure that is correct?' notifications when using a assignment in condition, it might help by reversing the values in the condition, but I find '=' pretty different to read from '==='. I mean, be honest, how often did you type '=' when you meant '=='... It doesn't justify the means in my opinion. But they, it's just an opinion, do as you see fit > the htmlentities() Ye I know, it's some bug between s9y and the highlighting plugin geshi, if (1 > 0) {} I have to dive in that some day, but I cba right now
> I mean, be honest, how often did you type '=' when you
> meant '=='... Only a few times, but the times I did it took a lot of time to actually find what was going wrong. This was before I wrote Unit Tests to isolate potential errors, but then, most people still don't. By the way: I think stating "false == $x" is the wrong order is a bit silly. Can there really be a wrong order in comparison?
I stand by my original comment in Remi's blog: Using invariants on the left side of comparisons is a good practice. I've become so used to that style that I even write strcmp() arguments the same way.
In my code you'll always find the "most" invariant (invariant-est?) parameters on the left, and more dynamic parameters on the right. This is also consistent when "designing" method signatures... optional parameters are the most volatile, thus they appear at the leftmost of the method signature: "public function foo(array $a, $b, $c = 'default')" I hope you agree that consistency is a good practice; and if you agree to that you should logically conclude that the whole ' ' order if consistent with the above stated style can only be considered a good practice.
I agree that keeping consistency is important, but method argument order versus condition order is totally irrelevant in this situation.
I understand why you would want to switch the condition order, but I don't find it good practice. And so far the only real reason I've heard is so you can't accidentally assign instead of compare? A Sad story really.. :-/
I see no sadness on having difference of opinions... actually the opposite.
At day's end what's important is what works best for each. For me "deep" consistency (eg. a small set of "axioms") works best. If using this "Yoda Speak"-like ordering convention suits you, use it... if it doesn't don't. I prefer VIM, perhaps you're more of an EMACS guy. Is any of us mistaken? PS: I know its you who's mistaken, but I won't say it |
Calendar
|
|||||||||||||||||||||||||||||||||||||||||||||||||
