TheDcoder

Should Au3Check Count Global Variables Declared Inside Functions?

46 posts in this topic

Hello everyone, I discovered a bug yesterday and I posted it at the bug tracker:

Quote

Au3Check is not able to recognise Global variables declared inside functions if the variables are used above the function declaration

I also made a simple script which can be used to reproduce the bug:

CreateVariable()

ConsoleWrite($sGlobalVariable & @CRLF)

Func CreateVariable()
    Global $sGlobalVariable = "Foobar"
EndFunc

The bug was closed by @BrewManNH:

Quote

This isn't a bug but poor programming.
Au3Check reads the script line by line from the first line to the last. It doesn't execute the script. Because you're trying to access a global variable that hasn't been declared yet (at that point in the script read), you get an error.

 
 

While I partially agree with the above statement, My code was not practical enough... so @mLipok advised me to create a thread on the forums with practical code (Thanks!). That is the point of this thread, I am going to provide the code where I experience this bug/problem :D.

I discovered this bug when I was working on one of my projects called "ProxAllium". When the main script finishes execution, Au3Check throws a nasty warning about "variable possibly used before declaration":
YJMj8db.png

As you can see, the variable is indeed being used after calling the function in which the variable is declared... The warning won't appear if I declare the function ABOVE the variable. As @BrewManNH said, Au3Check reads line by line... I think this should be changed, Au3Check should not throw warnings if the interpreter is able to run the code, at least most of the time anyway!

So what do you guys think? Is this a valid bug?... and I request those who participate in the discussion not to discuss the code being "poor", that is another thing/thread in itself ;)

P.S I had already written this once but the forum editor decided to mess up and when I undid (Ctrl + Z) something... This is a poorly written version of that article, I was very frustrated while writing this!


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites



TheDcoder,

You do realise that you are not declaring $g_hTorOutPut anywhere before the line in which that variable is flagged as "possibly used before declaration"? I presume that variable is being declared within the GUI_CreateTorOutputWindow function and so after the line which is flagged - hence Au3Check's warning.

Au3Check is a fairly simple tool (although if you know anything about YACC you will appreciate it is far from simple to code) and only checks the script line-by-line. Asking it to act as the actual interpreter is a whole new game and would be a complete waste of time in my opinion. So: no bug - just a tool doing its best to help you from making a possible error. After all, it is only flagged as a warning, not as an error.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
1 minute ago, Melba23 said:

You do realise that you are not declaring $g_hTorOutPut anywhere before the line in which that variable is flagged as "possibly used before declaration"?

Yes :):

19 minutes ago, TheDcoder said:

As @BrewManNH said, Au3Check reads line by line

 

3 minutes ago, Melba23 said:

Asking it to act as the actual interpreter is a whole new game

I am aware that it is not worth to make it act as an interpreter but it could at least read the function's code if the function is being called.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

TheDcoder,

As I said, Au3Check is only giving a warning, not an error - so you are quite at liberty to ignore it if you know that the variable has indeed been declared. But, despite your wish not to go there I would argue that not specifically declaring a Global variable before you first access it is not good coding practice as declaring it inside another function can lead to all sorts of problems when cutting and pasting modules between scripts.

And if you want Au3Check to do more than it does, you learn enough YACC to rewrite it - I am sure Jos and jpm would welcome your offer to take over development.

M23

 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

No bug and has been explained often: Au3check read the whole script in one pass and gives basic warnings and errors, no more no less.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
10 minutes ago, Melba23 said:

As I said, Au3Check is only giving a warning, not an error - so you are quite at liberty to ignore it if you know that the variable has indeed been declared.

The only thing which bugs me is that SciTE automatically takes the cursor (the blinking position thingy)  to the line of the warning... it is quite annoying!

17 minutes ago, Melba23 said:

declaring it inside another function can lead to all sorts of problems when cutting and pasting modules between scripts

The function in which I am declaring the Global variable is supposed to be only called once per execution... so that would not cause any chaos :D.

18 minutes ago, Melba23 said:

And if you want Au3Check to do more than it does, you learn enough YACC to rewrite it - I am sure Jos and jpm would welcome your offer to take over development.

