Writing Clean and Modular Code

Ashley Gaskins
Analytics Vidhya
Published in
2 min readDec 3, 2020

--

Helpful tips on making sure you’re code is the best it can be!

Let’s help each other be the best programmers we can be. Credit for the image here

What do you think it means to have clean and modular code?

Modular code is having code that is broken into functions and modules. Having functions and modules makes coding easier because you can call on the function later without having to repeat and rewrite lines of code.

One important step in having clean code is to refactor it! This is a process that happens after you initially write your code. Look it over and see if you can clarify or simplify it. Why? To reduce workload in the long run. It’s easier to maintain and reuse, and it helps you become a better developer.

Image found here: https://blog.jetbrains.com/wp-content/uploads/2015/02/pycharm-refactoring4.png

Use meaningful and descriptive names for clean code. Remember DRY — don’t repeat yourself! You can use is_ or has_ to help with descriptions and show type. Use your white space properly. Keep spacing and indentations consistent. A great online tool to use is Pep8Online.

The site is free! You just enter in your code, click the ‘check code’ button, and results will appear as above. This is beneficial to see exact lines and columns of your code that need to be cleaned up!

Below is an example of some of my clean and modular code. Feel free to check out my Github page for more tips, projects and guides on coding in Python. Happy coding!

Short clip of clean code from my Github here implementing a Binary Search Tree :)

--

--

Ashley Gaskins
Analytics Vidhya

Data Science student looking for Data Science/Engineering opportunities!