Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Leli196
Posts: 216
Joined: 29 Aug 2015, 05:47
Location: Germany

Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 09:20

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.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 09:34

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.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 12:11

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.

Code: Select all

count := 0 ;for count of files found in directory in below search
searchPattern := "*.txt" ;pattern to find all txt files
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.
Shadowpheonix
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?

19 Feb 2016, 12:25

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.
Leli196
Posts: 216
Joined: 29 Aug 2015, 05:47
Location: Germany

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 12:30

Thanks for your replies!
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.
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: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.
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.
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.
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 12:34

depending on my mood, usually in either of these formats

Code: Select all

mSpeed1:=1 ; m for mouse
mouseSpeed1:=1
ms1:=1 ; mouse speed
mx:=23 ; mouse x
my:=55 ; mouse y
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.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 14:05

Leli196 wrote:Thanks for your replies!
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.
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.
The issue SifJar raised was not using an X or Y suffix, but using x:= and y:= on its own.

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.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 19:00

Exaskryz wrote:
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.
The issue SifJar raised was not using an X or Y suffix, but using x:= and y:= on its own.

It's totally fine to use Window1X, Window2X, Window3X and such for variable names and distinguishing those from Window1Y, Window2Y, Window3Y.
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!
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 20:49

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.)
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 23:15

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).
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Variable names - keep them short for clear/neat/tidy code or long to be self-explanatory?

19 Feb 2016, 23:20

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'

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

19 Feb 2016, 23:34

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!
Agreed! :thumbup: That is awesome.
As far as the variables go I find that I can't decide myself. :oops: 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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: inseption86, mebelantikjaya and 334 guests