Naming Convention aka PHP vs Python
The naming convention. It’s obviously better to have any than none. As I was writing, I noticed that the title doesn’t reflect what I wrote. All turned out to be not about convention, but about comparing PHP to Python. Personally none of the languages is my favorite.
After reading the whole post, I realized that Python is not better than PHP, it is messed the same way, so why bother changing from PHP to Python?
The PHP Critic
The PHP Mess
PHP is often criticized for the mess in the function names. I always feel like remembering a function name is easy. Remembering how to write the function name isn’t. Just imagine that there is a function named stream get line
. You just remember those three words. Of course you don’t remember how the function is written, the possibilities are:
stream_get_line()
streamgetline()
streamGetLine()
Stream_Get_Line()
StreamGetLine()
All of the above versions are OK, the correct one depends only on the convention. In Java world all that is easy - there is one convension widely used. The Java correct version would be streamGetLine()
.
In the PHP world there is a very big mess. The correct version is stream_get_line()
, but it is not so easy with the rest of functions. There are some setlocale()
, phpversion()
and bindtextdomain()
.
PHP programmers also couldn’t agree on the functions names e.g. BIN TO HEX
(for converting binary string to hex one). Should there be 2
or TO
? The correct answer is bin2hex()
. On the other hand there is the function strtolower()
, that has TO
instead of 2
.
More examples can be find here. There is a comparison of Perl and PHP, but it doesn’t matter, the examples of PHP are quite nice.
PHP Objects
The problem is that for many years PHP was just a functional language. Just like many other functional languages, if the language is Turing complete, there shouldn’t be any problem.
Now PHP is an objective language, or I should name some other way: it allows you to write code in some objective way. You can use classes, but you don’t have to. Quite the same situation as in Python, C++ and Ruby.
Writing such objective code usually means that in some function in a class you have to use not objective code from PHP core, but nothing is perfect.
What To Do?
That all causes many programmers to cry, and change their language to another to have classes and so on. Many of them changed the daily used language to Python, because Python is the best
. I have heard that too many times not to check that out.
Python Standard Library
Module Name
Python standard library is very similar to the PHP library, it is available just after installing Python. There are many modules. The package names are messed like in PHP, there is
HTMLParser
sgmllib
copy_reg
UserDict
cStringIO
Cool, what is the convention? Is there any?
Function Names
Let’s look at the module HTMLParser, there are functions like these two:
getpos()
handle_data()
Cool, maybe you remember what I wrote about the mess in PHP? If you agree that it is a total mess… then how would you call this?
handle_starttag()
get_starttag_text()
Let’s even assume that starttag is one word. How could you then write this?
handle_startendtag()
What is the convention? Is there any?
Syntactic Sugar
Python is loved by programmers, because it doesn’t have any braces or begin/ end blocks. Instead just to simplify all the things they threw this away. A nice idea. Instead of just throwing that away, they just added intendation and “:“at the beginning of the block. So instead of:
void min(a, b) {
if (a < b) return a;
else return b
}
you have to write this:
def min(a,b):
if (a < b):
return a
else:
return b
Many people tell me that Python is the best language ever invented, and the syntax is clear, and doesn’t contain any not necessery things like {
/}
/begin
/end
. Yes. Just instead there is just the stupid :
.
What is that for if there is indentation? Is the indentation not enough? Wait a minute, that was going to be the simplest syntax, so where is it?
Magical Methods
Magical methods in Python are all those with underscores like:
__init__()
__str__()
__import__()
I was told that this language is going to be great and simple, now it seems that nice and simple is the java class hierachy, without any magic methods.
Python Convention
Yea, there is a convention. I was very happy when I found this information. Hurray, there is a convention so I suppose that all those examples are just wrong, someone wrote that without any convention.
PEP 0008
PEP 8 is The Style Guide for Python Code. Unfortunately too often I read programmers' opinions that PEP8 is great, and code should be compliant with it.
So let’s read it:
The naming conventions of Python’s library are a bit of a mess, so we’ll
never get this completely consistent – nevertheless, here are the
currently recommended naming standards. New modules and packages
(including third party frameworks) should be written to these standards,
but where an existing library has a different style, internal consistency
is preferred.There are a lot of different naming styles. It helps to be able to
recognize what naming style is being used, independently from what they
are used for.The following naming styles are commonly distinguished:
b (single lowercase letter)
B (single uppercase letter)
lowercase
lower_case_with_underscores
UPPERCASE
UPPER_CASE_WITH_UNDERSCORES
CapitalizedWords (or CapWords, or CamelCase – so named because
of the bumpy look of its letters[4]). This is also sometimes known as
StudlyCaps.Note: When using abbreviations in CapWords, capitalize all the letters
of the abbreviation. Thus HTTPServerError is better than
HttpServerError.
mixedCase (differs from CapitalizedWords by initial lowercase
character!)Capitalized_Words_With_Underscores (ugly!)
– pep8
Cool. I really like this one:
The naming conventions of Python’s library are a bit of a mess, so we’ll never get this completely consistent…
I really don’t get how it happened, isn’t it the best designed language?
Another problem is that the main idea of this PEP8 part is to use the convention in new modules.
Just one question: which one convention?
PyUnit Case
I wanted to use PyUnit in my code. My convention is like this:
def this_is_my_function():
x = 10
y = 20
return x < y
I was just trying to use PyUnit in my new funny module, I started with the documentation and suddenly I noticed that even in the example the whole convention is messed:
import random
import unittest
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.seq = range(10)
def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
self.assertRaises(ValueError, random.sample, self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
if __name__ == '__main__':
unittest.main()
I have to use the function setUp()
in my test even if I use another convention, just because there is no convention.
Let’s read it further…
Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used
in the module name if it improves readability. Python packages should
also have short, all-lowercase names, although the use of underscores is
discouraged.
Yes, Python library module names are a mess too, and don’t follow this Python standard.
Objective Python?
Many people tell me that Python is a really object oriented language. I’m sorry, Ruby looks much more object oriented.
Some time ago while writing Python code, I spent long minutes to find out how to count number of elements in an array.
In Java there is the method size() in containers, so it is obvious:
ArrayList list = new ArrayList();
list.size()
In ruby there is a method size():
x = []
x.size()
Knowing all that I just wanted to write my Python code the same way:
x = [1,2]
>>> x.size()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'list' object has no attribute 'size'
>>> x.length()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'list' object has no attribute 'length'
Yea, one buddy told me to use len(). Nice, I tried:
>>> x.len()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'list' object has no attribute 'len'
Well, that wasn’t easy. The correct answer is:
>>> len(x)
2
Sorry, I don’t get it, where is the object code if I have to use some global function? I know that this is a simple wrapper of the calling the function:
>>> x.__len__()
2
but I want to have a simple object code, it should work with a normal function size() or length().
A Small Sum_Up()
Many people say that PHP is a mess, and Python is much better. Yes, PHP is a mess. What is the difference between PHP and Python? Sorry, I don’t see any difference in the mess.
My advice is simple: if you know already PHP and want to switch to Python just because of the mess, think again. There is the same level of the mess.