Jump to content

global variable or function?


pcjunki
 Share

Recommended Posts

So czardas and kylomas seem to be agreeing with me on this whole subject of the meaning of Local in "Global scope".

 

Yes. I changed my way of thinking about this after listening to you previously guinness: so at least I listen to you even if I don't always agree with you. ;)

Link to comment
Share on other sites

Yes. I changed my way of thinking about this after listening to you previously guinness: so at least I listen to you even if I don't always agree with you. ;)

Haha, I think we would be super bored otherwise. :) Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I think I've got it now

local $test = "this is local outside of function"

test()

Func test()
    local $test = "This is local in function"
    MsgBox(0,$test,$test)
    Secondtest()
EndFunc

Func Secondtest()
    local $t_est = "This is local in function"
    MsgBox(0,$test,$t_est)
EndFunc

That has been a problem for me that I really did not think about before.

Thanks to all for this discussion.

REB

MEASURE TWICE - CUT ONCE

Link to comment
Share on other sites

Exactly reb, think of it like that.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

 
I have a question - is there really a good reason to use Local at all if I write under the rule that each global variable  start with 'g_' ?
 
The only good reason I know why you always need to use Local in function when you need the variable as Local, is to  ensure that the declared variable will be Local and not the global(Because maybe there is already global variable with the same name)
 
The "Because maybe there is already global variable with the same name" is why to declare variable with "Local" inside function(when you need the variable as Local)..

 

But if I know that all my global variabls start with 'g_' and I'm used to write like this then I know that "maybe there is already global variable with the same name"

Will never happen so I don't need to ensure that the variable (that I want to use it as local) will be Local using "Local". (It anyway Local by default)

So I can save myself the trouble of use Local.

 

Is this new approach it is correct? What do you think?
I do not implement this approach. It's something I thought about now

I'm not entirely sure it's related to this thread but I do not know about a better place to ask this question.

Edited by Guest
Link to comment
Share on other sites

I use it because we should declare variables in the lowest scope possible, even though technically speaking the variable is accessible everywhere. It then gives the code reader an understanding (well you anyway) that hey this variable is only used in the current scope i.e. global space and is not accessed with inside a function. Understand?

I first was told to look at it like this when I went a ahead and started to change all the examples to have Global declarations. It wasn't until Valik sat me down and explained why I was wrong, that I soon reverted my commits. That was 3 years ago when I think about it.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

True, this is one reason (that it helps you better understand the script).
But the main reason why I want to save from using the "Local" is that maybe it would make the script run faster.

I think that maybe $var = 0 faster than Local $var = 0
If this is the case, then it's a good enough reason why save the use of "Local".

So the question is whether there is a difference in speed?

 

I first was told to look at it like this when I went a ahead and started to change all the examples to have Global declarations.

"Global" Sounds more sense in global. I'm surprised that you used "Local" for Global declarations but at least I know I'm not the only one and also Recognized Developer made this :)

Edited by Guest
Link to comment
Share on other sites

There is a speed difference, but then I wouldn't recommend AutoIt for applications that time sensitive execution.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

There is a speed difference, but then I wouldn't recommend AutoIt for applications that time sensitive execution.

 

So you confirm what I think.

The only problem is that this way it is less easy to read.

Which is why you recommend to use the "Local" in a function (in addition to the second reason)

 

 
You say that this is how to solve the problem( the problem that it is harder to understand the script)
 

I think that no matter how minor difference is - as long as you have solution how solve this problem without eliminating the advantage of execution speed (which exists when using $var = 0 insted of Local $var = 0), then the solution is  better option.

I think of it this way:

The computer is the first priority. The second priority is me.

As long as the computer understand the code and I and someone else understand the code then it is OK

The goal is to write efficient code with all the advantages:

1) computer understand the code and not say "This is bullshit."

2) It runs faster

3) I and someone else understand the code easily like when I using "Local $var = 1"

So my solution is very simple - Which is to use a comment.

Global $g_var = 1 ; I always use 'g_' and even in UDFs that I downloaded(Need to edit the global variables with automation.)
Func Example()
    ; v There will always be Local in this case. and it should be faster. But we still have problem - Another coder not sure that this is Local. Hrader to understand...
    $var = 1 ; <- Local
    ; The Solution: add "<- Local" in comment.
    ; So now the computer is happier and also the reader!
    [ Code ]
EndFunc

Better than this:

Global $var = 1 ; No rule at all about what to name global vars.. 
Func Example()
    ; As a result, we must use "Local" In order to avoid the case that $var will be global.
    Local $var = 1
    ; The Solution: add Local before $var = 1
    ; So now the computer is NOT happier and ONLY the reader is hapy!
    ; Because the computer will run it slower. but the reader/coder don't care..
    [ Code ]
EndFunc

This should explain everything perfectly so there is no need for more words.

 

I'm not saying that I will use this approach(Because I'm not sure and that's why I am discussing here). I just presents the approach and this is the approach

And I'm open to everyone's opinion. (I'm trying to say is that I'm not locked in on it)

 
 
