Tuesday, November 3, 2015

Python Code Readability

As I mentioned before Python is a simple language with a focus on readability. This focus is expressed in its syntax:

* Lines of code end in newline (Contrast with Java, C++, Objective-C whose lines must end in semi-colon).

* Indentation is enforced in compile-time. For instance:

            This code compiles in Python:

                    if x > 0:
                       print(x)

            This code does not compile in Python:

                    if x > 0:
                    print (x)

The importance of code readability and indentation is very well explained in the Clean Code book by Robert C. Martin.

* No explicit data type specification when declaring variables:

             The following code in Java:
                     
                    int add(int x, int y) {
                         int result  = x + y;
                         return result;
                    }

                 Looks like this in Python:
                  
                    def add(x, y):
                          result = x + y
                          return result

Besides enforcing greater readability in syntax during compile-time, there are guidelines that a programmer should follow in order to write readable code in Python. Always keep in mind that most of your time as a programmer will be spent reading code. Hence, you should become very proficient in producing clean and readable code.

No comments:

Post a Comment