Thursday, March 3, 2022

Conventional coding style

This primarily focuses on how we write function names, variable names, and module names. Python has a coding system and naming conventions, which were discussed in the previous chapter of this book. It is important to follow the coding and naming conventions, especially when building reusable modules and packages. Otherwise, we will be discussing such modules as bad examples of reusable modules.

To illustrate this point, we will show the following code snippet with function and parameter names using camel case:

def addNumbers(numParam1, numParam2)

#function code is omitted

Def featureCount(moduleName)

#function code is omitted

If you are coming from a Java background, this code style will seem fine. But it is considered bad practice in Python. The use of the non-Pythonic style of coding makes the reusability of such modules very difficult.

Here is another snippet of a module with appropriate coding style for function names:

def add_numbers(num_param1, num_param2)

#function code is omitted

Def feature_count(module_name)

#function code is omitted

Another example of a good reusable coding style is illustrated in the next screenshot, which is taken from the PyCharm IDE for the pandas library:


The functions and the variable names are easy to follow even without reading any documentation. Following a standard coding style makes the reusability more convenient.


Share:

0 comments:

Post a Comment