Jump to content

General Coding Question: Variable Declarations


Recommended Posts

Sorry for being an all around programming nub ;)

So my program has base functions, then sub functions... and some sub functions have their own sub functions (sub2)

In the beginning of the program, I declare any Global variables that will be used by multiple base functions.

In each Base Functions, I declare local variables that will be used in that function, and its sub functions. And I do the same for the sub functions for sub2 functions. When I call a sub function, I just pass any variables as arguments to the sub function, and if the sub2 function requires it, I pass it again.

Instead of passing a local variable to multiple sub functions, should I just declare them as Global, or is this bad practice?

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

The generally accepted principle is to have as few Global variables as possible - this minimizes the memory overhead and reduces the chances of you altering the variable without meaning to do so. In reality it is pretty difficult to write an AutoIt script without any Global variables - but your goal should be to keep them as few as possible. Pass variables to other functions as parameters - remember that you can pass an array if you must pass a lot of them. ;)

Search the forum for other threads about this - Valik has offered some excellent advice at times. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you very much for the info Melba23...that was a very interesting read. As someone who tends to use a lot of Global variables, it will be challenging cutting back on them, but educational at the same time.

Do you think that from a practical standpoint there would be much to gain? Autoit is the only language I program in, so its a little hard for me to quantify what something like this means.

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

  • Moderators

someone,

Do you think that from a practical standpoint there would be much to gain?

Yes, it will make you a better coder. Regardless of the language you use, there are certain things which are worth doing:

- Not using GoTo (do not ask - it is just bad! ;))

- Making your code modular (easy to reuse functions in other scripts)

- Not having too many Global variables (as explained above, you save memory and make the code more stable)

And you can ask trancexx's cat to list many more if you wish! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I think you just want some attention. Someone to talk to maybe.

Get a cat.

heh? what was the stab for?

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

The responses from Valik are extremely helpful. I didn't stop to realize what holding global variables does to a system. Some of the machines that run the apps I make are pretty slim on resources so this is extremely helpful.

And I suppose the answer to my question is to make localized variables over global variables whenever possible.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

I suppose the answer to my question is to make localized variables over global variables whenever possible

;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

someone,

Yes, it will make you a better coder. Regardless of the language you use, there are certain things which are worth doing:

- Not using GoTo (do not ask - it is just bad! :))

- Making your code modular (easy to reuse functions in other scripts)

- Not having too many Global variables (as explained above, you save memory and make the code more stable)

And you can ask trancexx's cat to list many more if you wish! :)

M23

Cool thanks again for the info. I have a big rewrite of an AD clone I wrote to do, I'll try to limit my global vars as much as possible. It will be a good exercise for me. And you are absolutely right, making code modular is HUGE. The ability to reuse, and maybe even more importantly, find code you've written previously helps you be more efficient, and just a better coder. More times then I can count I've buried functions in scripts that I never ended up using, only to want to use those functions again and having no idea where I put it.... ;)

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Another noob question...

Is it okay to declare but not assign variables in the beginning of a function/script?

As Valik stated in his linked post, that front-loading variables is bad. But does that mean declaring them, or assigning values to them, or both?

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

As I understand the way AutoIt works, declaring the variable places it in the variable table so there will be some penalty involved if you declare it early regardless of whether you assign a value to it or not. ;)

I entirely agree with Valik's point that you should only declare variables when you use them or immediately before if you use them in some form of structure. Similarly you should not declare variables within a loop as AutoIt has to scan the variable table each time to check if it already exists:

Local $iFoo ; Declare here as you have 2 appearances within the structure (and a possibility it is never declared)
If $fFlag = 1 Then
    $iFoo = 1
ElseIf $fFlag = 2
    $iFoo = 0
Else
    ; And now for something completely different not involving $iFoo
EndIf

Local $iFoo = 0 ; Declare here and not inside loop
For $i = 1 To 100
    $iFoo = _Something()
    ; Code involving $iFoo
Next

But I am sure others can give you a more detailed explanation - or ask trancexx's cat again. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for all the assistance Melba. I have quite a bit of work ahead of me now with this new information =(. I've been a bad boy with the programs I've made thus far.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

My pleasure. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

In each Base Functions, I declare local variables that will be used in that function, and its sub functions. And I do the same for the sub functions for sub2 functions. When I call a sub function, I just pass any variables as arguments to the sub function, and if the sub2 function requires it, I pass it again.

What you end up talking about is global vs passed parameters. But at the beginning of the paragraph it sounds like you are talking about a programming language with a nested block scope. One such is Pascal. You may declare functions inside of functions. Also local vars declared in the enclosing function are visible in the nested function. AutoIt cannot do that.

One reason to have more globals in AutoIt is because hotkey assignment does not allow arguments in the assigned function. Also when passing arrays as params it may be a good idea to use ByRef instead of pushing the whole array on the stack(maybe AutoIt uses some table to avoid this but ByRef should only require a pointer to the array.)

When I did a lot of Delphi programming it seemed cool to have a function inside a function. But then I realized if I was only calling that function from the enclosing function(since it was not visible anywhere else) then I could probably produce the same effect as the function just using a loop or Case statement.

Just rambling on while drinking my coffee. ;)

Link to comment
Share on other sites

But at the beginning of the paragraph it sounds like you are talking about a programming language with a nested block scope. You may declare functions inside of functions. Also local vars declared in the enclosing function are visible in the nested function. AutoIt cannot do that.

That's what I was hoping for in the first place. After creating my own test scenario and trying it out myself, I noticed it didn't work, which prompted me some questions on declaring global vars vs passing local vars as parameters to nested functions.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...