Coding Awareness. Do you Have it?

Henrique Mota
Full-Stack tips
Published in
5 min readJun 18, 2018

If you already read some of my articles, you know that I like readable code. In this article I will present some techniques, and actions that can improve your code dramatically. Believe me, following some techniques, and rules, will allow you to be one year for now, three levels above.

But first let me describe a little bit code awareness. So you are sited at your chair in front of your computer and you are doing a task for your company. You start writing your code, you do the first line and you notice what a piece of shitty code. In a first stage this is coding awareness.

Well you get the point, but off course is not as simple as that. There are a lot of aspects concerning coding awareness.

Next we are going to explore those techniques and actions to put you on a good path.

Design and Architecture

Every time you develop a micro-service or even a monolith, design and architectural decisions must be made. This decisions have a profound impact in your project in terms of maintainability, readability and independence from third part libraries.

Imagine now that you are starting your tech startup project and you don’t divide everything into layers. Like happened to me in the past, imagine that you are programming and decide to put everything into a file.

Yes I already been there. Your project grows you gain some clients and you decide to hire some developers to help you out. How the hell are they gonna understand and maintain the project? What a pain.

Imagine that you must use a database to store and query information. There are a lot of well tested libraries out there in most of languages to interact with most known databases. We are not going to reinvent the wheel so we are going to use one of that libraries.

Now imagine that the your code becomes dependent of that library, and later that library becomes deprecated. You’re screwed as hell!

Design and architecture must be taken seriously. If you want to know more I recommend you this book:

Syntactic and Semantics

Writing code can be compared to writing text, so we must be aware of some syntactic and semantic rules. When you become a experienced programmer, practicing good code over and over this will feel a natural thing.

Until then you must monitor yourself while coding. Choose variable names, function names, built-in methods to give meaning to your code. Aspire to be a reference in your team. There is nothing more rewarding to a programmer than hearing from a colleague: “Hey what a job, your code is beautiful!”.

I want to give you a example of how semantic can change drastically the readability of your code.

Let’s imagine that we want to build a leaderboard of the most valuable players in the World Cup. We have 3 endpoints to an api:

  • Get all the team participating
  • Get the players
  • Get the goals and passes to goal

Our criteria like you may noticed will be the sum of goals scored and passes to goal effectuated.

You have two options. The first one is to put all inside a function called for example getLeaderBoard(). Imagine the code confusion with all mixed up inside this function, 3 fetch api calls, calculate the score and order the players, what a mess.

The second option is my favourite one.

function getLeaderBoard() { 
let teams = getTeams();
let players = getPlayers(teams);
let scores = getScore(players);
return scores.sort((firstPlayer, secondPlayer) => {
firstPlayer.score - secondPlayer.score;
});
}

Notice how the semantics and syntax help you to read this functions. Plus everything is isolated in other functions. Easier to maintain and easier to test.

If you want to read more about clean code:

Write Tests, if Possible Embrace Test Driven Development

Tests are often connoted as a waste of time, but they are a time saver. Imagine that you do an awesome system that makes you earn a lot of money. You detect a flaw on it and now you want to change it.

But there is a problem there aren’t any tests in your codebase, what a waste of time.

Writing tests allows you to gain more confidence in your code, it’s the ultimate tool to enslave your code and do whatever you want with it. But there is another benefit in tests, they allow you to use your own code as a third party library.

You will anticipate the pain of your colleagues using what you just developed. Then you are in time to improve and be admired by your colleagues, by the good code you produce.

TDD is the last thing I want to talk about in this section. Test Driven Development, it’s a way of coding where you first develop the tests and see them fail in the first execution.

Now the advantage of TDD is in this point, after you develop the test you develop the minimum code to make the tests pass. And you iterate over the two parts until you reach your solution with a minimal amount of code.

Less code, less problems. If you want to know more about this topic read the book:

Review Your Code, Review the Others Code and do it Again and Again

This last topic is very important. This is one of the most fast ways to improve your coding awareness. First ask if you can among your team to make code reviews. Then every time you finish some task, review your own code.

By reviewing the others code, you can exchange ideas with the authors of the code and absorb good practices.

By reviewing your code, you can improve the quality before it gets reviewed by another person.

Hope you enjoyed this article,

Stupid Gopher

--

--