10 Coding Principles Every Programmer Should Learn: 1. DRY (Don't Repeat Yourself)
10 Coding Principles Every Programmer Should Learn: 1. DRY (Don't Repeat Yourself)
Should Learn
1. DRY (Don’t repeat yourself)
Our first object-oriented design principle is DRY, as the name suggests DRY (don’t repeat yourself)
means don’t write duplicate code, instead use Abstraction to abstract everyday things in one
place.
If you have a block of code in more than two places, consider making it a separate method, or if
you use a hard-coded value more than one time, make them public final constant. The benefit of
this Object-oriented design principle is in maintenance.
It’s important not to abuse it, duplication is not for code, but for functionality.
It means if you have used standard code to validate OrderIdand SSN, it doesn’t mean they are the
same, or they will remain the same in the future.
By using standard code for two different functionality or thing, you tightly couple them forever,
and when your OrderId changes its format, your SSN validation code will break.
So beware of such coupling and don’t combine anything which uses similar code but is not
related. You can further check out the Basics of Software Architecture & Design Patterns in Java
course on Udemy to learn more about writing the right code and best practices to follow while
designing a system.
The benefit of this OOP Design principle is that It’s easy to test and maintain proper encapsulated
code.
If you are coding in Java, then follow the principle of making variables and methods private by
default and increasing access step-by-step.
Several of the design patterns in Java uses Encapsulation; the Factory design pattern is one
example of Encapsulation, which encapsulates object creation code and provides flexibility to
introduce a new product later with no impact on existing code.
Btw, if you are interested in learning more about design patterns in Java and Object-Oriented
Programming, then you must check this Design Pattern Library course on Pluralsight. It’s one of
the best collections of design patterns and advice on how to use them in the real world.
The key benefit of this design principle is that a**lready tried and tested code is not touched
which means they won’t break.
Here is a Java code example which violates the Open-Closed Design Principle of Programming:
In this code GraphicEditor is tightly coupled with Shape, If you need a new Shape then you need
to modify already tried and tested system inside thedrawShape(Shape s) method, which is both
error-prone and not desirable.
Ideally, if you are adding new functionality only, then your code should be tested, and that’s the
goal of the Open Closed Design principle.
By the way, the Open-Closed principle is “O” from the SOLID acronym. If you want to learn more
about this principle, the SOLID Principles of Object-Oriented Design and Architecture course on
Udemy is one of the best resources to consult.
The key benefit of this principle is that it reduces coupling between the individual component of
the software and Code.
For example, If you put more than one functionality in one Class in Java, it introduces coupling
between two functionalities, and even if you change one feature, there is a chance you broke
coupled functionality, which requires another round of testing to avoid any surprise on the
production environment.
You can further see From 0 to 1: Design Patterns — 24 That Matter course on Udemy to learn
about patterns that are based on this principle.