DRY

Don’t Repeat Yourself
Write each line of code only once.

If somewhere in your application you feel like you have written very similar code before, or worse, are copy-pasting code from somewhere else, you are probably doing something wrong. Duplicated code makes it hard for other developers and you to extract the really relevant instructions, and debugging a living hell (as you correct a mistake in copy-pasted code, you will have to do the same thing at all other places where you have used that code).

You can omit duplicated code by abstraction; write your code in a way that it works for all inputs. Use interfaces and common parent classes to execute tasks on similar objects: bad vs good (see method public double CalculateAreas())

If you can’t reduce the problem by implementing interfaces or inheriting from common parents, you can take it a step further and try reflection: Reflection allows you to determine at runtime what properties and what methods you want to call from an object. As this topic is rather complex, and differs a lot from language to language, it is probably best if you look up for yourself how it is implemented in the language of your choice.

 

One Comment

Leave a Reply

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