[Grump from a beginner] Function Names and more

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
elnaz1010
Forum Newbie
Posts: 12
Joined: Sun Mar 28, 2010 10:28 pm

[Grump from a beginner] Function Names and more

Post by elnaz1010 »

hi
i had some knowledge with php and asp, but i was not an advanced one. recently i decide to learn php in more depth. because i like open source and i see a lot of fans trying to make it better than before.
and im reading a book for it.
first thing that bother my mind is function names.
i want to know why functions (not all of them though) have no meaningful names in them? why they select a chop() name for a function? it have no mean :(

another thing is naming abbreviations
for example if i want to work with files, i have: fread(), readfile(), fpassthru(), file() functions. some of them use “file” (like: readfile, file) and some of them use “f” as abbreviations (like: fread(), fpassthru()) .
and as you see none of them tells you what they do if you are not familiar with them.

also for reading from file i have: fgets(), fgetss(), fgetcsv(). again, name tells you nothing. for example if you see fgetss() for the first time, you cant say what is the difference between fgets() or fgetss().

another problem i see in functions is name convention. for example we have file_exist() and filesize(). why in some function there is an underline between names and in some of them there isn’t?
if i know some language like c or delphi, i expect when i use filesize(), there is a function name fileexist() not file_exist(), or vice versa.

also i noticed that php still haven’t got unicode support for function names and some others but asp had it a long time ago.

i know unicode support probably needs huge code to be implemented, so its not a big deal in my mind if php haven’t got it since.
but names of function is a simple thing to fix and it should be straightforward for those who are going to learn php.

as far as i know, asp have a better naming and almost all of them gives you some idea about what they do.

i was a fan of php and when we debate with my friend, i was in php side and against asp. but these things is something that i dont know why php developer wont fix.

the next thing that i couldnt figure out is new features, i looked in changelog of php 5.3, and i see its now support for closures and true anonymous functions, i know it has its use, but lets face it, doesnt something like this make our code much less readable?
i saw an example about it in wikipedia:

Code: Select all

function getAdder($x)
{
return function ($y) use ($x) {
return $x + $y;
};
}

$adder = getAdder(8);
echo $adder(2); // prints “10″
i heard guys says its heavily on C so its going to be like this,
ok about C, yes it is heavily on C syntax, but php should not have filesize because C have no filesize at all as far as i know, so if we want to be like C, we should have file_size(). because C use underline in such a functions. example is file_exist()


all i read in programing source in computer science, they say one parameter for a good language is a good naming convention.
and for a language like php (my be loved one :( ), i think its good to have a perfect naming convetion, i mean come on, its not hard to implement a good name for a function.

if you say we should support backward compatibility, i say at least make these bad names an old feature, so they will be fixed in next versions.
just like globals removed. or magic quotes that going to be removed.

another thing is parameter order, for example when you work with strings we have to function,
explode:
array explode ( string $delimiter , string $string [, int $limit ] )

and strtok:
string strtok ( string $str , string $delimiter )

so which one? delimetter before string or after.

i know these are not such a big thing, but i think they can fix it very easily

these things that i see are bothering me when i compare them with asp,

can someone please correct me if im wrong? i like php alot and i need to know the reason why these things still exist.

i hope i could make my mean clear.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: [Grump from a beginner] Function Names and more

Post by Weirdan »

Function naming and parameter order is a mess in PHP, that's right. Why this is so is of little importance though, since this doesn't bother you anymore once you reached familiarity with most common extensions. You just get used to rely on your IDE to hint the parameter order / names (or get used to look it up in the online manual). This is frustrating for the first year or so, after that you stop noticing it. So, no big deal.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: [Grump from a beginner] Function Names and more

Post by califdon »

I agree with some of your criticisms, disagree with others. This is the problem. PHP was developed by a lot of people, over a long period of time, with not much relationship with each other, unlike Visual Basic, for example. How can you expect to enforce a naming convention unless you have a fully developed protocol in place from the very beginning? Even if there had been such a standard, I think you (or I) would not be happy with every aspect of it, so you would still be dissatisfied! I agree with Weirdan, it's not a real problem except at the very early stages of learning.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: [Grump from a beginner] Function Names and more

Post by josh »

Back in the 60s and 70s people thought typing was the primary coding activity, and tried to minimize the amount of typing (this is back before PHP was around)
Today, we realize understanding code is the primary activity, and prefer longer names. Many function names are still short for historic reasons. PHP probably used the historical conventions, to make it friendly for people used to those older languages. (for example, using printf instead of print_formatted, etc..)

But, I think the more important thing to realize is you can wrap functions with custom user functions or classes. This is preferable, since you can change a function you wrote, but not a core function. If you're going to make heavy use of a function, it may be a good idea to wrap it first.

For example, regardless of how they named it, you don't want your code coupled to the mysql_query function (what if you want to use postresql later on for example)

Also yes what they thought you in school about monadic functions is correct, in theory, but in the real professional world we welcome dyadic functions if it means cleaner code.

Closures can make code messier or easier to read, but is a different concept than anonymous functions. They are often used together. You guys have got me confused, I had to double check but it appears PHP does support both now, although in a non-conventional syntax. For example Zend framework's validation components make use of anonymous functions (which are not closures), so you can write "plug ins" to add new validation logic to the framework.

The example you provided is pointless. To make it realistic, have the anonymous function passed in as a parameter.
Last edited by josh on Mon Mar 29, 2010 4:57 pm, edited 4 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: [Grump from a beginner] Function Names and more

Post by Benjamin »

I don't know. I mean I think the functions are fairly easy to find once you learn your way around the manual. I'd rather type fgets than read_file_one_line_at_a_time(). But I have noticed issues with differing naming conventions. I don't consider this to be a major problem though.
elnaz1010
Forum Newbie
Posts: 12
Joined: Sun Mar 28, 2010 10:28 pm

Re: [Grump from a beginner] Function Names and more

Post by elnaz1010 »

thanks all for sharing your idea,

i agree, every thing you said but when you know something is wrong, you will going to fix it

we need backward compatibility, its ok. but tell me whats wrong with making these bad name an old feature, and build an alias with a good name, so we can remove them in the next big versions. i think 4 years later, its a good time to remove it, so if we start now we wont have any of these in php ver 7. but as far as i know, they didnt even start to fix these things.

and you say they are volunteers from every where, ok we all know they are. but one thing you forgot is "why they volunteered?" to make a good language, to help php to be the best and challenge with his competitors.

i saw they put their effort in something like anonymous functions(which i didnt like, because it reduce its readibility in my personal idea) and didnt even think about making a good alias for a bad named function(which i think is so easy to implement).

also i think if you define an alias for a function, you still have your backward compatibility just like rtrim() instead of dirty chop()

last of all, i didnt said "php have these faults, so its not a good language" you all talking about this, we all know php is a perfect language.
what i said was "why they don't even try to fix these things, its a easy fix".


that bothers me.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: [Grump from a beginner] Function Names and more

Post by Christopher »

First, you make the very common mistake of thinking that the names you like make sense and are better. Everyone thinks their names are better, but the names that last are those the majority find useful.

Second, yours is a very old complaint about PHP. A small number of people have always complained about this, while the majority happily and easily uses the PHP library.

And finally as has been mentioned, most of the functions follow the naming and parameter order of the subsystem they call. Your opinion is that they are "bad names." I find them very memorable because I know Unix and most of the subsystems. Creating new names would require me to know a different names for all the functions I already know.

And you would complain about the new names they picked too ... ;)
(#10850)
elnaz1010
Forum Newbie
Posts: 12
Joined: Sun Mar 28, 2010 10:28 pm

Re: [Grump from a beginner] Function Names and more

Post by elnaz1010 »

Christopher wrote:First, you make the very common mistake of thinking that the names you like make sense and are better. Everyone thinks their names are better, but the names that last are those the majority find useful.

Second, yours is a very old complaint about PHP. A small number of people have always complained about this, while the majority happily and easily uses the PHP library.

And finally as has been mentioned, most of the functions follow the naming and parameter order of the subsystem they call. Your opinion is that they are "bad names." I find them very memorable because I know Unix and most of the subsystems. Creating new names would require me to know a different names for all the functions I already know.

And you would complain about the new names they picked too ... ;)

well my friend, although i am disagree with you in all 3 parts you mentioned, i will appreciate your idea.

for the first: well if you build a voting system and make 4 object to vote for:
1: file_exist(), filesize()
2: fileexist(), file_size()
3: fileexist(), filesize()
4: file_exist(), file_size()

you will see that number 3 and 4 will have a much higher voters. so your first reason is out of discussion, i think.

second: yes if you are familiar, or even get familiar to php you will use php happily. i didnt said php is a bad language (again!) as i mentioned in my previous post. if i said so your second was legit.

about third: again i answered it in my previous post. i didnt said remove them, i said make a good alias for them, removing is the next phase. and its not soon.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: [Grump from a beginner] Function Names and more

Post by josh »

elnaz1010 wrote: 1: file_exist(), filesize()
2: fileexist(), file_size()
3: fileexist(), filesize()
4: file_exist(), file_size()

your first reason is out of discussion, i think.
Not me. I hate rammed together words. I'd write MappingsExport, not mappingsexport

Look. Anything can be abused, including function naming. Even a function with no parameters can be used in a messy way. In your example the original author managed to take a beautiful construct like closures and use them in an utterly pointless way that turns away programmers who knew nothing about it.

To put it in plain English, don't judge a book by it's cover. There's lots to hate about PHP but the function naming is hardly one worth discussing, if you want to hate PHP, hate it for not throwing a NOTICE error when you try to access undefined array keys :dubious:

/thread
elnaz1010
Forum Newbie
Posts: 12
Joined: Sun Mar 28, 2010 10:28 pm

Re: [Grump from a beginner] Function Names and more

Post by elnaz1010 »

calm down my friend, im just talking about my personal ideas here.

ok forget it :)
end of discussion

