I hear a lot of times that you should not abbreviate a variable or use bad variable names, and that you should specify what your variable is being used for. However I see a lot of tutorials and people use _rb2d. Is there some kind of rule or something that makes it okay to abbreviate.
I try to keep my code for personal reasons as clear as it can get.
I suppose in theory the smaller the length of the variable identifier will process faster. However such an optimization would never be entirely suitable to implement while the game is receiving active development. Nothing is worse than working with confusing code that you have completely forgotten how it worked.
A project can be of mammoth proportion so itâs nice to be able to dip back into areas and know that a variable was as clear as day, this is the CURRENT_CELL instead of the cc.
Saying that I am guilty in forloops of abbreviating various datas. Ones that are created perhaps only one or two lines before entry, that I had not specified a private for. I do not work with team of colleague so I would not know about practice when sharing a project. But from personal standards all my major specifications are always clear to read and remember.
And I do this in part as one day I plan to sell things to other programmers, learners etc⌠and when I do I want them to read that code like itâs a nursery rhyme.
Thereâs rules (what the compiler will force you to comply with) and thereâs suggestions and guidelines like the abbreviation advice you gave.
The key is consistency. If you always use the same abbreviation, itâs not necessarily bad to make it a little shorter. But itâs really easy to be inconsistent with naming when youâre tempted to cut corners. A little _rigid here, a little _body there, a little âridgedâ accidentally there, and you have a real mess, trying not to make redundant or overlapping variables and hitting the dreaded ânot defined in this contextâ error messages.
If itâs too short, it makes it hard to search for it, too. Search for ârbâ and you find âperturbâ, âturboâ, and âoverbalance.â
If itâs too long, itâs just a little hassle to type. But this isnât 1975 on a teletype terminal, you have modern editors which can help you type longer identifiers that are more clear and consistent.
It is ok to abbreviate when itâs something youâll be looking 2 years in the future and youâll be like âright, of course, this is X, itâs abbreviated for brevity, and Iâm totally not confused rnâ
If you canât imagine being able to do that, do not abbreviate. Also make sure your names arenât too long and generic either. Thatâs actually way worse than abbreviating too much, because of how the brain typically shuts down when confronted with too much information. At least when youâre abbreviating the code remains compact, everything fits the screen, and your brain will be more attentive to details. Not so when the names are huge.
The correct answer is empirical for how your cognition works. Also lean onto some convenient standards for communication.
Common cases where we do abbreviate or simply use short one letter variables are for local variables for primitive types or for-loop variables. When you have a loop like this:
foreach(var u in users)
{
}
Itâs clear that âuâ is a âuserâ (whatever that may be, probably a class). A more descriptive name would not really help much here. Here the declaration is close to the usage and limited to that small section.
Member variables should be more descriptive since they are used at various places. As others have said, there are no hard rules.