How Do You Write Better Code? 12 Best Practices for Writing Clean, Beautiful Code- PART 1

How Do You Write Better Code? 12 Best Practices for Writing Clean, Beautiful Code- PART 1

Has that time come yet? Time to level up from writing code that "works... somehow" to writing clean and elegant code? Code that's literally a joy to build upon, to test and to maintain... But how do you write better code more precisely?

Are there any specific good coding habits that you need to develop? Some kind of "universal principles" to learn for improving the way you write code?

Of course there are and more than just a few!

And for this post here I've selected 12 of the most effective techniques and best practices that lead to good code. Meaning code that's:

  • conveniently simple
  • testable
  • easily maintainable
  • thoughtful
  • consistent
  • modular

So, ready to dig in?
 

1. Release Early, Release Often and You'll Start to Write Better Code

Or briefly put: shorten your release cycles!

This way:

  • your team will handle priorities much better than if you released only one time a year, for instance
  • as for you, you'll get to learn more about your code and your users
  • … as time spent on development tools, on scripts, on your build, is never a wasted time: it leads to high quality, clean and usable code
     

2. Write Pure Functions: Make Your Code Easier to Read, Easier to Test 

“Betting on” purity is one of those proven principles that lead to simple-clean code...

“But what is a pure function?”, you might ask yourself. Here's a concise definition for it:

It's a (functional) method that doesn't depend on or alter an external state. 

Usually, pure functions include:

  1. one argument
  2. one or more return values

Here's one example:

function getSumOfSquares(number1, number2) {
  return (number1 * number1) + (number2 * number2);
}

3. Enforce a Clean Separation of Concerns in Your Code 

Let's try a little exercise:

Replace the “How do I write better code” with “How do I write a better... recipe?”

Both codebases and recipes include multiple steps for the coder/cook to take, so that's where the analogy stems from.

Now, going back to recipes, you could have:

  1. a simple recipe: with each cooking step depending on all the previous ones; as a chef you'd need to go through them all for reaching your goal: cooking the given dish
  2. a slightly improved recipe: where a second cook steps in to either help you or to... complicate things a bit for you in the kitchen
  3. the best type of recipe: where multiple cooks are involved, each one free to focus on a sub-recipe, independently from the rest of the cooking team

Can you identify the 3 forms of code corresponding to each type of recipe?

  1. there's code that's like an “immutable block of lines”: for every small change that you need to make, you have to read the whole thing... a couple of times
  2. there's writing code where you hand off some of the operations to a peer; yet, this could complicate the whole process, since working on the same codebase calls for top collaboration skills and perfect coordination 
  3. and then, there's the best form of code: where you're a member of a whole team of coders working on a codebase split into multiple “classes” or “modules”, each one with its own minimal interface 

And this is what separation of concerns is all about:

Breaking code down into multiple, mostly-independent modules, each one concerned with its own piece of data or single operation.

The main benefit of applying this clean code standard?

If one of you, coders of the same team, need to make a change to it, later on, you can focus your attention on that specific small module that needs to be modified. Instead of perusing the entire program to modify just a small part of it.
 

4. Learn to Collaborate: Work Well with Your Peers on a Shared Codebase

Striving to improve the way you write code is a... form of professional courtesy.

That's right, since, unless you write the entire code yourself, you'll be writing it for:

  • all those open-source maintainers that you'll be sharing it with
  • all the other coders in your team, working on the same codebase
  • the “future you”, who might someday need to go through the code that you're writing now 

In other words: you don't strive to write better code for you (only), but for all the above-mentioned persons. 

So, it makes no difference if you are 100% certain of the way that code should look in the end:

It all comes down to reaching an agreement with your peers that it should, indeed, work that way!

Therefore, needless to add that sharpening and constantly improving your collaboration skills is one of the best habits that you could develop:

  • try pair programming
  • do code reviews as often as possible
  • be patient with your pair, even when he/she's about to make a mistake (a mistake is always a great learning opportunity for both of you)
     

5. Avoid Global Variables at All Costs

Here's a common scenario where using a global variable is the most tempting solution to go with:

You're putting together your app's login form and you realize that the user's username will have to get displayed in a whole lot of places. What do you do then? You create a global variable for it. This way, it will easily show up on all those places app-wide.

And then: trouble strikes! 

You need to track down a username-related bug. And, once you type “username”, there are thousands of results that pop up.

Pretty nasty, isn't it? There still are ways to refine your searches, but your debugging process will, nevertheless, take longer than expected.

The best coding practice for preventing this bad scenario from becoming reality? 

Always put the “username” inside a container that gets passed on as an argument to precisely those methods/classes that call for it.

This way, debugging will go way smoother.
 

6. Read the Best Books on Writing Clean Code 

And there are books on writing code and great books on writing and reading code.

Let me list just some of those that “lightened” my own path to writing clean, “beautiful” code:

  • Practices of an Agile Developer 
  • Refactoring
  • Clean Code
  • Pragmatic Programmer
  • Implementation Patterns
  • The Art of Agile

From “high cohesion”, to “simple design”, to “low coupling”, these books will shed light upon the key terminology that you need to fully understand in order to write better code.

END of Part 1! Stay tuned, for I have 6 more universal principles leading to clean and elegant code to share with you in the second half of this post...