Breaking Down the CODE: How to keep the modern design way of coding

One way to avoid writing massive amounts of code is to use modular design principles. This involves breaking down your code into smaller, reusable components that can be easily managed and maintained.

For example, instead of writing one large function that performs multiple tasks, you can write several smaller functions, each of which performs a specific task [UNext Editorial]. This makes it easier to understand and modify the code, as well as making it easier to test and debug.

Before:

def process_data(data):
    data = data.strip() #remove whitespaces
    data = data.lower() #convert to lowercase
    data = re.sub(r'[^\w\s]','',data) #remove punctuation
    data = re.sub(r'\d+','',data) #remove digits
    data = data.split() #split data into a list
    return data

After:

def remove_whitespaces(data):
    return data.strip()

def convert_to_lowercase(data):
    return data.lower()

def remove_punctuation(data):
    return re.sub(r'[^\w\s]','', data)

def remove_digits(data):
    return re.sub(r'\d+','', data)

def split_data(data):
    return data.split()

def process_data(data):
    data = remove_whitespaces(data)
    data = convert_to_lowercase(data)
    data = remove_punctuation(data)
    data = remove_digits(data)
    data = split_data(data)
    return data

I got the concept of this example code from [niharika].

As like the example above, to avoid writing massive code is to use libraries and frameworks that provide pre-built functionality. For example, instead of writing your own code to handle database interactions, you can use a library like SQLAlchemy. This can save a lot of time and effort, as well as making your code more reliable and maintainable.

After reading multiple articles about the efficient way of coding, having monolithic functions is what I need to aware of. This is because such functions are often difficult to understand, test, and modify. They also tend to be more prone to bugs and errors, and can make it more challenging to identify the source of a problem. By breaking down the code into smaller functions or modules, it makes it more manageable, easier to test and debug, and less prone to errors.

In this article, we discussed the benefits of modular design and provided examples of how to apply it to your code. We also highlighted the importance of breaking down monolithic functions and the pitfalls of writing massive code. The key to avoiding massive code is to break it down into smaller, manageable components.

References

  • UNext Editorial , T. (2022, December 15). What is a modular design? everything you want to know in 8 easy answers! Jigsaw Academy. Retrieved January 26, 2023, from https://www.jigsawacademy.com/blogs/product-management/modular-design/#:~:text=Modular%20design%20in%20software%20engineering,designed%20to%20achieve%20specific%20functionality.
  • niharika. (2018, September 7). Modular approach in programming. GeeksforGeeks. Retrieved January 26, 2023, from https://www.geeksforgeeks.org/modular-approach-in-programming/
Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *