Forum Moderators: coopster
What would be better?
$ILikeThisStyleOfVariableAsItIsClearWhatsAbout
$u
$Variable['Variable1']
is there a difference how much resources are used to execute this script, or is this a totally negligible factor?
I like $ILikeThisStyleOfVariableAsItIsClearWhatsAbout style and I ues it.
Respect to computer resources used I think than any char (1 byte) count to the memory overhead.
But in another hand legibility is transcendental to understand what a program do. Never I heard about variable names lenght economy contrary to convenience of extensive comments to the code (even though are not processed this characters in any way must be omitted).
Yes, sometime I thought about this, but now I'm worry more in to economy code through good logic.
One problem with long variable names is that some people (I'm one!) like to inaudibly pronounce them, as well as reading them, and a long name slows you down! It sounds trivial, but I have worked with programs where it was seriously irritating to have lots of 7 or 8 word variable names.
Even with long names, I find $i_like_this_style_of_variable_as_it_is_clear_whats_about more readable than $ILikeThisStyleOfVariableAsItIsClearWhatsAbout, but again that's just a personal preference.
I tend to use medium-sized variable names -- 5 to 20 chars -- except in a couple of situations:
(i) index variables: $i is fine if it is only ever used as an index; and
(ii) in a function with only a few lines, where again a short variable name may be acceptable, as there's no danger of a clash with a global name.
Inside a function I write $_var for mantain value of $var intact.
Variable names should be Yes understandable and could be such that their Name would simply explain what they are for but i feel for our work to be called "Programming" and not just "Coding", our code should be such that if one day some other programmer reads it (definitely with your consent) , he can understand what you have written.
For that, SW gurus usually try to ask their juniors to make variable names with some kinda stard naming convention, that convention can definitely be different by two programmers but, i would say, once you decide that yes this is how i would name my variables then throughout your code of that project, you should use the same style of naming variables, Again, that has no edge over performance, but sometimes performance is not the only thing one is looking for, you dont want read your code after 6 months and say "hmm, i think i have seen this code, who wrote it? ." Do u? ;)
A pretty easy naming convention would be
1) First letter of variable name should be small and contain first alphabet of the name of variable type you are using. i.e if you want to create a variable of type string you should use "s":
string : s
integer: i
float: f
long : l
and so on
2) After that first alphabet, you should have your variable name as you want, However Typing style should be that, each time there is a new "word" , it's first alphabet should be a capital letter.
for example
1) you want to define a string for storing "Employee Name".
Based on above style , your string could have this name
$sEmployeeName
2) You want to create a Temporary Variable for storing a temporary integer value you could have
$iTempVariable
3) Another rule could be that all the CONSTANTS should have ALL CAPS name and better start with an underscore.
And on and so on.
Although it is not important at all that you follow someone's pre-defined naming techniques but even if you make your own, important thing is to keep the same naming technique throughout the code of same application. And again, its only Helpfull to Human not
to Computer, Computer has no effect good or bad by different variable names.
Regards,
Kami
P.S: "All Code Constants are Variables, Funny na?"
About the only standard for variable names that I've applied is to try to use _CAPS for certain variables. Even that isn't consistent. I pity the poor fool who has to rewrite my scripts.