How to name variables?

Mohit Rakhade
2 min readJan 31, 2023

--

It's essential that you should follow proper naming conventions when you code to be a good developer.

It won’t change the performance of the code for sure, but the time and effort ( forget about your peers ) you will require to make changes in code or debug your code will keep you awake for some nights.

thanks DALL E2 for image

note: whatever writer has written above is based on true events, nothing is fiction. don’t take the advice casually. thanks for reading note :)

Forget x, y, and i, j’s like they are your ex

leave them when you are actually coding on the project, you can use them when you are wasting your time doing competitive programming.

Don’t get into the habit of giving your variables short names that mean nothing. Don’t name variables x or y.

Give them descriptive names like numberOfDepositsLeft and costOfItem.

Let’s optimize this also, what if you use itemCost instead of costOfItem this is better.

And for boolean type variables, ask a question on the state of things like -

IsActive, IsPaid, IsFloating, IsDeletable, etc.

non-compulsory tip

here is one non-compulsory tip, it’s called Hungarian notation. some people like some don’t. Add an identifier of the type of variable as a prefix.

E.g. iLives (for integers), fTokens (for floats), bIsAlive (for booleans), etc. To check the type faster than hovering on it in your IDE.

And most people avoids it for good reason. If you spend that much time wondering what types you’re dealing with, your design probably sucks. It’s hard for me to recall ever spending so much time figuring that out that I thought I should change anything.

you can use this non-compulsory tip depending on the project structure you will be working on.

You can comment, code doesn’t get Offended by a Comment

try to add comments to your code that explain things a little because when you come back to the code at a later date you won't understand that mess. Comments are best served to tell you why you did something rather than what you did.

The comments have to be very meaningful in my opinion and not be used to replace bad coding.

If you are following the Design and Development Principles of KISS, SOLID YAGNI, etc in building your projects, a lot of things you do would be very self-explanatory. For example, if you building functions, they should be self-explanatory with descriptive names and descriptive variable names, do only one thing at a time.

Do check my Twitter 🐦

Thanks for your time, hope you enjoyed this ❤️

--

--

Mohit Rakhade

Diving deep into Decentralized world of Blockchian to achieve transparency, enhanced security, Increased Efficiency & Improved Traceability ✨