Is there a convention for naming variables?
When one has to name variables in different contexts, is there a prevailing convention to prefix the variable with a notation indicating where it belongs?
For example, variable contents are passed between Javascript, Local Storage, HTML(forms) and server side scripts. I got into the habit, possibly a bad habit, of making the variable name slightly different for fear of any conflict when there may be no conflict possible. If not and if useful, is it an idea to copy the "hungarian notation", which prefixes a variable name with "s" for string, "i" for integer etc and starting the variable name with a capital letter? In my simplistic reasoning, the variable name can be prefixed with "js" or "lc" or "ht" etc (perhaps already applied in "good programming"). Thank you for your help. |
Nothing 100% universal. Hungarian notation is/was one attempt.
What organizations do is to agree upon, document, and use their own coding conventions, which may or may not be in agreement with proposed standards. Same for you, decide what you feel is a good choice, use it uniformly, and modify as needed. |
Off: Hungarian notation has a more meaningful version, described by Joel Spolsky:
https://www.joelonsoftware.com/2005/...de-look-wrong/ TL;DR: such prefix is useful if similar but not identical objects have to be handled in parallel, then the prefix emphasizes the difference, e.g. mileDistance vs kmphSpeed, celsiusWaterBoiling and fahrenheitIceMelding or pixelScreenWidth and mmScreenHeight |
Don’t use Hungarian notation if you’re using a dynamically typed language.
|
I simply suggest that your team, and maybe the higher-level organization, should reach a consensus as to what all of you will choose to do, so that, when anyone in the future reads your source-code, it all looks more or less the same. The future reader, being aware of your consensus, will use it to help him or her understand what you have done.
Personally, when I am passing "variables" between clients and servers, or caching them in storage, I strongly prefer to package them into structures. This gives the reader an immediate clue as to what the variable "belongs to," and it also makes it much less likely that you will be vexed a by "simple typo" that – just as in the current sentence – you might have innocently overlooked because your eyes "knew" what to expect. We have all been extremely annoyed by a bug that we couldn't see when we were in fact looking directly at it. |
All times are GMT -5. The time now is 05:09 AM. |