Explicit is better than implicit is one of python's Zen. Coming to PHP, however, there are too many ways to make things implicitly. One of them is variable variable:
$var = 'name';
$$var = 'henry';
echo $name;
The output is 'henry'. Maybe this code snippet is not that horrible. But sometimes it is not that simple.
There was once I tried to locate where a variable is defined. The variable is $branchName, for example. It is being used directly in a file in this way: echo $branchName; and it works, so the variable must be defined/assigned a value somewhere else. Since the legacy code is procedure style, so I naturally trace back to other files that are included into the file. It must be one of those included files in which $branchName is defined. However, I can't find it. So i start to search the whole application, but i still can't find any place that defines $branchName.
The first possibility i can come up with is, register global, another notorious php feature. The application is so old that register global is turned on and used. Unfortunately, I still can't find any place showing that $branchName is a register global.
Then, i think that must be a variable variable, so i search the string '$$' in those included files. I cannot find any. I start to get a little confused. But suddenly, i remember that PHP can define variable variable in two ways: $$var = 'henry' or ${$var} = 'henry'. They both work. So i try to search '${$' in the files, finally, i found this code, with a comment from a prior developer, which is funny:
for ($i = 0, $last = count($parameters); $i < $last; $i++) {
//Good ol' dollar-dollar string interpolation... um, yeah, "thanks".
${$parameters[$i]['name']} = $parameters[$i]['value'];
}
When i var_dump($parameters), I finally found this 'branchName'. PHP can make things so implicitly, and even worse, it provides different ways to do implicit thing while Python preaches there should be only one way to do the same thing. That is why when i learned the fact that "Ruby inherited the Perl philosophy of having more than one way to do the same thing", I immediately dump this language into my rubbish bin and never want to touch it again, unless i was forced to.
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Thursday, August 25, 2011
Wednesday, August 3, 2011
lovely Python (compared with PHP or maybe some other languages)
The Zen of Python, especially "There should be one - and preferably only one - obvious way to do it", highly attracts me. And that actually drives me to start learning Python. And once I start, I find I like it more. Just some simple examples can show how Python practise what it preaches.
Python code
name = 'henry'
if name == 'hengrui':
print ('Hello ' + name)
else:
print ('Is ' + name + ' your real name?')
PHP code
version 1
$name = 'henry';
if ($name == 'hengrui') {
echo 'Hello ', $name;
} else {
echo 'Is ', $name, ' your real name?';
}
version 2
$name = 'henry';
if ($name === 'hengrui') {
echo 'Hello ', $name;
} else {
echo 'Is ', $name, ' your real name?';
}
Found the difference in this version? Well, it is '===' not '=='.
version 3
$name = 'henry'
if ($name == 'hengrui')
echo 'Hello ', $name;
else
echo 'Is ', $name, ' your real name?';
version 4
$name = 'henry'
if ($name == 'hengrui') echo 'Hello ', $name;
else
echo 'Is ', $name, ' your real name?';
Surely we can still have some other versions of implementation in PHP code. Like it or not? It is up to you. But for me, i don't like this kind of variations and definitely coding conventions must be set up to ensure we won't have all these different coding practices in serious projects.
In my post, http://hengrui-li.blogspot.com/2011/05/variable-assignment-inside-if.html, i believe doing assignment in if statement is a very bad practice. In Python, you just can't do that.
PHP code
function getResult()
{
//oh, by the way, for boolean value, you can also return TRUE, True. Anyway, case insensitive
return true;
}
//this code works in PHP
if ($result = getResult()) {
echo 'it is true';
}
Python code
def getResult():
#you can only use True, case sensitive.
return True
if result = getResult():
print('it is true')
The python code simply cannot work. It doesn't allows you to do assignment in if statement. in a if statement, the only one thing you should do and you can do is logic operation.
Another very simple example:
PHP code
//both works
print 'hello';
print ('hello');
Python code
#this is correct
print ('hello')
#this is syntax error in python 3
print 'hello'
You may say, hey in this case PHP is better because you can type less. Well, typing more or less doesn't matter here. The point is: "There should be one - and preferably only one - obvious way to do it". (Actually, in python 2, print is a statement as well and it works exactly like PHP print. But Python 3 corrects this issue, which is good!)
Sunday, July 31, 2011
I like Python philosophy
I like Python's philosophy. Especially this one, my favorite: "There should be one - and preferably only one - obvious way to do it".
I also like these: "Explicit is better than implicit", "Readability counts", "If the implementation is hard to explain, it's a bad idea".
More Python philosophy: http://c2.com/cgi/wiki?PythonPhilosophy
I also like these: "Explicit is better than implicit", "Readability counts", "If the implementation is hard to explain, it's a bad idea".
More Python philosophy: http://c2.com/cgi/wiki?PythonPhilosophy
Subscribe to:
Posts (Atom)