Shorthand functions (was: Code Snippets' you couldn't live without)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Murray @ PlanetThoughtful wrote:
with exploring include files to find out what a function does or how a class
operates. I doubt half-a-dozen shorthand functions in that include file
would place a measurable strain on the readability or maintainability of a
project.

I disagree on that. The problem here is not that the functions are short. The problem is, that their names gives absolutely no clue about what they are doing. mfr() can be mysql_free_result() but it also can be mysql_fetch_row(). So it's not only unreadable, it's also error-prone. Sure, it's possible to look up what this function is doing. But is it necessary? Everybody (even coders not knowing the mysql functions) can understand what "mysql_free_result()" is doing, without decyphering it first.


But I agree that many PHP functions are named too long. But this is because they are non-OO functions living without namespaces. So if you don't use OO (where you may write something short like $res->free()" you have to live with that, I fear. Or write shorthand functions, nobody can keep you from doing so. I just said, that it is problematic for more professional projects where new programmers may begin to work on at any time).

And I agree that there are even some very short function names in PHP like nl2br(). This looks like a bad naming, but it is not really. "nl" is a well known shorthand for newline. "2" makes clear that it converts something. And "br" is named after the HTML-Tag. So this function name GIVES some clues about what it is doing. But for newcomers it may not be clear at the beginning what this function is doing. But after a quick lookup the newcomer can remember the meaning very well. And I think this is different with functions like mfr() and asl() which gives no clue about their meaning.


when first viewed by someone new to PHP. Also witness the deprecation of
variables such as $HTTP_GET_VARS in favour of $_GET etc.

$_GET and $_POST are well named. It's clear what they are doing. Thank god their were not named $_HGV and $_HPV.



Or the .=
concatenation assignment operator as a shorthand to $var = $var . ' added
string.'

"$a .= $b" is not a shorthand of "$a = $a . $b". The result is the same but the internal processing is totally different. "$a .= $b" is a lot faster then "$a = $a . $b" because $b is directly appended to $a. The second form constructs a completely new string and overwrites the old value of $a. At least this is the case if no optimizer is used. Maybe optimizeres are transforming the second form into the first form, don't know.


So you can't compare this to a shorthand function. It was not introduced because coders are to lazy to write the long form. It was introduced because it is absolutely intuitive to do so if the language already has something like "+=". But I can't tell if these operators were introduced into the C programming language (or wherever it appeared first) because lazy coders. ;-)


Back to the intent rather than the form of my original question: what are
the code snippets you can't live without?

I already posted some. But this thread is interesting, too. So I changed the subject ;-)


--
Bye, K <http://www.ailis.de/~k/> (FidoNet: 2:240/2188.18)
[A735 47EC D87B 1F15 C1E9  53D3 AA03 6173 A723 E391]
(Finger k@xxxxxxxx to get public key)

Attachment: signature.asc
Description: OpenPGP digital signature


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux