ready to build your app

5 Ways How to Code Better

After a few years of coding, many developers find themselves facing a seemingly insurmountable wall. Overcoming this wall is what separates intermediate developers from proficient coders who build real applications, but many end up hitting their head against the wall with no hope of ever reaching its other side.

If you feel that you’re ready to start building real applications, you must learn how to write code that’s maintainable, reusable, and testable. Depending on how talented you are, this might take you anywhere from months to years to decades, but our 5 tips on how to code better can help you improve your code right now.

1. Make Your Code Readable

Writing new code is difficult, but trying to understand someone else’s poorly written code can be downright nightmarish. Over the years, developers have come up with certain coding conventions, many of which have been codified by standards like Zend Framework Coding Standard, PSR-1 & PSR-2, or Google’s Style Guides.

Coding standards cover everything from proper indentation to the use of white space to naming conventions to declarations and more. Their purpose is to help developers write readable code that’s well-organized and easy to maintain.

For example, they often prohibit the use of undescriptive names for variables and stress the importance of using meaningful names like $tempStatus or $myHeaderDiv.

Making your code readable ensures high quality.

For code to be readable, it must also be properly commented. Writing useful comments is a lot more difficult than it may seem. Developers tend to make all sorts of assumptions, and they often expect others to know things about their code they can’t possibly know.

On the other hand, developers often comment only the most obscure parts of their code because they fear that others would mock them for stating the obvious.

While it takes a lot of time, patience, and effort to learn the art of commenting, it’s an essential skill that every developer must acquire to code better and join the masters of the craft.

2. Avoid Global Variables

Let’s say you’re building a simple app with a login screen and realize that you need to display the user’s username in several different places. You can approach this problem from multiple angles, but creating a global variable is arguably the path of the least resistance.

The problem is that global variables are ticking time bombs that can explode with a single mistake and bring your entire project down for a very long time.

Because global variables are by their very nature available everywhere, you may end up using a global variable when you think you are using a local variable, or vice versa. Global variables also significantly complicate testing, and they make code difficult to understand.

Of course, there are cases where global variables have a legitimate use, such as when writing a simple plugin or when working with a language that provides minimal support for local variables, but those are exceptions. In general, you should avoid using global variables as much as possible if you want to code better and be praised by others for your good habits.

3. Read Others’ Code to Code Better

Coding is ultimately about solving challenges, and there is seldom only one way how to solve a particular challenge. In most cases, the same problem can be approached from a number of different angles, and it’s often difficult to decide which of the possible solutions is the best.

If you code for a living, you probably use the first solution that comes to mind and move on because you don’t have enough time to come up with something better. That’s why it’s so important to study the code of others in your spare time.

Reading others' code is a good way to learn to code better.

When you read others’ code, you can see how someone else has solved a problem that you would likely approach from a completely different angle. Next time you encounter a similar problem in the wild, the chances are that you will remember that there’s another possible solution, one that may be better than your own.

However, it’s important to remember that, while studying others’ code is a great way how to code better, copying code that you haven’t written isn’t. In fact, blindly accepting the solutions of others can stunt your growth as a developer, which is the last thing you want.

4. Embrace Code Refactoring

Most writers hate reading their own work, and most developers hate rewriting what works. Although the process of code refactoring, or restructuring existing computer code without changing its external behavior is not exactly sexy, it’s more than necessary.

“By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you’ll find that it is easier to extend and maintain code,” explains Joshua Kerievsky, the author of Refactoring to Patterns.

Code refactoring is a huge topic, but its most essential rule states that you must always try to break long functions and methods into smaller units. If your functions are longer than 25 lines, there’s a good chance that you can split them into two or even three small functions and thus improve the readability of your code.

5. Take Advantage of Version Control Software

Back in the old days when JavaScript was considered a toy language, tracking changes in computer files and coordinating work on those files among multiple people were two very difficult tasks. Fortunately, things have changed dramatically since then, and modern version control software makes it effortless to keep track of changes in any set of files.

From Git to Fossil to Mercurial, there are many excellent version control software solutions available today, and all developers who want to code better should use them to improve their workflow. This detailed comparison of version control software is a great place to start.

Conclusion

Good code is a pleasure to maintain and reuse, while bad code complicates things and gives its author a bad name. If you want to code better and become a seasoned developer who’s admired by others, you have a long road ahead of you, but our 5 simple tips are a great place way to start.

Matt Warcholinski

Matt Warcholinski is the COO of Brainhub (a software house building awesome node.js web and mobile apps) who loves to build startups and play guitar.

Follow Me: