I have reviewed more than 1,000 frontend pull requests.
I've noticed some common comments made to junior developers, which I also got when starting out. Dealing with these comments often makes the review process longer.
So, if you're a junior developer just starting:
Here are 5 simple tips to make your code more readable.
1. Save meaningful or shared values as constants.ย If a value is significant in your code or used in multiple places, store it in a constant variable in ALL UPPERCASE. This helps give the variable context and makes it easier to locate or modify.
2. Include the unit in variable names when relevant.ย You saved your meaningful variables in constants. Great ๐. Now, make sure to specify the unit if applicable. For example, instead of naming a variable DEBOUNCE_TIME
, use DEBOUNCE_TIME_MS
to make it more straightforward.
3. Use the JavaScript numeric separator when possible.ย Improve the readability of numbers like 1000
or 20000
by writing them as 1_000
or 20_000
.
4. Return as early as possible in functions.ย Whenever feasible, exit a function early to enhance the code clarity. This will drastically increase the code readability.
5. Avoid negative conditionals. Instead of naming a variable isWebsiteNotAccessible, name it isWebsiteAccessible and use !isWebsiteAccessible. Positive conditions are generally easier to understand.
Thank you for reading this post ๐.
Leave a comment ๐ฉ to share a tip.
And Don't forget to Drop a "๐๐ฆ๐ฅ".
If you like articles like this, join my FREE newsletter, FrontendJoy, or find me on X/Twitter.