Jump to content

Should Au3Check Count Global Variables Declared Inside Functions?


TheDcoder
 Share

Recommended Posts

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!

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

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

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

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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

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

 

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

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Developers

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  

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
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.

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

Link to comment
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?

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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

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

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators
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. :)

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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. 

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

Link to comment
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!

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
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.

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

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

×
×
  • Create New...