Slight knock on PHP in the section on coding standards
I admittedly held back quite a bit from how much I could easily rant on the topic of PHP's complete lack of consistency when it comes to its function/object library. However, I need to finish the chapter on documentation, not how much PHP's lack of consistency annoys me. As a result, the following excerpt reads much more like a hint of slight frustration than the outburst of obscenities uttered as I hit <ctrl>+<l>, <p>, <tab>, <return> to go back to the PHP manual for the twelfth time that day.
Coding conventions should also include variable, function, method, and object naming practices. When languages support upper-case and lower-case alphanumerics, underscores, and sometimes even the dollar sign character, function libraries and APIs have the potential to include a wide variety of calls available to developers. The following four PHP library functions, while all consistently lower-case, have different parameter ordering and variable naming:
strpos ( string $haystack, mixed $needle [, int $offset] )
str_split ( string $string [, int $split_length] )
explode ( string $delimiter, string $string [, int $limit] )
The strpos and str_split functions in particular should not have differences in their naming, as they reside within in the same categorization of library functions in the PHP Manual, but one has an underscore separating the "str" prefix from the full word of "split" while the other has the "str" prefix unseparated from the abbreviated "pos" instead. Looking at str_split and explode, these two functions have very similar functionality: str_split breaks a string into an array of substrings of a constant length, while explode breaks a string into an array of strings as divided by a passed delimiter. Unfortunately, while str_split takes the string as the first parameter and the split length as the second, explode takes the delimiter as the first parameter and the string as the second, requiring calls to pass an empty string as the first parameter in order to break the string into an array of characters.
No matter what the conventions decided, they should not deviate so far from standard practice that new developers have a difficult time working in the code. By using tabs, consistent naming conventions, and consistent parameter ordering, developers should have the ability to "just know" what a library function or API call looks and acts like, and developers will spend less time researching functions and more time using them.
Labels: advanced ajax, excerpts, php

2 Comments:
This is an old problem stated by many, do you have a solution other than adding a zillion function aliases? PHP has been around for over 10 years and tries to preserve BC whenever possible and unfortunately cannot rewrite history. It's not like the people who wrote PHP decided "You know what, let's mess with our users... it'll be fun!" Nobody can rewrite the past, hindsight is 20/20, let's move on. Much of the old school PHP was written by random people doing random things, but now there is a little more structure. Or maybe it's a conspiracy by the documentation team to increase manual use... shhh, don't tell! :)
There are naming guidelines in place now for all new functions. For example, string function begin with str_.
Other than function aliases (and dual function/object libraries), I would recommend deprecating (then warning and finally removing) the old functions in PHP5+ after deciding on a bit more standard of a methodology, which I think PHP has done in a few instances. I can also think of extensions that could do with a complete scrapping (well, deprecating) and rewriting. I still see fresh examples today of vastly different implementations that could have even a little more in common with regard to their usage (filter vs. PDO for one...er...two). You can pretty much guess which developer contributed to which extension based on the style of calls necessary to interact with it.
But luckily, I just needed to write about preventing situations like that. Recovering from it with as huge and diverse a user base as PHP has can't make for an easy corner to have painted one's self into. An easy or simple way out simply doesn't exist.
Post a Comment
Links to this post:
Create a Link
<< Home