Forum Moderators: coopster

Message Too Old, No Replies

What variable names are better?

Ist there any reason to use different types of variables to improve speed?

         

AcsCh

9:43 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



Is there any difference in type of variable used, or is that only a question of personal preference?

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?

NomikOS

10:52 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



While follow the PHP rules I think that is not matter.
see: [php.net...]

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.

directrix

11:35 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



I wouldn't worry about performance differences between long and short variable names -- they will be dwarfed by other factors, such as your choice of algorithm, and database design.

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.

NomikOS

1:12 am on Nov 5, 2005 (gmt 0)

10+ Year Member



Yes: $i_like_this_style_of_variable_as_it_is_clear_whats_about
is more readable but my problem with this style is when a need search a var through an editor. Was $the_variable_pi or $the_variablepi? because $a_b_c is more easy write it like $abc.

Inside a function I write $_var for mantain value of $var intact.

directrix

1:24 am on Nov 5, 2005 (gmt 0)

10+ Year Member



You need a good editor, such as UltraEdit, that can do a regular expression search for $the*variable*pi!

AcsCh

2:35 am on Nov 5, 2005 (gmt 0)

10+ Year Member



Thanks a lot for your insight! I'm usually using as directrix pricipally $ThisVariable, but keeping it as short as posible. I've just read once where some SW guru did not like this style, so I got a bit unsure if I've missed some fundamental thing.

NomikOS

3:02 am on Nov 5, 2005 (gmt 0)

10+ Year Member



I use editplus, for me is the best, powerful, light, all-terrain and extremily customizable. Too do regular expression searches (in various files very rapidly), but this is not the point, right?
Any way the better is feel comfortable with the own style and concentrate in logic.-

Anyango

5:44 am on Nov 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As others have said, By Short and Long names of variables, there is No difference on performance of your code, and it's algo and functions that can effect performance.

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?"

larryhatch

7:35 am on Nov 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a long time programmer, I split the difference.
Numberofwidgetssoldtoday = 12 looks terrible to me.
WG = 12 is unmaintainably vague without a comment line right before it.
WidgSold = 12 is a nice compromise, but I still believe in inline comments. -Larry

grandpa

8:15 am on Nov 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A little OT..
I create a comment section at the top of my scripts. It contains useful info about the script, and could also be a place to note anything about variable names. Script modifications are recorded in the comments section. Then inline comments mark the beginning of a change in the script.

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.