1.1. Naming
1.2. Commenting
1.3. Code duplication
1.5. Complexity
3.1. SOLID
3.2. YAGNI
3.3. DRY
4.1. Code daily
4.3. Useful reads
Regardless of chosen naming/spacing convention, applying the conventions consistently matters. For example if the given naming convention specifies using only upper case letters for constants within the application, then stick to it as personalising the code opposed to keeping entire codebase in the same format, causes readbility, consistency and various other minor issues.
White space increases readability and should be used to separate ideas just like paragraphs in prose.(reddit, 2017)
E.g. for loop, the outer loop should be i, inner loop should be j, then k, etc. This is easier to follow for most people (reddit, 2017).
for(int i = 0;...){
for(int j = 0;...){
for(int k = 0;...){
...
}
}
}
Useful when choosing names(reddit, 2017):
hourly_rate
)compute_sum()
)is_max()
)Use comments sparingly, usually to reference external documentation or other resources. Filling your code with obvious comments liked int balance; // balance of account isn’t useful and just creates noise. Additionally if you feel the need to excessively comment your code it’s a good sign you aren’t being clear, try adding more methods and making your code simpler to understand.(reddit, 2017)
To be expanded upon…
Don’t repeat yourself. This includes not analyzing your code for areas where you could generalize a bit. This is a fine line though because there is such a thing as too general or too abstract. Instead of using variable names such as num1, num2, num3, …, numN use more appropriate data structure(e.g., array, dictionary, list)(reddit, 2017)
Not everything needs to be public. If it shouldn’t be called by any other code, it should be private. The fewer public members that are exposed the easier it is for someone else to understand what your class does and how to use it.(reddit, 2017)
If a function performs more than its’ name implies, then it’s quite likely that the function is too complex. If such is the case, then it’s recommended to break down the functionality into several smaller and more focussed functions. This approach produces much more modular, testable and extensible code, and also reduces places where the function may fail - which helps tremendously when debugging and writing code which needs to be secure!
This same complexity break down approach also applies to classes, and any other aspect of software.
SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible and maintainable. Acronym introduced by Michael Feathers.
Initial | Concept | Desciption |
---|---|---|
S | Single responsibility principle | “The single responsibility principle is a computer programming principle that 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.” |
O | Open/closed principle | “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.” |
L | Liskov substitution principle | “objects in a program should be replaceable with instances of their subtypes without altering the correctness, tasks performed, etc., of that program.” |
I | Interface segregation principle | “The interface-segregation principle states that no client should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces. ISP is intended to keep a system decoupled and thus easier to refactor, change, and redeploy.” |
D | Dependency inversion principle | one should “depend upon abstractions, [not] concretions.” |
“You aren’t gonna need it” (YAGNI) is a principle of extreme programming (XP) that states a programmer should not add functionality until deemed necessary. XP co-founder Ron Jeffries has written:
“Always implement things when you actually need them, never when you just forsee that you need them.”
“Don’t repeat yourself” (DRY) is a principle aimed at reducing repetition of all kinds.
When the DRY principle is applied successfully, a modification of any single element of a system does not require a change in other logically unrelated elements. Additionally, elements that are logically related all change predictably and uniformly, and are thus kept in sync.
Most classroom courses love them some inheritance, but in the real world composition via interfaces is much easier to test and maintain. Composition forces you to make your code more modular which makes it easier to test. It also gives you the flexibility of mocking out interfaces until you are ready for them, which lets you focus on the task at hand during development instead of “hopping around” to various functionality when trying to complete a task. This added focus tends to make you write better code in earlier iterations since you are not trying to juggle too much scope in your head at once.(reddit, 2017)
Self note: Yeah, that’s correct, stick to this rule as much as possible! (additional elaboration can be found here)
By everyday, I mean everyday! How do professional athletes improve their skills? By practicing on a daily basis. Sure there can be off-days, where you take a break from it, but this should not be the norm. Name one athlete that won a gold medal by only practicing once or twice a week. Learning how to code is very similar to learning a new language or art. It takes a lot of practice! You can know everything there is to know about computers. But without practice, you won’t be able to write code that is maintainable, let alone write something that people would actually want to read.(The Coding Delight, 2017)
Every developer should read high quality open source code. When I first went through underscore.js and jQuery (not the entire code base), my mind was blown. Let me warn you: because the code is written by professionals, readers need to have a strong foundation in programming and in the language that the source is written in. In the the case of underscore and jQuery, the language would be JavaScript.
In order to get a good open-source education, I recommend finding source code that is
- Relatively short (roughly around 1000 lines or less would be ideal)
- Well documented.
I recommend reading relatively short source code for two reasons. Firstly, short source code means readers don’t have to spend as much time navigating the source to connect the pieces. Secondly, readers can fully understand shorter code bases much faster than a framework that has 100,000 lines of code. Shorter code bases such as modules are compact. Readers only see what they need to see.(The Coding Delight, 2017)