Published on 3/25/2022 by
NevuloWhen reviewers or other people are reading your code, something they’ll inherently feel is the quality of your code.
“Good” quality code can be understood easily, reasoned with, while remaining neat. “Bad” quality code can often be hard to read, difficult to understand, and can result in bad performance or bugs, affecting end users.
Defining “quality” in code is fairly subjective, but there are some key metrics solidified around the community to identify the overall quality/health of your code.
You should generally strive for two main goals when writing high-quality code:
Apart from making the code functional and understandable, high-quality code also thinks about maintainability and testability, not just for now, but also considering how things might change 6 or 12 months down the line.
Readable code means that the code can be followed in a logical series of steps, while accurately communicating intent.
These are some key points to readable code (not a definitive guide):
Concise, to the point
Keep your code simple and don’t repeat yourself; you can often reuse existing functions in your codebase, or make generic functions that can be used in different situations.
Functions are responsible for one thing – avoiding side effects Try to follow the
- meaning functions/modules should have one purpose in providing functionality to the core program. Furthermore,Formatted correctly The code should have correct, consistent indentation. Try to include gaps between “sections” of the code, like grouping imports, or defining variables.
Documented where necessary
Your code is providing an explanation to how some software is working, so if something is confusing, it is good practice to leave notes and write documentation. Leave comments explaining why you implemented some code, the code itself is “how”.
Meaningful and intentional naming of variables and functions
Names of variables and functions can give a lot of value to reviewers of your code. Even things like user
and users
for differentiating between a single object and a series of objects.
Example of unreadable code:
1let p = SECRET_PASSWORD2if (i.sts === 'approved') {3let b = true4let dc = 11005 if (psw != p) {6 let r = 'wrong password'7 let c = 11238 let msg = 'Denied: '9 console.log(msg + r + ", err code:", c)10 return11 } else if (psw == p) {12let dc = 110013 let rmsg = 'You\'re in'14 console.log('' + rmsg + ':', dc)15 return16 }17 return18} else {19let c = 1000console.log('Unapproved, code:', c)20return}
The same program as above, but more readable:
1// Each message returned to the user has a unique code2let messageCodes = {3 WRONG_PASSWORD: 1123,4 CORRECT_PASSWORD: 1100,5 UNAPPROVED: 1000,6};78// Unapproved users are not authorized9if (user.status !== "approved")10 return console.log(`Unapproved, code: ${messageCodes.UNAPPROVED}`);1112// Input password must equal secret password13if (password !== SECRET_PASSWORD)14 return console.log(15 `Denied: wrong password, err code: ${messageCodes.WRONG_PASSWORD}}`16 );1718return console.log(`You\'re in: ${messageCodes.CORRECT_PASSWORD}`);
One key difference between the “unreadable” vs “readable” code is the intent conveyed to the reader.
All they have to work with is what you’ve written, so if it’s just let c = 1000
and if (psw != p)
; what does it all mean? How does it all come together to produce a functional result?
Striving to produce correct, readable and understandable code ultimately benefits everybody, and should be a goal for all developers, for two main reasons:
At the end of the day, most – if not all the code you write will be used by other people at some point, and those are the people we’re writing the code for in the first place. They’re the foundation for our apps, games and other software.
It’s important they have a good experience, or they’ll just move elsewhere due to frustration. You’d never want to see someone struggling to use something you’ve made!
Users won’t see the written code, but if they’re having a fundamentally poor or broken experience, it might be time to start reworking things.
If your code is open-source, or you work in a team, it is good etiquette and productive if your code can be easily read and understood, rather than unorganised or non-functional.
Ensuring that everybody on the team is following a consistent coding standard increases productivity, rather than fiddling around with configuration.
More importantly, making your code easier to read and understand for other developers you work with takes away time and mental load, following the instructions in a logical series of steps is helpful, particularly when attempting to fix a bug.
We’re all human, and it’s natural that in 6 months time, you’ll probably be thinking “did I really write this code?”
Actively identify areas of improvement for yourself, so you can continually & iteratively improve the quality of your code.
Technical debt is essentially a culmination of small imperfections or trade-offs resulting in a working solution at the time, but causing more work later down the line that needs to be addressed at some point (often called “
”).When writing code, it’s important to set out to think about future state and implications of changes you’re implementing now, which may bite you later.
As an example; let’s say you’re building a system where users can level up, going from level 1 to level 2, and so on. You’ve hardcoded all the levels in code, up to 100.
However, this system is not scalable, and users will eventually get to level 100. If anyone goes past level 100, the system breaks. So, you need to spend time fixing it, so users can still have a good experience.
The idea is stopping future problems from the start if possible. It’s hard to predict the future, but there are common things you can do in code to help yourself down the line, and lower the risk of frequent maintenance.
As the name implies, it’s best to stop “collecting” technical debt and endeavour to address concerns proactively, keeping your code clean along the way.
Importantly, have an open mind! It’s a great habit to continually find ways to improve your code to benefit everyone.
Subscribe to the Nevuletter so you never miss new posts.
Comments
• 0
You need to be signed in to comment