Stefan Wöhrer, view all articles
10.05.2019
7 min read

3 Essential Tips That Make Your Source Code Shine

Short summary:

Take your source code to the next level with 3 simple yet powerful tricks

In this article, you will discover 3 essential tips that will help your source code shine. There are some unwritten rules that every good programmer follows. Even if you're on a tight schedule and trying to crank out your code quickly: You still need to adhere to these rules.

Even if you're an experienced programmer!

I’m not talking about algorithms, data structures, software architecture, or design. I’m referring to something much more fundamental, much more vital: Readability

Source code needs to be carefully crafted. Always. There’s no such thing as “quickly hacking around.” If you try to speed up by skipping these basic rules, you’ll always end up slower in the end.

See, source code is written once, but it is read many times. Optimizing code for readability is therefore crucial. I present to you 3 vital rules you must follow to produce highly-readable code. Adhering to these rules helps you and everyone else working with your code to maintain, extend, and adapt it. Plus, readable code is far less error-prone.

The best part about these 3 rules is: You can implement them right away. No training time needed. You’ll be a better programmer in just minutes!

Let’s get started.

Use meaningful variable and function names

A line of code says more than 1000 words. Well, sometimes not. Using self-explanatory variable and function names makes your code much easier to read, understand, maintain, and extend. Here’s an example:

Of course, when writing that piece of code, I knew exactly what formula I was implementing. Do you know as well? Maybe yes, maybe no. I could have added a comment to describe the purpose of that formula, but a much better way is to just use descriptive variable names like this:

Wow, now it’s crystal clear. Just by using descriptive variable names, the reader can immediately see that this code converts Fahrenheit to Celsius. Now let’s put that code into a function and observe the readability based on function names and parameter names.

Compare the “short” version:

with the more detailed version:

I know what you’re thinking: It takes much longer to write those “long” words instead of just using single letters for variable and function names.

However, let me ask you a question: How much longer does it take to understand a piece of code written in either style? Self-explanatory code is not only a big time saver for others reading your code but even for yourself. How often have you gone back to extend or change some code you wrote 3 months ago? And how often did you think: “Gosh, what the heck did I do here?” Remember:

Code is written once, but it is read many times.

Anything you can do to optimize reading performance is worth the extra writing effort.

Use proper indentation

If you’re programming in Python, you can skip this tip and thank Guido van Rossum for his decision to make indentation mandatory in “his” programming language.

For most other popular programming languages like C/C++, Java, JavaScript, PHP, C#, etc., read carefully:

Indent your code hierarchically

I don’t care if you use tabs or spaces. (Okay, I do, but that’s for another blog post..) I don’t care if you use 3 or 4 spaces. I just want you to understand that indentation is a must in programming. Let’s compare some pieces of JavaScript code. This code:

  • Converts the values from 100 to 110 from Fahrenheit to Celsius
  • Rounds the converted values to 0 decimal places (making them integers)
  • Outputs to the console all even celsius values (This is done by using the modulus operator

Compare the non-indented form:

with the indented form:

I also added some blank lines to structure the code even more. Which of those two code snippets is more readable?

Use Functions (properly)

Create reusable building blocks by using functions. These also help you structure your code and make it more readable and understandable. The most important feature of functions is that they avoid repetitive code. Remember:

Avoid Repetition!

If you see the same lines of text scattered multiple times throughout your codebase, you likely missed creating a function (or a class, or a module, etc., depending on your programming language). Let me give you an example:

The above code prints out the 4 values in degrees Fahrenheit and their converted values in degrees Celsius. We can easily see that we are dealing with code duplication here, so let’s eliminate it by creating a function that handles the conversion and printing for us:

Much better, right? The code looks “clean” and “lean.” Also, it is far more descriptive than the first example. Anyway, we still have that formula residing in the ConvertAndPrint function. What if we want to convert Fahrenheit to Celsius without printing their values? Let’s extract the formula to its own function:

Et voila! We produced a self-explanatory piece of code that consists of building blocks we can reuse in many ways in different scripts.

However, we’re not done yet.

See, we still have some repetitive code. And our PrintFahrenheitAndCelsiusfunction takes 4 values. But what if you want to print only 3 values? What if you want to print 300? There’s a better way to do this.

Depending on your programming language, there are likely several options to abstract the number of parameters of a function. What is almost always a good idea is to use a container (like an Array or a List) instead of multiple variables. See that final example:

This last change made our function far superior:

  • It is shorter and easier to read
  • It is much more generic

Conclusion

Source code is much easier to read (and thus to understand, maintain, and extend) if you follow some simple rules / styling guidelines.

Also, we’ve seen the power of abstraction: Use functions (or whatever means of abstraction your programming language offers) to create reusable building blocks.

Upgrade your variables and functions by using proper naming so that your source code becomes easy to read. Some people even claim that source code should read like a story.

Implement these 3 tips, and I promise you’ll see amazing results.

this is the end my friend

Who wrote it

Stefan Wöhrer CEO & Senior Software Architect

Stefan combines technical expertise with entrepreneurial thinking. As CEO and Senior Software Architect, he shapes systems and strategy, oversees projects from conception to implementation, and creates scalable, future-proof solutions with a focus on clear, sustainable structures.