One of the well-known truths about programming is that the readability of the code is one of the most important factors. Everybody knows that the longest part of every software life cycle is its maintenance, and the more readable code, the easier to maintain it is.
While comparing Python with Java, some common “truths” can also be spotted. One of them is the “fact”, that Python code is more readable than Java code. This thesis originates from an other one: the Java code is more verbose than the Python code. How could’ve someone suspect, that the more verbose code could be easier to read?
Well, I have no doubt that Java is more verbose than Python. The question is: can we assume that verbosity is the exact opposition of readability? Because if this assumption is false, then the thesis of Python code being more readable than Java code also might be false.
In my personal opinion, code verbosity is something quite different than code readability. Moreover, I think that Java code is easier to read than its Python equivalent, and it’s due to the fact, that Python uses dynamic typing, and thus hides (sometimes essential) information from programmer. In order to prove it, I’ll analyse some simple examples of Python and Java code.
The Python examples
The first example is a very simple Python code:
class SomeClass:
def someMethod( someArg ):
(some stuff here)
class SomeBaseClass:
(some stuff here)
class SomeOtherClass( SomeBaseClass ):
(some stuff here)
What can we say about it? Not too much. The used classes and methods names are horrible: they don’t say anything about their purpose. All we can say about the code presented above is that this SomeOtherClass is based on the SomeBaseClass. Such a code is very hard to read and understand.
So, let’s try to make this code easier:
class Forest:
def getTree( treeID ):
(some stuff here)
class Tree:
(some stuff here)
class Oak( Tree ):
(some stuff here)
That’s much better. Now we can see that the second and the third classes are related to the first one. We can also suspect that the getTree method returns a Tree object, according to the given treeID.
So far, all I showed is the fact, that using meaningful names for classes, objects, methods and variables might make code more readable. This is a common-known truth, though, and it applies not only to Python, but to any programming language.
The Java examples
Now, let’s take a look at the first example in Java:
public class SomeClass
{
public SomeBaseInterface someMethod( SomeType someArg )
{
(some stuff here)
}
}
public interface SomeBaseInterface
{
(some stuff here)
}
public class SomeOtherClass implements SomeBaseInterface
{
(some stuff here)
}
This is the equivalent for the first example of Python code. What can we say about it? The code is more verbose, of course. We have to write more lines of code in Java than in Python. But there is something more: although the used names are still horrible, we are able to notice a relationship between all the three classes: the someMethod method returns an object of the SomeOtherClass class. That’s more than we could say about the Python code.
That’s not all, though. We can say that the SomeBaseInterface is an interface. Thus we can say something about the analysed API design.
Let’s get to the second Java example, which is the Java equivalent for the Python second example:
public class Forest
{
public Tree getTree( int treeID )
{
(some stuff here)
}
}
public interface Tree
{
(some stuff here)
}
public class Oak implements Tree
{
(some stuff here)
}
Here, almost everything is clear. We can get all the information we could get in the first example, and also we can say that a Tree object identifier is an integer value.
What does it mean?
After the examples have been shown, it’s time for some conclusions.
We can safely assume that code in practise is like from the second example. In general, programmers tend to use meaningful names, because it makes their work easier. So, let’s focus on the two second examples first.
Although the Python code is less verbose, it hides one information: what is this treeID? It’s some kind of Tree identifier, of course, but what exactly is it? An integer value, a string, or something else? We don’t know it. So even using meaningful names in Python code won’t give us the information we get easily reading Java code.
Of course, one could argue that such information could be in the code docs. Yes, that’s right. But it would make Python code more verbose, and it makes programmer to document even trivial functions/methods. In the Java code no documentation is needed in order to find out what the Tree identifier is. Therefore I find the Java code more readable.
And when we take a look at the both first examples, and start an academic discussion, we can see that the Java code contains much more information than its Python equivalent, and thus we can say it’s more readable, even being more verbose.
The conclusion
Of course, it’s the well-known dynamic vs. static typing debate. Personally I’m all for static typing, provided that I have to write more than 100 lines of code. In another words: Python is better while writing small scripts, while Java takes its ground in more complex projects. It’s one of the reasons for which I decided to switch from Python to Java, although I prefer higher-level programming over lower-level one.
In my opinion, programming in Python might lead to writing less lines of code, using a higher level of abstractions, and thus completing projects faster, than programming in Java. But it all comes with some price, and this price is code readability. For me, Java code is more readable, because it shows more details about itself, and thus makes the analysis easier. And I know it not from my theoretical assumptions, but from practical experiences. Java code written by someone else is much easier for me to read, than Python code written by someone else.