Photo by Artur Shamsutdinov on Unsplash
How to Write Better Code
An introductory guide on improving code clarity and effectiveness as programmers.
Table of contents
Have you ever been stuck trying to understand code that was written just a few days ago by you? Ever had JavaScript at run time outputting an error, precisely undefined
, yeah sucks a lot, ever had your code look like spaghetti code (literally). Well enough of the salesman pitch, this article should be a problem solver, giving you introductory Tips on how to solve these problems and hence increase your overall effectiveness as a programmer.
Writing readable code may sound like an unnecessary and demanding TASK,i mean if the code works then there is no point in fixing it. like everything new is difficult to grab at first and then later becomes a lifestyle, so is writing clean and readable codes. In writing readable code we can use all or just few of these tools and practice:
this article is specifically geared towards javascript programmers, but some of these tips can also be applicable to other code bases or tech stacks.
- Prettier.
- Eslint (for JavaScript).
- Typescript.
- Comments.
- Good Naming Conventions.
Prettier
Prettier is an opinionated code formatter, that easily integrates with your favorite programming and web languages, and best of all ,it integrates with your code editor, in my case Vscode. ever had that code that looks amateur in appearance with the opening<div>
is not aligning with the closing </div>
hence leading to a mess in your code readability, yeah i know, prettier easily solves that and more. If you are using vscode there is an extension in the marketplace by prettier which integrates smoothly into your text editor.
The benefits of prettier cannot be overemphasized, some of them are:
- Easily Maintainable code styles across various code base, that is your code will look similar regardless of the project been carried out, hence with this you could create a standard for your code, example semi colons are not necessary in JavaScript, so on save you could just get rid of it.
- Saves Time used in formatting, hence improving effectiveness, which is what every good programmer should aim at.
- Your code is formatted on save.
more of these benefits are outlined on their website
Eslint
Eslint is a linting tools used mainly to follow standards and catch tinny forgettable bugs that may cause issues mostly at runtime, the goal of this tools is to avoid bugs and improve consistent code amongst your projects. You can also add plugins to your Eslint rules or configurations and parsers to fully extend the feature of Eslint. Eslint also can be integrated into your code editor.
Some benefits of using Eslint are
- Eslint helps to reduce the bugs that are not easily noticeable or that are forgettable by the programmer.
- Since JavaScript is not s strictly typed language like C++ or java etc. Eslint serves as a guide for writing better code. also, this is best solved using typescript.
- Since Eslint has a plugin extension feature, you can use programming Rules set by bigger and more organized Tech Firms like google, Airbnb (imagine your code looks as similar to that of Airbnb).
get more information about Eslint Here
Typescript
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. sounds like a robot talking, that is because this is the official definition on their website.so far the best on the list is typescript. Typescript is safety net, and provides strict type checking for JavaScript programmers
Using typescript helps programmers generally in been effective, the best tools so far, the pros range from.
- Strict Type checking for JavaScript (which is a loosely typed language), hence reduce the chances of bugs happening at run time.
- Provides Auto complete for various types.
- Easy to grab if you already know JavaScript.
checkout this link to get started with typescript
Comments
Comments are the programmers todo list or also a help guide for the future. imagine your future self travels back to view or read your past code and does not get lost because you probably commented your code. or maybe you make a push commit to a public repo and the lead programmer has to go through your code, your comments can easily help the person navigate easily.
comments should be follow some certain rules,these rules are not a necessity, but are helpers, same as the tools above.
- Comments should not duplicate the code.
- Good comments do not excuse unclear code.
- If you can’t write a clear comment, there may be a problem with the code.
- Comments should dispel confusion, not cause it.
- Explain unidiomatic code in comments.
- Provide links to the original source of copied code.
- Include links to external references where they will be most helpful.
- Add comments when fixing bugs.
- Use comments to mark incomplete implementations.
click on this link to get further incites on how to use comments
Good Naming Conventions
Generally Programmers are known to have bad naming practices (not excluding me self). but this gladly is something that can be improved with just a little incremental change. variable, function or class naming should not be that difficult, you can use this guide, it goes in depth on how to name variables and functions in order not to cause conflict.
Thanks for reading up to this point, i would have given in to my big bed by this time, if you have any question and suggestions that would help readers please include them in the comments sections, and also we can interact on this platform or on twitter, ha ha no instagram for now.