regards
elnaz
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Grump from a beginner] Function Names and more

Post by AbraCadaver »

josh wrote:Not me. I hate rammed together words. I'd write MappingsExport, not mappingsexport

Look. Anything can be abused, including function naming. Even a function with no parameters can be used in a messy way. In your example the original author managed to take a beautiful construct like closures and use them in an utterly pointless way that turns away programmers who knew nothing about it.

To put it in plain English, don't judge a book by it's cover. There's lots to hate about PHP but the function naming is hardly one worth discussing, if you want to hate PHP, hate it for not throwing a NOTICE error when you try to access undefined array keys :dubious:

/thread
What!?!?

Code: Select all

$a = array();
print $a[0];
print $a['test'];
Notice: Undefined offset: 0 in test.php on line 3
Notice: Undefined index: test in test.php on line 4
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Grump from a beginner] Function Names and more

Post by AbraCadaver »

Hmmm... Josh. My previous post is obviously correct, however I just noticed this in something I was doing:

Code: Select all

$a = 5;
print $a[0];
print $a['test'];
No notices?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: [Grump from a beginner] Function Names and more

Post by josh »

AbraCadaver wrote:No notices?
That's what I meant. If you have a variable that is null instead of an array, your program can fail silently. This is a legitimate complaint about PHP. I have no idea why they want it like that, but they have a history of bugs filed against this behavior, that have been closed with the response "RTFM" (even though the behavior is not really documented)

That's another legitimate complaint is the developers have a history of being stubborn when it comes to implementing user feedback.
Post Reply