Being a CRM developer is challenging and from time to time we may find out that solution we provide is not the most elegant solution there is. Therefore, it is important to remember to always write good and clean code. We can ensure that by following few core programming principles which have proven themselves to be true.
Single responsibility principle
Single responsibility principle is one of core principles you need to follow if you want to write clean and simple code. An insert from Wikipedia’s great article on the topic:
The single responsibility principle states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. Robert C. Martin expresses the principle as follows: A class should have only one reason to change
So what does it mean “A class should have only one reason to change”? As an example we can imagine a function that sets text content of a webpage as well as CSS styles for the content. This means function has two responsibilities and worst case scenario is that something goes wrong in both. Imagine a situation where you use this function and after execution your webpage doesn’t behave as expected – your text is missing. Is there a problem with text publishing or maybe styles are not applied correctly? Changing code for one process can affect another process. Right way to do this would be to separate this function to two different functions, each of them having single responsibility – one for text publishing, another for applying styles.
You ain’t gonna need it (YAGNI)
YAGNI principle states that a programmer should not add functionality which is not necessary at the moment. We should implement things when we need them and not when we think we are going to need them. By following this principle we can avoid wasting time writing unnecessary code. For example, it may look like we are definitely going to need a feature X just to support another feature Y which is going to be implemented next year. It is wrong to implement feature X right now because a lot can change in this time period and we may end up not implementing that feature Y at all, meaning we wasted time implementing feature X.
Don’t repeat yourself (DRY)
DRY principle aims at reducing any kind of repetition during the process of development. This principle states: “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” This principle should be applied to all aspects of development such as database schemas, software design, documentation, etc. With respect to very act of code writing it means that we should not write same code twice at any cost. If we end up with two same or even similar pieces of code they can probably be refactored.
Keep it simple stupid (KISS)
“Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better.” ― Edsger W. Dijkstra
When writing code we should follow KISS principle. As Wikipedia article says:
“The KISS principle states that most systems work best if they are kept simple rather than made complicated; therefore simplicity should be a key goal in design and unnecessary complexity should be avoided.”
Simple systems are easy to maintain and enhance if necessary. They are easy to understand and most of the time self commenting.