Best Practices for Becoming A Good Python Developer

Best Practices for Becoming A Good Python Developer

Introduction

Many developers or students write code, a lot of code, but sometimes forget to follow some good practices which can make the code written more efficient and readable. There are some good practices to be followed while programming. It is not just only for Python developers, it is applicable for each programming language like C++, Java, C#, etc. So, in this article, we are going to look at some of those ignored but interesting and important practices to be kept in mind while programming.

No alt text provided for this image

Look at the image above. I know it’s quite funny. But the point I want to make here is that Python is this cool. Python programmers can effortlessly do a task whereas others have to sweat a lot for the same task. Hence for python developers, it is important to follow some practices which will make their work even more productive.

So, Let’s get started.

1. Proper Documentation and Commenting

This is the first and foremost point I want to put to be a good python developer. It is really important to follow this. Whatever code you are writing should be well documented and commented on by you wherever required. But do you know why we need Code documentation? The answer is very simple. A company project may go for years and years. New developers join the project any time, so now they need to know and understand what is in the code, for this purpose code documentation is important. They can simply refer to it and will get a clear understanding of the code. Just think about that developer if there was no documentation and you simply throw him into the pool of code. It’s really difficult for him without this. Commenting is also for the same purpose, you should be adding comments in your code wherever required.

There are mainly three types of comments:

a. Single Line comment:?They start with the hashtag symbol(#) and last till the end of the line.

Example:

#This is a commen
#Printing Hello World!
print("Hello World!")        

b. Multi-Line comment:?It is a text enclosed in a delimiter?(""")?on each end of the comment. There should be no white spaces between delimiters?("""). They are used when the comment text is more than one line.

Example:

""
This is a multi line comment,
spanning over three lines.
This is how it's done.
"""
print("Sum of two numbers:", 45+23)"        

c. Docstring Comments:?It is an in-built feature of

Python. It is used to associate documentation that has been written with Python modules, functions, classes, and methods. It is used just

below the functions, modules, classes to tell what they are doing. The docstring comment is made available by the __doc__ attribute.

Example:

def add(a, b)
    return a+b
print(add.__doc__):        

Output:

Add the two numbers a and b        

So, use any of these comments and help other developers to know what you have done.

2. Avoid Creating Global Variables

Global variables are those which remain in the application till the last breath i.e., till the time application is running they exist in the code space and can be invoked anytime from anywhere. These are useful at times because of their accessibility but at the same time, this proves to be a disaster for developers. Because of these variables memory also can’t be used efficiently, because a large chunk of your memory will be permanently going to these global variables.

There is one more problem with them. Since every function in your application can access global variables so, it becomes very difficult to identify which function is reading or writing the value of that global variables. Now, to identify this you need to take a closer look at all functions individually which is a headache.

Hence, just avoid these global variables.

3. Exception handling

Source: Google Images

This is a very important practice to be followed by Developers, not only python developers but by all developers of each and every language. So, let me tell you the reasons why it is needed.?Suppose you wrote a program to open a text file and perform certain operations on it like reading the file, closing it, finding the length of the file. This would be a simple code to write but what if some errors like these occurred while execution:

  • What if the file can’t be opened?
  • What if the length of the file can’t be determined?
  • What if the read fails?
  • What if the file can’t be closed?

What if the program threw these errors? Your code will come to a stop and your application will not work. So, these are potential errors that can occur because of any reason. Hence it’s important to handle these exceptions instead of ignoring them. Hence it’s a must to use Exception handling in your code.

Example:

try
  print(y)
except:
  print("An exception occurred in your code!"):        

The?try?block will generate an exception because?y?is not defined.

4. Create Separate Environments

Python programmers often try to work in a single environment, they don’t create a separate environment for different projects. It is strictly not good practice to implement all projects in a single environment because you may face some issues in the later stages of development, then you have to change some installations, modify their versions, install new ones. This may affect previous projects which might not accept these new changes as they were dependent on these dependencies. Hence, always prefer to use a new environment.

No alt text provided for this image

Installing virtualenv

$ pip install virtualenv        

Test your installation:

$ virtualenv --version        

Using virtualenv

$ virtualenv my_name        

Now once you created your virtual environment, you need to activate it. So, for that use this command:

$ source virtualenv_name/bin/activate        

When your work is complete in that environment, then close it using this command:

(virtualenv_name)$ deactivate        

5.?Use inbuilt functions

Use inbuilt functions of python instead of writing them from scratch, because they are already compiled, optimized for efficient execution and if you are writing them from scratch you are missing out on a big advantage.

6. Structuring of projects

The structuring of projects is also a very important part of development because it shows how efficiently you are going to write your code, how the entire project is coupled. So, if your project has a front end, back end, database part, APIs, and other things and if you have not managed all this then it’s going to be a very messy project. Suppose, later you want to make some changes in your code but you won’t be able to do so, because there is no structure in your code and now it’s a real mess to find what thing is where.

End Notes:

These are some very good practices to be followed by every student or developer. They will for sure help you in your programming career. I hope you find the pointers in this article helpful. Let’s connect on Linkedin .

Thanks for reading if you reached here :).

Happy coding!









要查看或添加评论,请登录

Rajesh Thangaraj的更多文章

社区洞察

其他会员也浏览了