Improving one's coding skills: Best Practices & Resources
1177 words
· 6 minute read
Last updated: Jul 16, 2023
· Published: Aug 8, 2020
In this document, I present my favorite best practices & resources on improving one’s coding skills. Warning: This document is not concerned with learning a programming language. It is directed at people who already know how to code, and who want to improve their craft.
In the first part, I will pick & explain some concrete coding best practices & principles that are easy to follow and will already improve your code significantly. Then, I will talk about general resources related to improving one’s code. I will end with a link-list of further blog posts that influenced the way I code.
Concrete & Easy to apply Coding Best Practices
Early Returns: Getting rid of if/else spaghetti code
One thing that I see very often in code are nested if
statements:
|
|
Nested ifs are evil! They make it very hard to get the program flow into your head. Actually, I would even go that far and say that else blocks should be avoided as much as possible. else-blocks often become the place where you do your error handling logic - but this means that the error handling gets pushed down to the bottom of your method and you it becomes hard to keep in mind under which conditions which error handling occurs. Instead, you should do the error handling as soon as possible and focus your mental energy on the happy path!
One feature to the rescue: Early returns! I don’t know how, but programmers often get caught up in the idea that a method should only return at one well-defined point at the bottom of he method’s code. Until then, allnecessary information for returning gets passed through all of the method’s code. But this is a mistake! Often, you can easily rewrite code like the one in the example like this:
|
|
More infos
- https://blog.codinghorror.com/flattening-arrow-code/ (also: Try to refactor your
if
-conditions so that they are positive ⇒ easier to read) - http://wiki.c2.com/?ArrowAntiPattern
- https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html
- https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement
Comments: Code should explain the what, comments the why
TODOs
- Code should explain the what, comments the why. The best comments are the ones you don’t need. https://blog.codinghorror.com/code-tells-you-how-comments-tell-you-why/
- Make everything as
private
and asconstant
as possible. https://stackoverflow.com/a/8353371 , https://stackoverflow.com/a/3182664/3327577 - Think twice before using class inheritance. People tend to overuse inheritance. Inheritance can easily lead to confusing code. You almost always can do without inheritance. Use composition over inheritance. https://news.ycombinator.com/item?id=19863871 , https://stackoverflow.com/a/37624298/3327577 , https://softwareengineering.stackexchange.com/a/371715
- Baseline when coding: Think about your future collegues & your future self. Make your code easy to read (vs. easy to write). It will be read much more often than modified ⇒ take time for naming and structuring, refactor often,…
- Variable names: https://wiki.c2.com/?GoodVariableNames
- Use variable names that describe what the variable is doing. Don’t just use random letter. If in doubt, prefer longer variable names over highly-abbreviated ones.
- Do not use Magic Numbers directly - extract them into constants with good names & use the constant instead
General Resources
The best resource: Discussing with coders around you
First things first: The best way to improve one’s skill is to talk & discuss & pair up with coders around you and learn from them. Be it your co-workers, your fellow students, or some friends on the internet. By actually talking to people, you will get a lot more context than by simply reading articles on the internet. Also, by discussing with them, it helps you structure & formulate your own thoughts, instead of simply consuming knowledge. So, even if the people around you are not the best coders, the discussion helps you improve. That said, best case is of course when you get to talk to people you have better coding skills than you and to learn from them. A big “Thank you!” to Johannes, Johnny, Joel, and Jochen, who I all admire & who significantly influenced my way of coding!
A general advice: Challenge everything you hear and read and make up you own thoughts. Don´t blindly follow the advice of someone without agreeing on the reasons behind. This counts both for personal discussions, as well as for blog posts. Even the most well-regarded programming books might have advice that you should not follow, e.g. because your situation does not fully apply to the one assumed in the books.
To challenge everything (to an extent) is a good advice for (not just professional) life in general… 😉
The one book: Clean Code
If there is one book that is regarded as “the” book on coding craftsmanship, it is Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (also called “Uncle Bob”). This is the bible of coding, and has a lot of inspiration to offer. In fact, a lot of the coding advice you’ll find on the internet is probably just a rehashed version of some of Clean Code’s concepts.
The book was published in 2008, which is a long time in IT, but has aged well. That said, I like the book especially for his general inspiration and ideas, e.g. on the purpose of clean code, how to distinguish good code from bad code, code smells. I don’t especially like the refactoring code examples and would recommend to not follow them religously - I find them too verbose and to actionable enough to apply on one’s own code.
Clean Code collections for specific programming languages
Clean Code is very well-known in the programming world, and you will find many references to it in “programming subculture”. E.g. some people on GitHub have documented how Clean Code concepts can be applied to specifc programming languages, e.g. PHP and JavaScript. I highly recommend these collections!
Great quotes from Clean Code
The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. Comments are always failures. We must have them because we cannot always figure out how to express ourselves without them, but their use is not a cause for celebration…. Every time you express yourself in code, you should pat yourself on the back. Every time you write a comment, you should grimace and feel the failure of your ability of expression.
WIP
- Coding Posts that influenced me: In general: Blog posts by Jeff Atwood on codinghorror.com. Also: Posts by Joel Spolsky
- Dependency Injection explained: http://www.jamesshore.com/v2/blog/2006/dependency-injection-demystified
- You are in control
- My Favorite Shortcuts for VS Code
- Customizing my shell: From bash to zsh to fish
- JSON5: JSON with comments (and more!)
- jq: Analyzing JSON data on the command line
- Get Total Directory Size via Command Line
- Changing DNS server for Huawei LTE router
- Notes on Job & Career Development
- Adding full-text search to a static site (= no backend needed)
- Generating a random string on Linux & macOS