Use Modular Programming To Improve Your Code And Life

Scott Waring
3 min readNov 13, 2018

--

Breaking your code out of long form functions will increase your codes readability for other programs and expand your own understanding of how the code actually works.

Modular programming is a design technique for separating code into independent and interchangeable parts (aka modules) responsible for handling one part of an applications functionality, to put it another way it is sub-dividing the program into individual sub-routines.

The advantages of breaking code in to small parts are many:

  1. Application building can more easily be distributed across teams with programmers responsible for building specific sub-routines that fit into the larger architecture.
  2. Modules can be created in a way that allows them to be used for multiple purposes keeping your code dry.
  3. Code can be easier to debug since everything is broken into smaller parts is then easier to isolate the errors.
  4. Making changes and updates can also be easier.
  5. Generally less code will be written.
  6. Variable scoping is easier to control.

If you find that you are breaking the DRY principal (don’t repeat yourself), or that your code base has become a monolith that’s hard to decipher or debug, then your code maybe a good candidate for some modular refactoring.

Here’s a convoluted analogy for functional vs modular programming.

Imagine you are planning a cross country road trip, New York to California, this trip is your application. To complete this trip the driver will need to refuel, get food, sleep, use the restroom, and hopefully not suffer a breakdown.

The functional approach would wrap up all the gas, food, rest room breaks, and sleeping in the car. How hard would it be to fuel the car while driving down the road? And when nature calls? Disgusting. The car will quickly become an overflowing mess. This needlessly complicates what should be simple.

The modular approach says that you don’t need to carry all your gas etc, because when you need gas you make a call to a gas station and fill up. If it’s true that you are hungry, then pull over and eat till your hunger is false. The modular approach breaks all the parts of the larger functional way into parts into smaller logical functions that will be called as needed. It is a simple clean way to structure the trip in a way that allows you access to everything you need but with out having to bundle it all into one your car. Modular programming says you should execute the functions only when you need them, otherwise they can sit idle.

There will be use cases for modular and functional programming only exposure over time will help you better evaluate when one is better than the other. Every programmer though can benefit from making their code more flexible to meet the depends of their application, to simplify their code down to what is necessary, and to keep it dry.

--

--