DEV Community

Cover image for An Example of How Software Becomes Complicated

An Example of How Software Becomes Complicated

jorin on May 27, 2018

Let's write a cache in JavaScript and see what it means to keep things simple. We always hear software developers saying things like we should kee...
Collapse
 
marcel_cremer profile image
Marcel Cremer • Edited

Even after reading the article, I have to disagree and think the DRY-principle still should be applied and in fact, you should be afraid of copying code for all the reasons, one wants to apply DRY and keep maintainability.

You're already showing it by the last example, where you dependency inject a cache. The expire-Cache is basically a wrapper to an original object which might be perfectly suitable. However, calling it "copy code" might be misleading, especially for new devs.

To be more precise:

Every time you implement a feature by simply adding one more IF statement, you help growing it further – the big ball of mud.

I think this is the exact point, where you need to immediatly stop your work(!) for the moment to think about what you're doing and why you're doing it!

In your example, you had a simple feature, which had one purpose and it served it well. After a while, you got already 2 times additional feature requests, that should be implemented for your feature.

You're "simple" feature has now outgrown it's state of "simple" to a crucial part of your application and needs refactoring!

The "IF-Statement" addition is just patch for the bullet wound you've just identified.

Instead you should now think of treating it well, with (non-exclusively) the following thoughts:

Refactoring

  • How can I keep things working, if I strongly modify it in the future? (Securing by Unit-Tests if not tdd, ...)
  • What parts have a single purpose?
  • Do I need to put some parts of my code in well named, smaller functions/ methods to keep their single purpose and be able to reuse them?

For Object-oriented languages:

  • Do I need some Interface and different Implementations?
  • Can I DRY functionality by using abstract classes or mixins with partial implementations?

Overall Design/ architecture Thoughts

  • Can I apply some well known patterns (e.g. Repository-Pattern, Dependency Injection,...) to keep my code simple, but also generic?
  • Is this place responsible for doing, what it does? (e.g. is the cache responsible to expire here, or do I need some Scheduled Worker / external service / ... which should handle that)

Adding an IF-Statement is a quick-fix and sometimes necessary when there are close deadlines (combined with a TODO:-Comment to refactor it later) - and I fully understand that it sometimes needs to be done.

But real duplication of code is like seeding and watering our challenge, causing the future developer (who might be you!) to face a real problem!

Edit:
Added a sentence to make it sound less offending ;)

Collapse
 
vikkio88 profile image
Vincenzo

The main problem there is that you are not using semicolons 😭

Collapse
 
jsn1nj4 profile image
Elliot Derhay

😆

Collapse
 
jsn1nj4 profile image
Elliot Derhay

Thanks for sharing that. That's a way of handling this type of problem that I hadn't seen before. Can't wait to try it out myself.

Collapse
 
mrm8488 profile image
Manuel Romero

Great post!