Code Quality

Often when developing softwares the initial goal is to solve the domain specific problem as soon as possible. And this is how it should be. However, in rushing to get the feature done may result in compromising code readability, modifiability, testability and ultimately delivering a poorly coded and hard to maintain software. Which may not perform as expected when go live.

A well designed software architecture should address all of these issues in advance. And delivers better experience for stakeholders as well as its maintainers.

In python, there are few tools which comes handy in finding code complexity, logical issues, code smells and ultimately improving code quality.

pylint, is the first in the list that can help in finding syntax error, wrong indentation, invalid variable name etc. It also give rating at the end of check. Below is the sample output of pylint.

[bash] >> pylint
    ************* Module app
    C: 51, 0: Wrong continued indentation (remove 1 space).
     ), 200, {'ContentType': 'application/json'}
                               |^ (bad-continuation)
    C:  1, 0: Missing module docstring (missing-docstring)
    C:  9, 0: Invalid constant name "app" (invalid-name)
    C: 19, 0: Missing function docstring (missing-docstring)
    C: 24, 0: Missing function docstring (missing-docstring)
    C: 29, 0: Missing function docstring (missing-docstring)
    W: 30, 4: Unused variable 'xyz' (unused-variable)
    C: 35, 0: Missing function docstring (missing-docstring)
    C: 54, 0: Missing function docstring (missing-docstring)
    C: 62, 0: Missing function docstring (missing-docstring)
    C: 69,36: Invalid variable name "f" (invalid-name)
    C: 83, 0: Missing function docstring (missing-docstring)
    W: 84,16: map/filter on lambda could be replaced by comprehension 

    Your code has been rated at 7.17/10 (previous run: 8.14/10, -0.96)

pylint can be install using pip python package installer. pip install pylint

mccabe, is another python tool to help finding code complexity. This is the result of mccabe command run against python code.

    [bash] >> python -m mccabe
        ("3:0: 'num_str_map'", 4)
        ("14:0: 'num_str_map_refactored'", 1)

Below is

    def num_str_map(num):

        if num == 0:
            return 'a'
        elif num == 1:
            return 'b'
        elif num == 2:
            return 'c'
        return 'z'

    def num_str_map_refactored(num):

        num_str_map = {
            0: 'a',
            1: 'b',
            2: 'c'
        return num_str_map.get(num, 'z')

Both the above functions produces identical result. But one is more complicated than other. And based on mccabe output code can be further refactored in order to reduce its complexity.

These small set of tools can be utilized from the very beginning of development to avoid any technical debt. And delivering an easy to maintain and extendable code.

I am skipping code testability here for now. Since it requires much more details and should be covered in separate post.


Now read this

Python class & static method

In this post i will try to explain python’s class and static method and how to use them. prerequisites # In order to understand class and static method, specially class method, one should know the difference between class and instance... Continue →