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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function foo(arg1, arg2) {
	if (isValid(arg1)) {
		// ...some code...
		
		if (isValid(arg2)) {
			// ...
			
			if (isPositive(arg1) && isPositive(arg2)) {
				// ...
			} else {
				// some error handling
			}
		} else {
			// some error handling
		}
	} else {
		// some error handling
	}
}

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
function foo(arg1, arg2) {
	if (!isValid(arg1)) {
		// some error handling
		return;
	}
		
	// ...some code...
		
	if (!isValid(arg2)) {
		// some error handling
		return;
	}
	
	// ...
	
	if (!isPositive(arg1) || !isPositive(arg2)) {
		// some error handling
		return;
	}
	
	// ...
}

More infos

Comments: Code should explain the what, comments the why

TODOs

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