Great programming is a balancing act between writing good, readable and organized code. Good code is fast, efficient and memory conscious. Good code is also very easy to read and to understand. But also the way the code is organized is of the utmost importance. It’s a balancing act because sometimes fast and ugly code needs to give way to readable code. A single linear function that’s optimized to the nines may be super efficient. But it’s also a maintenance nightmare.
Code is usually organized in layers, modules and functions to make maintenance simple. Once the architecture is clearly understood then each function should be easily readable. And only when it’s readable can it then be optimized. Coding in the reverse order will lead to confusion and loss of productivity. Plus the next programmer will thank you. Or at the very least, not come knocking at your door with some choice words.
This includes organizing your application into layers, modules and functions. In most programming languages that also includes organizing code into files. And in the case of OOP languages, their own classes. Follow standard development practices such as loose coupling. Single responsibility. Use recognizable design patterns.
Business code and boilerplate code should be kept separate as much as possible. Extract boilerplate code into methods when the isolation of the logic makes sense. Use descriptive language when naming methods that isolate business logic. Also use client provided terminology as much as possible so that everyone gets along.
Boilerplate code is the same code that gets written all the time. To validate data structures and variables. It may set up new variables or constants. It even handles errors and exceptions when things go wrong. It’s the code that makes sure the business code works properly.
Business code, on the other hand, acts on data. From the input, it generates the output based on business rules provided by the client. Business rules do the math when needed. It determines what happens when a customer is older than 18 years old. Or if a value is null versus an empty string.
Performance is a combination of using the right libraries and proper control statements. But also being smart about how best to solve a problem. Should you use a switch statement. Multiple if-else statements. Or perhaps configure a hash map. All while watching how much memory gets eaten up.
The JIT is a marvellous wonder at optimizing code at runtime. It will inline your code if and when possible. It’ll optimize your conditional statements. Unroll your loops. It’ll even compile your code to binary if the need arises. So at the outset assume that performance is handled by the JIT. But also keep in mind that it will only do so after it’s gathered enough data points to make a decision.
Even though you should never prematurely optimize code. The boilerplate code you write every day should already be optimized in your head. Write your conditions so that they short circuit. The most likely if-else condition to occur should be coded in order of most likely to happen first.
It important to always keep these three rules in mind when coding. Your co-developers with thank you. And so will the guy doing the code reviews.