Why Write Reusable/Modular Code?
While this question and the corresponding answer might feel obvious to some people, someone in our community recently asked it, and if one person asks it, it means others are thinking it.
If you feel that you already know the answer, use your trusty browser "back" button and return to your mission.
If you are interested in the answer or would like to contribute your thoughts, please leave a comment!
I saw this recently (https://twitter.com/rtfeldman/status/1092885754265489408)

I think it's more related to the issue of trying to abstract code too soon or to a too high level. however being able to reuse some very common part of code (e.g authentication system) without having to start from scratch is will save us a lot of time.
@SimonLab thank you for sharing! However, I feel it is worth adding the relevant context. The tweet relates to people attempting to reuse code in a specific project by making it "DRY" ... Rather than the the attempt to modularise code into independently tested reusable packages.
I agree that DRY ("Don't Repeat Yourself" https://en.wikipedia.org/wiki/Don%27t_repeat_yourself ) can be misused and often well-intentioned people end up creating monsters of unmaintainbility! "DRY-ing" out code prematurely often results in complexity because instead of making functions smaller people make them bigger. i.e. the function does too many things.
Everything in modern computing it is made possible because of code modularity and reuse. Just look at the Linux Operating System on your Phone, it follows the Unix Philosophy:
- Write programs that do one thing and do it well.
- Write programs to work together.
- Write programs to handle text streams, because that is a universal interface.
Eric Raymond (author of "The Cathedral and the Bazaar " and "The Art of Unix Programming"), listed as his first rule: "Build modular programs". Making a program or function tiny and then assembling a larger application out of lots of small parts is how reliable large scale systems are built.
Every time we turn on a computer (or device that has an integrated circuit) we are benefiting from the reusable/modular code that someone else has made the effort to write that way. If there was no reuse or modularity there would be no Internet (as we know it today). If each time someone sat down to start writing a program they were not allowed to reuse anything that other people have released as "modules", each programmer would have to write their own Operating System, Editor, Programming Language, Compiler, Linker and Distribution system. While some people are certainly capable of achieving this single-handedly, it make software development virtually impossible for the "rest" of us.
Richard has contributed a lot to reusable open source code e.g: https://github.com/rtfeldman/elm-css and has benefitted immensely from the contributions of others. e.g: github.com/elm/compiler
As Richard says: The original authors meant
"strive for a single source of truth in your data model and business logic"
This is excellent advice that everyone should follow in their programs/applications.
Further down Richard says (with regards to the acronym "DRY"):
"It'd have been better if they'd never coined it."
I respectfully disagree with Richard that "DRY" should never have been coined. "DRY" explains a very important and useful principal in Computer Science & Programming:
"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system".
Without this principal programs are chaos!
"DRY" should be contextualised for people as:
- If you find you are repeating yourself in your code, write a small function and call it instead. That way if/when you need update the function, you only do it one place, re-run your tests and if they all pass you know you haven't "broken" anything inadvertently.
- Always write small functions that do only one thing and are easy to read, reason about and test.
- If you see that your code can be useful in a different context (e.g. you can reuse it in a different/unrelated project or someone
elsecan use it to solve their similar problem),thensplit it into an independently tested and documented module/package that does one thing.
I agree that an Authentication system is a perfect candidate for modularity and is a highly reusable. 👍
https://motherboard.vice.com/en_us/article/43zak3/the-internet-was-built-on-the-free-labor-of-open-source-developers-is-that-sustainable