Link to comment
Share on other sites

Unless you're VERY careful in naming of your variables, when you don't declare them you run the risk of overwriting the variable because you used the name of a global variable inside a function without using the Local keyword. You'll run into mistakes like this when you reuse code, when you have functions in an include that you've forgotten what variables are used in it, as examples. Not explicitly declaring your variables is a bad idea and not recommended, even for the incredibly small difference in speed, and I do mean incredibly small difference in speed.

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 think one of the biggest confusions here is that the words which describe scope have the same spelling as the keywords used to declare variables. I do not recommend using the Local keyword to declare global variables. If I see a global variable declared using the Local keyword, I normally draw one of the following conclusions: either the code is unfinished or the programmer doesn't know what they are doing. One thing I notice quite often are variables being used in the wrong scope - or rather ones which would be better placed inside a function.

What we are discussing here is mainly semantics: the meanings of words which describe scope are not the same as the keywords used in a programming language. Which is worse - a variable declared in the wrong scope or one declared using the wrong keyword? The answer may depend on context. Regardless of such questions, if the interpreter runs the code without encountering problems, it does not automatically follow that the code has been well written.

Edited by czardas
Link to comment
Share on other sites

Here's an example of this stuff.

Why are both the global and "local" variable altered?

Global $SomeVariable = 10

_Func()

ConsoleWrite($SomeVariable & @LF)

Func _Func()
    $SomeVariable = 10
    $SomeVariable += 10
    ConsoleWrite($SomeVariable & @LF)
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

There is only one var, $SomeVariable declared in "global" scope using the GLOBAL keyword...

Is this what you mean..???

Global $SomeVariable = 10

_Func()

ConsoleWrite('outside of func --->>> ' & $SomeVariable & @LF)

Func _Func()

    local $SomeVariable = 8
    $SomeVariable += 12
    ConsoleWrite('inside     func --->>> ' & $SomeVariable & @LF)
EndFunc
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Using the standard definition of local scope (not keyword), we can make the following simple statement about AutoIt:

Variables which are not explicitly declared are created in the local scope.

; Example 1
_assign()
ConsoleWrite(IsDeclared("var") & @LF) ; ==> 0

Func _assign()
    $var = 1 ; local variable (local to the current scope inside the function)
EndFunc

; Example 2
$var = 1 ; global variable (local to the current scope outside the function)
ConsoleWrite(IsDeclared("var") & @LF) ; ==> 1

;

The code is illustrative, and not necessarily well written. If we confuse keyword with actual scope, the above statement becomes meaningless. It is important to make a clear distinction in such cases.

Edited by czardas
Link to comment
Share on other sites

 

Here's an example of this stuff.

Why are both the global and "local" variable altered?

Global $SomeVariable = 10

_Func()

ConsoleWrite($SomeVariable & @LF)

Func _Func()
    $SomeVariable = 10
    $SomeVariable += 10
    ConsoleWrite($SomeVariable & @LF)
EndFunc

No bug, as you're not specifying in the function that the other variable (with the same name) is local.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

There is no other variable, just the one as demonstrated here...

Global $SomeVariable = 10

_Func()

ConsoleWrite($SomeVariable & @LF)

Func _Func()
    $SomeVariable += 10
    ConsoleWrite($SomeVariable & @LF)
EndFunc

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Unless you're VERY careful in naming of your variables, when you don't declare them you run the risk of overwriting the variable because you used the name of a global variable inside a function without using the Local keyword. You'll run into mistakes like this when you reuse code, when you have functions in an include that you've forgotten what variables are used in it, as examples. Not explicitly declaring your variables is a bad idea and not recommended, even for the incredibly small difference in speed, and I do mean incredibly small difference in speed.

 

 

You're right.

This is why I do not do it. It's not worth the risk.

But maybe it's a good idea to add an option in Au3Stripper so Au3Stripper will do it.

My idea is that Au3Stripper convert this code:

Global $var = 1
Func Example()
    Local $var = 1
EndFunc

To this:

Global $g_var = 1 ; I always use 'g_' and even in UDFs that I downloaded(Need to edit the global variables with automation.)
Func Example()
    $var = 1
EndFunc

 

But you'll probably say it's not worth the effort of the development of this feature. I might agree with that.
Link to comment
Share on other sites

LOL

Global $g_g_g_g_g_g_Var

;

I don't think it's such a good idea because of potential conflicts with existing variables. :whisper:

 
You think too much simple.

It should not be big deal to do that it will not add 'g_' when it already exists

 

Of course I have not tried to do it myself so I'm not the most suitable position to say it.

But I guess the whole thing here is to use something like this

If StringLeft($VarName,3) <> '$g_' Then ...
Link to comment
Share on other sites

By all means try it: you can create whatever exceptions you wish. I would add an Else to that conditional statement just in case the condition turns out to be False (and decide how you want to handle the exception). Also check that whatever you do (regardless of the condition) does not cause any conflict with other existing variable names.

Edited by czardas
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...