I wrote a lot of code for Autohotkey in the past days and I keep asking myself something about variable names:
Should I better keep them really short so that my code remains clear, neat and tidy or should I put up with long names in favor of them being self-explanatory?
Examples:
ms1 vs. mouse_speed1/mousespeed1
kd2_max vs. keydelay1_max
cpos12_X vs. click_pos12_X
sarea2_X_min vs. search_area2_X_min
Until now, I used the long versions most of the time, what would you do?
Btw, this is a serious concern for me, I like to have consistency in my scripts.
Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
If you ever intend to share your scripts and expect people to modify them, I would recommend the longer names so those people can better understand the use of the variable when they are first introduced to it.
If you are working with someone, I'd ask them what they prefer, though it is probably in the best interest to use longer names to avoid confusion.
If you are keeping this project to your self, it doesn't really matter. I like to use shorter names for variables that I'm using a lot, so I can type less.
Since you like consistency, if you think you'd ever be collaborating with someone or sharing scripts you intend for other people to modify, sticking with the long versions is probably the better idea.
If you are working with someone, I'd ask them what they prefer, though it is probably in the best interest to use longer names to avoid confusion.
If you are keeping this project to your self, it doesn't really matter. I like to use shorter names for variables that I'm using a lot, so I can type less.
Since you like consistency, if you think you'd ever be collaborating with someone or sharing scripts you intend for other people to modify, sticking with the long versions is probably the better idea.
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
I'd recommend using the shortest name possible while maintaining clarity. For example, don't use things like "x" or "y" for important variables (although fine for temporary variables used in calculations or whatever). If you come back in a month having not looked at the code since, you don't want to spend an hour decoding what everything actually means.
It can also be a good idea to clarify what a variable means/will be used for using a comment when you initiate it e.g.etc.
Of course, this is rather unnecessary for the second example here, where it is immediately clear what the variable is & means, but for something more generic like "count" it can help.
It can also be a good idea to clarify what a variable means/will be used for using a comment when you initiate it e.g.
Code: Select all
count := 0 ;for count of files found in directory in below search
searchPattern := "*.txt" ;pattern to find all txt files
Of course, this is rather unnecessary for the second example here, where it is immediately clear what the variable is & means, but for something more generic like "count" it can help.
-
- Posts: 1259
- Joined: 16 Apr 2015, 09:41
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
I personally use the longer names. However, whenever I have a long variable name I need to enter frequently, I make a quick hotstring for it - IE: ::lvn::LongVariableName in my always running script.
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
Thanks for your replies!
So I am way more comfortable to have some comments but use generic names for the vars. Therefore I think there is no real benefit about using long names as they will not tell me what they exactly are for, anyway.
The variables given as examples by me with x and y at the end were vars for coordinates. What would you suggest, if not a X or Y? Of course I do not use this for non-coord vars.SifJar wrote:I'd recommend using the shortest name possible while maintaining clarity. For example, don't use things like "x" or "y" for important variables.
I do not think this is unnecessary. Aside from short vs. long names I also asked myself whether I should use super clear names (even for every single coordinate, e.g. del_button_X) or generic ones like click_pos8_X. Regarding this question, I prefer the generic names now, as it is too hard to always find a suitable and non-confusing name and this fast enough. Therefore I put comments about what is in the var when it is set.SifJar wrote:Of course, this is rather unnecessary for the second example here, where it is immediately clear what the variable is & means, but for something more generic like "count" it can help.
So I am way more comfortable to have some comments but use generic names for the vars. Therefore I think there is no real benefit about using long names as they will not tell me what they exactly are for, anyway.
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
depending on my mood, usually in either of these formats
I leave a comment next to the initial variable explaining the short name. that way if someone is reading it (if if I look back in 6+ months), they know the meaning and can reference it if they forget.
Code: Select all
mSpeed1:=1 ; m for mouse
mouseSpeed1:=1
ms1:=1 ; mouse speed
mx:=23 ; mouse x
my:=55 ; mouse y
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
*poke*
Is it December 21, 2012 yet?
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
The issue SifJar raised was not using an X or Y suffix, but using x:= and y:= on its own.Leli196 wrote:Thanks for your replies!
The variables given as examples by me with x and y at the end were vars for coordinates. What would you suggest, if not a X or Y? Of course I do not use this for non-coord vars.SifJar wrote:I'd recommend using the shortest name possible while maintaining clarity. For example, don't use things like "x" or "y" for important variables.
It's totally fine to use Window1X, Window2X, Window3X and such for variable names and distinguishing those from Window1Y, Window2Y, Window3Y.
And thanks for making the post, it's great to see everyone's reasons for using short or long names, or just striking a balance. And of course, as SifJar and tidbit brought up, using comments is a way to explain a variable upon introduction.
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
Yes, exactly. I was just talking about using an individual letter on it's own as a variable; something those who are used to working with unknowns in mathematics, for example, might be prone to do. Of course, the use of a variable in a script/program is (usually) somewhat different to an unknown in a mathematical equation (although of course, you can be using a variable in a mathematical formula), but when first learning programming, it can seem similar enough to just keep using such symbols in place of proper variable names. Sorry for the confusion Leli196!Exaskryz wrote:The issue SifJar raised was not using an X or Y suffix, but using x:= and y:= on its own.Leli196 wrote:Thanks for your replies!
The variables given as examples by me with x and y at the end were vars for coordinates. What would you suggest, if not a X or Y? Of course I do not use this for non-coord vars.
It's totally fine to use Window1X, Window2X, Window3X and such for variable names and distinguishing those from Window1Y, Window2Y, Window3Y.
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
One thing that influences the names of my variables is auto-complete: I often fall into the trap of naming variables like "drive" or some other prefix that collides with command names. Then auto-complete ends up either giving me the wrong name, or the wrong/inconsistent case, or the variable name when I want the command.
If you use long names but put the unique part at the beginning, auto-complete can take care of the rest.
(I use this auto-complete script with SciTE, so auto-complete includes variable names I type and command names from a pre-defined list. With my current settings, the auto-complete list comes up automatically and are confirmed when I type certain characters, so accidental completions happen.)
If you use long names but put the unique part at the beginning, auto-complete can take care of the rest.
(I use this auto-complete script with SciTE, so auto-complete includes variable names I type and command names from a pre-defined list. With my current settings, the auto-complete list comes up automatically and are confirmed when I type certain characters, so accidental completions happen.)
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
Thanks Leli196 for this thread because I had not yet run across Lexikos' auto-complete script before now. It answers everything I wish the built-in Scite4AutoHotkey auto-complete had while also eliminating its annoyances. Thanks, Lexikos!
My 2 cents on the thread topic is to help keep variable names shorter but still readable, use caps for the start of new words as others have shown and not underscores. So instead of del_button_X, you would have delButtonX or DelButtonX. I have adopted the convention of using the initial lowercase as used in C (delButtonX) to indicate local variables while global variables have initial caps (DelButtonX).
My 2 cents on the thread topic is to help keep variable names shorter but still readable, use caps for the start of new words as others have shown and not underscores. So instead of del_button_X, you would have delButtonX or DelButtonX. I have adopted the convention of using the initial lowercase as used in C (delButtonX) to indicate local variables while global variables have initial caps (DelButtonX).
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
good topic. this is important
variable names should be expressive, so the code is self-documenting, and less comments are needed. in all of your examples i would choose the 2nd
so always make the variable names descriptive first, and then try to shorten
the exception is for loops where ofcourse you would use something like 'i' or 'j'
variable names should be expressive, so the code is self-documenting, and less comments are needed. in all of your examples i would choose the 2nd
so always make the variable names descriptive first, and then try to shorten
the exception is for loops where ofcourse you would use something like 'i' or 'j'
-
- Posts: 319
- Joined: 23 Jan 2016, 23:03
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
Agreed! That is awesome.boiler wrote:Thanks Leli196 for this thread because I had not yet run across Lexikos' auto-complete script before now. It answers everything I wish the built-in Scite4AutoHotkey auto-complete had while also eliminating its annoyances. Thanks, Lexikos!
As far as the variables go I find that I can't decide myself. I tend to like things to be as small and concise as possible, but I have found that sometimes I suffer later going back through code that I wrote long ago. So I guess my advice would be to find a happy medium. Not too long that it takes lots of time to type/lots of space, but also not too short that it becomes unreadable later.
Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?
I have created a new topic and re-posted boiler's off-topic post to it.
Who is online
Users browsing this forum: inseption86, mebelantikjaya and 334 guests