I would love to do that but I still have to a lot to learn... and the very limiting TIME factor :(.

 

P.S I see that Jos has replied before I finish this post, will reply to his post in another post.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

I get you @Jos, we should at least have something by which we can "ignore" some errors. Do you agree with me?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

9 minutes ago, TheDcoder said:

The only thing which bugs me is that SciTE automatically takes the cursor (the blinking position thingy)  to the line of the warning... it is quite annoying!

My thinking in my head: I find it quite annoying that forum members tell me features are quit annoying without checking whether this is a configurable feature!

My political correct answer: No that is a feature which I actually like as that is the starting point for clearing the shown warnings and errors. Did you assume this is not configurable or did you actually check the SciTE helpfile to see whether you could switch that feature off? ((hint:#AutoIt3Wrapper_Jump_To_First_Error)

Jos

Edited by Jos
typos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Just now, TheDcoder said:

we should at least have something by which we can "ignore" some errors. Do you agree with me?

No I do not: You either use au3check and adhere to its standards or simply don't use it. ;)

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
7 minutes ago, Jos said:

Did you assume this is not configurable or did you actually check the SciTE helpfile to see whether you could switch that feature off?

I remember visiting the SciTE help file, did not find anything there... not even #AutoIt3Wrapper_Jump_To_First_Error! I might have missed it, I am not much of a reader but a skimmer


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Hey and I understand... the trigger for my response in the way I did it was was the word annoying.
Do you understand it sometimes can be pretty frustrating when spending a lot of time to make these type of feature for the greater good and someone comes along and dumps his shortsighted response like you did?

Jos  


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
1 minute ago, Jos said:

Do you understand it sometimes can be pretty frustrating when spending a lot of time to make these type of feature for the greater good and someone comes along and dumps his shortsighted response like you did?

 
 

I sincerely apologise for using the word "annoying" but it is/was not my intention to hurt anyone :). I am very grateful for the efforts which you have put in the community and I mean it! It is not my intention to sadden anyone, but circumstances force me to use terrible word choices...

I am currently looking into possible workarounds, I will shortly reply about my workaround if I find it.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
16 minutes ago, TheDcoder said:

I am currently looking into possible workarounds

The workaround is don't write bad code that causes issues like this. If you REALLY want a work around that will compensate for your bad programming style, put the function at the TOP of the script and the rest of the code below it.

Func CreateVariable()
    Global $sGlobalVariable = "Foobar"
EndFunc
CreateVariable()

ConsoleWrite($sGlobalVariable & @CRLF)

This is what happens when you include another file in your script using #include, that's why you don't get these warnings when using Global variables from include files.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I don't want to move the functions to the top... It's not the place where a function should be. Now I am thinking of changing the style, any recommendations on how should I proceed?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Put the function into a separate file and #include it in your main script would be one way.

Also, a function can be anywhere in the script, as long as its not inside another function or loop. Just because most people put them at the bottom of the code doesn't mean you have to do it that way. When you include another UDF in your code, everything in that UDF is inserted into your script at the point where you put the #include line. So putting an include at the start of your script means all those functions are placed at the top.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
1 minute ago, BrewManNH said:

Put the function into a separate file and #include it in your main script would be one way.

A great idea actually... I will try this one ;)

And yes, I know that #include tells the interpreter to think of the included file as if it has been copied over at the #inlude line :D


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
13 hours ago, TheDcoder said:

I get you @Jos, we should at least have something by which we can "ignore" some errors. Do you agree with me?

We get this from time to time, forum members who ask for a workaround to permit them to write crap code; I will never understand it. @Jos has more patience than I in his continued pleasant responses to such inane requests. :)

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

and to recap:  this behavior is due to both awful scripting and a stubborn preference for functions at the bottom, and the most suitable mitigation is certainly not found in altering the way AU3check behaves.

@Jos was downright saintly in this thread. 

1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
10 hours ago, JLogan3o13 said:

forum members who ask for a workaround to permit them to write crap code;

Maybe those people can't think of any better way to do it? ¯\_(ツ)_/¯
They will, of course, find a better way after asking at the forums :P (Same thing with me)

10 hours ago, iamtheky said:

this behavior is due to both awful scripting and a stubborn preference for functions at the bottom, and the most suitable mitigation is certainly not found in altering the way AU3check behaves.

 

I totally agree with this one.

 

As for my workaround, after taking useful input from here, I decided to separate the GUI #Regions into 2... GUI Creation (functions) at the top and the GUI Event Handlers at the bottom. It works perfectly :D.

Thanks to everyone who participated in this thread!


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Booo, I was hoping you would have a decent defense for declaring global within functions.  

Concessions when I am trying to be provocative make me look even more like a dick than I already am.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
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

  • Similar Content

    • ur
      By ur
      I have a script , during compilation and test execution, it worked perfectly but sometimes I am getting error as "Variable used without being declared."
       
      I understood somewhere in the branching logic this is happening.
       
      But not able to find it exactly.
       
      As I am using multiple include statements.the line number is also not giving accurately.
       

       
      Can anyone suggest what is the approach to resolve this?
    • Byte11
      By Byte11
      WinSetState ("[CLASS:OpusApp]", "", @SW_SHOWMAXIMIZED) and WinSetState ("[CLASS:OpusApp]", "", @SW_MAXIMIZE) don't maximize the window.  Using WinSetState("[CLASS:OpusApp]","",@SW_RESTORE) allows me to maximize it about 15% of the time. I've tried using  WinMove("[active]",0,0,@Desktopwidth,@Desktopheight), but that only makes it worse. I've also tried putting in a sleep and maximizing it again. Does anyone have a solution?
      Here's my code:
      The startup script is intentionally commented, because I'm not testing it right now. The other comment is just for debugging when I need it. 
       
      UPDATE: When I run two instances of the program, it works almost all the time, but if I copy the code inside the else statement twice it doesn't work at all (despite the fact that it's running the same code just in one program vs two). I also noticed that when I run two instances of it, about 50% of the time when it opens both the windows, it also hits the windows button and types the letter d into the search box. Neither of these actions are in my code. I'm not really sure what's going on. 
    • distancesprinter
      By distancesprinter
      _ArrayDisplay($aArray, "Window Title", "1:", 0, Default, "Column") ; Expected results are rows 1 to the end of the array, all columns. The result is rows 0-1, all columns. The API reference is here:
      https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayDisplay.htm
       
      Am I doing something wrong?
    • TrashBoat
      By TrashBoat
      Is this possible:
      Executing a function from an include, but taking the function name from a gui input and then executing that function using the include:
      #include <Something.au3> ;input reads "Tree" $functionName = GuiCtrlRead($input1) $functionName(1) And the include is gonna have
      Func Tree($x) If $x = 1 Then $this = "text" MsgBox(0,$this,"whatever") EndFunc is it possible?
    • Altor
      By Altor
      Hi all:
      Sorry for such an innocent question .....
      It is possible to delete all Au3Check error messages with a hot key when they appear with F5 and you can continue editing the code without seeing the red lines. For now my only solution is to reload the script but it is a bit annoying.
      Thank's to all
      Altor