Modular Coding
What is modularity?
Modularity refers to the practice of building software from smaller, self-contained, and independent elements. Each element is designed to handle a specific set of tasks, contributing to the overall functionality of the system.
Modular coding is explained in more detail in these slides
Writing functions
One of the best ways to improve your code and to make it more modular is to write functions. Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. Writing a function has four big advantages over using copy-and-paste:
- You can give a function an evocative name that makes your code easier to understand.
- As requirements change, you only need to update code in one place, instead of many.
- You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
- It makes it easier to reuse work from project-to-project, increasing your productivity over time.
A good rule of thumb is to consider writing a function whenever you’ve copied and pasted a block of code more than twice (i.e. you now have three copies of the same code).
Challenge: Identify code that can be put in a function
In your own project: identify code that would fit better in a function. Try to look for pieces of code that you repeat throughout your project.
Create an issue in your project for each possible function that you find. (Actually implementing the function is beyond the scope of this workshop).
GitHub issues are a good way to track your progress and to-do list. As well as a way for others to signal issues with your code.