The only principle you need to follow to become a great developer.

The only principle you need to follow to become a great developer.

·

6 min read

Intro

Being a software developer is a challenging profession, and writing code correctly is not an easy task at all. This is why experienced developers have come up with different principles to help make writing code easier. Unfortunately, the variety of different coding principles available online (such as KISS, DRY, YAGNI, SOLID, and SoC ...) can be overwhelming, especially for new developers. Understanding and applying these principles on a daily basis can be difficult.

After 15 years of working in the software development industry and with hundreds of developers across all levels of experience, I have concluded that there is one simple principle that, once developers start to follow, makes a dramatic improvement to their code quality.

The principle is:

Always write a short functions!

Simple right? You are probably saying to yourself, "I know that" but there is no way this principle will make me a better developer or can replace the other principles. But keep reading, and I will explain why following only this principle automatically makes you follow the most important principles in coding.

Prevent Code Duplication

Code duplication is undoubtedly the worst thing a developer can do in code. I often see developers are too lazy to extract existing logic into a common function and just prefer to copy-paste it. They do this because they feel there is an unlimited number of code lines to use, but if we enforce a limit to the length of the functions, developers will automatically start to think about code reuse.

For example, I see this problem all the time. A developer duplicates a few lines of existing logic as it is much easier and faster.

function existingFunction(price) {
    // some existing code to format price
    const formatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
    });
    const formatedPrice = formatter.format(price);

    // the rest of the existing function
}

function newFunction(price) {
    // sometimes developers prefer to duplicate formatting part
    const formatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
    });
    const formatedPrice = formatter.format(price);

    // some new logic here ...
}

By simply asking during the review to shorten the function, a developer will find a way to extract the common logic into a new reusable function.

// new function with common code
function formatPrice(price) {
    const formatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
    });
    return formatter.format(price);
}

function existingFunction(price) {
    // extract to new function
    const formatedPrice = formatPrice(price);

    // use the formatted price ...
}

function newFunction(price) {
    // reuse the common logic
    const formatedPrice = formatPrice(price);

    // some new logic here ...
}

Will Force Single Responsibility

To prevent complex logic in a single function, it is usually better to define a function to handle one logical operation only. If you follow the principle of writing short functions, writing a function that handles more than one responsibility will be very challenging.

For example, I reviewed a developer's code that wrote a function of around 100 lines of code that handles multiple data operations in one function.

function someFunction(api) {
    // code to read data ...

    // code to make changes to data ...

    // code to save data ...
}

Asking to shorten the function will force the developer to break the code into smaller logical parts with a single responsibility only.

function someFunction(api) {
    const data = readData(api);
    const changedData = makeChanges(data);
    saveData(api, changedData);
}

Prevent Premature Optimizations

Premature optimization is the practice of optimizing a function/system before it is necessary. This is usually a waste of time and resources and leads to unnecessary complexity, and increased risk of errors, and a focus on details that may not be important in the end.

This is a huge problem that I encounter all the time. Developers love to develop by themself complex, unnecessary code.

But by following the principle of writing short functions, we force the developers to find a shorter and simpler implementation.

For example, I reviewed a developer's code that wrote a function to iterate an array of complex objects to get some statistics. His idea was to iterate the array only once and try to keep track of the data he needed on the fly using complex hashing. The result was a long function that took him a long time to write, and that was hard to understand with unnecessary complexity.

By asking him to shorten the function (even by reducing the performance), he wrote a much simpler code by iterating the data several times instead of once, making his code much easier to understand and test.

Make The Code Testable

One of the most important aspects of writing great code is to be able to test it easily. I have worked with many teams, and the main reason why teams skip testing is that sometimes it is just impossible to do it because of the complexity of the functions in the project. Everyone would like to test their code and prevent bugs, but how do you test a function of 500 lines of code?

For example, a function like this is impossible to test.

function someImprotantnFunction(api, prices, customers, permissions) {
    // code block to make api call

    // code bock to check permissions

    // code block to calcualte prices for the customers

    // code block to update db with new prices
}

But if you follow the principle of writing short functions from the beginning, such functions won't exist in the code; instead, you will have smaller functions that are easy to test.

function makeApi(api) { ... }

function checkPermissions(permissions) { ... }

function calculatePrices(prices) { ... }

function updateDB(api) { ... }

Make the Code Much Readable

I believe everyone will agree that reading and understanding a function of 20 lines of code is much easier than a function of 500 lines of code. But still, thought my entire developing career, I kept encountering huge endless functions with bugs and complexity that no one could understand and maintain. Following the principle of writing short functions will allow other developers to better understand your code and be able to prevent bugs.

Make The Project Structure More Organized

One great side effect of always writing small functions is a creation of a better project structure.

For example, In one of the projects I worked on, we had a complex structure of modules without any logical structure. After I asked the team members always to write short functions, the developers suddenly started to think about a better structure to organize many common files in logical folders and new folders like common, utils, db, api started to appear in the project, making the entire structure much more organized.

Summary

This principle is very powerful. I encourage developers always to follow it, and I have seen great results for the code quality of developers after applying this principle in the code.

One question you probably ask yourself by now is, how much is "a short function"? The answer is simple, the shorter, the better. The number of code lines depends on the programming language, but I usually try not to exceed 25 lines of code.

Also, some of you probably say, "there is no way you can always write short functions." My answer to this will be, "Yes, you can."

I developed in all possible programming languages over the past 15 years, and It is always possible to follow this principle. Yes, it is not always an easy task and will not always solve all the problems in code, but it will always make you think about a better solution and eventually make you a better developer.