5 Ways to Instantly Improve Your Coding Style & Skills
After the first couple of years of coding, many software engineers find themselves facing a wall seemingly impossible to overcome. This wall often has the form of a codebase polluted so much by all kinds of hacks and third-party plugins that the product seems to have no future in terms of development.
Overcoming this problem is what separates intermediate developers from proficient coders who build successful applications. Sadly, many programmers end up hitting their heads against that wall with no hope of ever reaching its other side.
If you feel that you’re ready to face the challenge, you’ll need to learn how to write code that’s maintainable, reusable, and testable. Depending on your talent it might take you months, years, or decades. But don’t despair, there are at least 5 tips that can help you improve your code instantly.
1. Make Your Code Readable
Writing your own, new code is usually fun, you know what to do and you do your best. However, trying to understand someone else’s poorly written code can be downright nightmarish. What people often forget is that code is more often read than written. That’s why over the years, software engineers have come up with coding conventions, the majority of which have been codified by standards like Zend Framework Coding Standard, PSR-1 & PSR-2, or Google’s Style Guides.
Coding standards define everything, from indentations to the use of white space to naming conventions to declarations and more. Their purpose is to help engineers create readable code that’s well-organized and easy to maintain.
For example, they very often prohibit the use of vague names for variables and emphasize the importance of using meaningful names like $tempStatus or $myHeaderDiv.
To make the code even more readable, you might consider adding some comments. Writing helpful comments is 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 at the first glance.
While it takes a lot of time, patience, and effort to learn the art of commenting, it’s an important skill that every developer should possess.
2. Avoid Global Variables
Imagine you’re building a simple app with a login page and realize that you need to display the username in several places. You can approach the problem from a few different 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 an entire feature down for a very long time.
Because global variables are everywhere, you may end up using a global variable when you think you are using a local variable or the other way around. Global variables also significantly complicate testing, and they make the code difficult to understand.
Of course, there are cases where it’s reasonable to use global variables, for example, when writing an uncomplicated plugin or when working with a language providing 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 make your code more readable for your teammates.
3. Read Others’ Code
Coding is about solving challenges, and there is rarely only one way to solve a particular challenge. In many cases, the same issue can be approached from a number of angles, and it’s often difficult to decide which of the possible solutions will be the best.
If you write 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.
When you read others’ code, you can see how the author solved a problem which you would likely approach differently. Next time you encounter a similar problem, the chances are you will recall 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 to code better, copying code that you haven’t written isn’t. In fact, blindly applying the solutions that worked for others can slow down your personal development, which is the last thing you want.
4. Embrace Code Refactoring
Most writers hate reading their own work, and most clients hate hearing about rewriting what works. Although the process of code refactoring or restructuring existing 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 deep topic, but its most essential rule states that you should always try to break long functions and methods into smaller units. If your functions are longer than twenty-five lines, there’s a good chance you can divide them into two or even three small functions and improve the readability of your code.
5. Take Advantage of Version Control Software
From Git to Fossil to Mercurial, there are plenty of great version control tools available today, and all developers can take advantage of them. This detailed comparison of version control software is a great place to start.
While good code is a pleasure to work on, maintain and reuse, poorly-written code complicates the development process 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 these 5 simple tips are a great place to start.