DEV Community

Kaushal Sharma
Kaushal Sharma

Posted on

Write clean, reusable, and modular code

If you’ve ever searched for a job in tech or you’re just starting to code and looking for your first job and see -

Must be able to write clean, maintainable and reusable code

In the top one or two bullet points of the requirements and not know how to do it?
What recruiters are basically asking is, “Can you write modular code”

But what is Modular Programming and why should you care about it?

  • Modular programming means breaking up your code into small chunks, that often repeat itself over the lifetime of your code execution, into small and manageable modules which can be split across different files.

And to answer why should you care about it?

Well, Apart from breaking up one large problem into smaller, more feasible problems, additional reasons to make your code modular are -

Image description

  • It makes your code more Maintainable - Ideally each module you create would be designed to tackle a certain singular problem, hence reducing the interdependency between each module. Therefore, if you were to make some changes in Module1, Module2 or the rest of your code would remain unaffected for the most part. This also facilitates collaboration between a team of programmer where each programmer can focus on one module at a time and making debugging a whole lot easier!

Image description

  • You can Reuse your code across different modules, even projects! - Since your module would tackle a single problem independently, it is possible to use this module in other modules or even projects where you face the same problem statement making your code DRY(Do not Repeat Yourself), one of the many important software engineering principles.

Image description

  • Variable Scoping - Creating different modules and splitting them into different functions or even files allows you to use the same variable name, avoiding a debugging nightmare from spaghetti code!

But wait, you’ve already tried and it did not work? here’s why

Because you may have forgotten to add your project directory to the list of directories the python interpreter searches for modules in!
Which brings us to the next topic -

Module Search Path

  • One thing to note is that, the location of the module that you have created, should be present in the PYTHONPATH environment variable.
  • The reason for this is whenever you “import” a module, the python interpreter will search for that module in list of directories. This list of directories contains
    • the current script’s directory,
    • all the directories present in the PYTHONPATH variable
    • the installation dependent directories which would’ve been configured at the time of python installation.

Image description

Let’s see what we’ve read till now in action

Project Root -

Go to your project root directory

Image description

Checking all the directories present in the list of directories of sys module. We see that our project root is missing here.

Image description

Adding Project Root to sys.path

Using the os built-in module, since we are already in our project root, we add the current working directory to the list of directories present in sys

Image description

Now we’ve added our project root to the list of directories and we can proceed to import the modules!

Now, when you type import, the interpreter should automatically show you the modules/directories available to import. Here we see both the directories we created(dir1 and dir2) which contain their respective modules.

Image description

After selecting dir1 and adding a period(.), you should be able to see all the modules/directories contained in this directory. Here we see two modules, main and module1.

Image description

Using the module

After importing the module1 from dir1 and giving it an alias as mod1, we can use all of the module1’s objects in our current script!

Image description

We can use the string object defined by variable a, as well as the function defined in the module1

Image description

Similarly, we can import another module and use all of it’s objects in our current code

Image description

In such a way, we can structure our code better so changing or update in any single module does not breaks other modules.

Creating different modules for your code instead of just blurting it out in a single script also allows for ease in unit testing, which is also a primary requirement in many places.

Now off you go writing clean and maintainable code!

Top comments (0)