Jump to content

global variable or function?


pcjunki
 Share

Recommended Posts

You can't put values into a variable the way you're looking to do with a Static variable, so you need to do it in more than one line, it's as simple as that. Just because you are trying to make your code shorter, and probably a lot harder to read and follow, doesn't mean you're going to be able to achieve that.

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

You can't put values into a variable the way you're looking to do with a Static variable, so you need to do it in more than one line, it's as simple as that. Just because you are trying to make your code shorter, and probably a lot harder to read and follow, doesn't mean you're going to be able to achieve that.

 

Now you understood my question.

Ok, I will live with it. It can only lead to more lines but if there is no way to shorten it so no matter.

 

I think it would be good if in the next versions of Autoit, will be added to the syntax something which is considered as @CRLF.

Just like _ is considered as "Continue the line (While you can write one line as few lines..), Then there is a need for something

that is translated to @CRLF During the Compile time.

So that way I can write:

$var1 = 1 [Key] $var2 = 2 [Key] $var3 = 3

(It is necessary to decide what [Key] will be choose for this purpose)

And the compiler will translate it to this:

$var1 = 1
$var2 = 2
$var3 = 3
 

I hope you get the point. Hard for me to explain in English

Edited by Guest
Link to comment
Share on other sites

  • Moderators

gil900,

Why do you have this burning desire to reduce the number of lines in your script to an absolute minimum? What do a few more lines matter? The main thing is to keep the code readable so that you can return to it after a while and still understand what on earth is going on. :)

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 agree with Melba23. A nice addition to AutoIt might be a verbatim string literal, in which a new line sequence can be achieved by just hitting enter in the editor and not having to use @CRLF or such-like. I won't be adding it to trac though.

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

gil900,

Why do you have this burning desire to reduce the number of lines in your script to an absolute minimum? What do a few more lines matter? The main thing is to keep the code readable so that you can return to it after a while and still understand what on earth is going on. :)

M23

 

 

For me there is no difference between $var1 = 1 [Key] $var2 = 2 [Key] $var3 = 3 .

Suppose that [Key] is | then it looks easy to read:

$var1 = 1 | $var2 = 2 | $var3 = 3 | $var4 = 2 | _

$var5 = 13 | $var6 = 6 .........

EDIT: Fixed example ^

If it was with a lot of variables then definitely it was more comfortable to read the script because it would save a you a space to read more easily the rest of the code.

 

That's how I think.

I suggests considering developing this feature. Not everyone will have to use it ..

Anyone will do what is comfortable for him

Edited by Guest
Link to comment
Share on other sites

You could create this yourself in a pre-processor if you feel it's of worth, though I can see if only causing more headaches or at least not be used. BY the way, AutoIt doesn't get compiled in the traditional sense, the "compiler" just adds the script as a resource to the interpreter, which is then read when the executable is run. Yes, the "compiled" executable is just the interpreter.

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

You could create this yourself in a pre-processor if you feel it's of worth, though I can see if only causing more headaches or at least not be used. BY the way, AutoIt doesn't get compiled in the traditional sense, the "compiler" just adds the script as a resource to the interpreter, which is then read when the executable is run. Yes, the "compiled" executable is just the interpreter.

 

You could create this yourself in a pre-processor if you feel it's of worth, though I can see if only causing more headaches or at least not be used.

Yes, I know. But if I do that then a nearly nobody will use it except me - not worth it.

 

AutoIt doesn't get compiled in the traditional sense, the "compiler" just adds the script as a resource to the interpreter, which is then read when the executable is run. Yes, the "compiled" executable is just the interpreter.

I know. Just to write in English, I save words.

I know that the word "compile" is not the most appropriate in this case but I do not know another word

Link to comment
Share on other sites

A good place to use a static variable would be when you need to run a long process to assign a value to the variable. Here's a mock illustration of this type of use:

;

#include <MsgBoxConstants.au3>

For $i = 1 To 3
    ConsoleWrite(ParcelDelivery() & " hours delivery" & @LF)
Next

Func ParcelDelivery()
    Static $ParcelPost = LongProcess() ; This line of code only runs once, no mater how many times you call the function.
    $ParcelPost += 24 ; The current value of a static variable is kept in memory.
    Return $ParcelPost
EndFunc

Func LongProcess()
    Return (MsgBox($MB_RETRYCANCEL, "Long Process Simulator", "This process may take for ever!") = 4) ? LongProcess() : 0
EndFunc
Edited by czardas
Link to comment
Share on other sites

There are many uses for it.
I thought about this usage

SomeFunc()
SomeFunc()
SomeFunc()
Func SomeFunc()
    Local Static $IsFRunned ; Is the function was not runned before?
    If Not $IsFRunned Then
        ConsoleWrite('This is the first time that the function is running' &" (Line "&@ScriptLineNumber&")"&@CRLF)
        $IsFRunned = 1 ; Whe have to update it to 1 in this case so it will not happen again
    Else
        ConsoleWrite('This is NOT the first time that the function is running' &" (Line "&@ScriptLineNumber&")"&@CRLF)
    EndIf
    
    ; ... Some code that use $IsFRunned ...
EndFunc
Edited by Guest
Link to comment
Share on other sites

There are many uses for it.

I thought about this usage

SomeFunc()
SomeFunc()
SomeFunc()
Func SomeFunc()
    Local Static $IsFRunned ; Is the function was not runned before?
    If Not $IsFRunned Then
        ConsoleWrite('This is the first time that the function is running' &" (Line "&@ScriptLineNumber&")"&@CRLF)
        $IsFRunned = 1 ; Whe have to update it to 1 in this case so it will not happen again
    Else
        ConsoleWrite('This is NOT the first time that the function is running' &" (Line "&@ScriptLineNumber&")"&@CRLF)
    EndIf
    
    ; ... Some code that use $IsFRunned ...
EndFunc

Maybe use True or False and take full advantage of the language. Another thing $bHasFuncRun, denotes that the variable datatype is boolean, just as $iAge tells that it's an integer. This greatly improves code readability and reduces errors where a string is "accidentally" assigned to a variable that should be a number.

Unfortunately AutoIt isn't strongly typed, so we (the coders) need to add some method to ensure that we don't inadvertently mess up along the way, despite AutoIt being too forgiving. Now if there was a type checking script, then we could adopt a similar approach to how TypeScript ensure variables in JavaScript are strongly typed with zero overhead.

 

Local $age: Int = 21
Local $name: String = "guinness"
$name = 100 ; This would display an error, and thus wouldn't be able to "compile" the script.

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

TheSaint,

 

This demonstrates using the latest version of AutoIt, what we have been talking about. You might want a Global variable when you have a filehandle open, but then people could argue even this can be done without a global variable, which they would be right in saying.

 

#include <File.au3>
#include <StringConstants.au3>

Global $g_sSomeBigString = "" ; As this is only used in the Example function, why is it declared here?
Global $g_hFile = FileOpen(_TempFile(), BitOR($FO_READ, $FO_OVERWRITE)) ; This is okay, as it's referenced in more than one place.

OnAutoItExitRegister(OnClose) ; Register a function to call on AutoIt exit.

Example()
Write()
ReadAndReplace()

Func Example()
    ; $g_sSomeBigString should be called $sSomeBigString and declared using Local.
    For $i = 0 To 100
        $g_sSomeBigString &= String($i)
    Next
    ConsoleWrite($g_sSomeBigString & @CRLF) ; Since this is only used here.
    Return Null ; Like void.
EndFunc   ;==>Example

Func OnClose()
    FileClose($g_hFile) ; Tidy resources on exit.
    Return Null ; Like void.
EndFunc   ;==>OnClose

Func ReadAndReplace()
    FileSetPos($g_hFile, 0, $FILE_BEGIN)
    Local $sFileRead = FileRead($g_hFile) ; Assign to a local variable.
    FileSetPos($g_hFile, 0, $FILE_BEGIN)

    $sFileRead = StringReplace($sFileRead, "1", "", 0, $STR_NOCASESENSE) ; Manipulate the data in the variable.
    ConsoleWrite($sFileRead & @CRLF)
    $sFileRead = "" ; Pointless as this will be destroyed once this function is returned from.
    Return Null ; Like void.
EndFunc   ;==>ReadAndReplace

Func Write()
    FileSetPos($g_hFile, 0, $FILE_BEGIN)
    For $i = 0 To 1000
        FileWrite($g_hFile, $i & @CRLF)
    Next
    FileSetPos($g_hFile, 0, $FILE_BEGIN)
EndFunc   ;==>Write
Does anyone have a valid example?

 

Oops, just noticed this buried in amongst all the other posts.

Thanks, food for thought.

Pretty straightforward, but very different from my simplistic approach, where I keep Declaration and Assignments separate.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

where I keep Declaration and Assignments separate.

I never understand why people do that, especially in a scripting language where the script is interpreted line by line.

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 guess it is a bit like how some eat their meat and veg.

Many eat them separately.

I don't tend to, unless the meat is exceptional and I really want to savor it on its own.

Still, I'm not a big fan of eating many cooked veggies on their own, without some meat in the mix.

Really though, it's a deliberate approach I favor.

I like to read things line by line in a simple manner.

This means that I often deliberately avoid nesting statements or commands.

Just an aid to troubleshooting I guess ... and easier to borrow code from, with less chance of introducing a bug (i.e. you forgot to edit .... I do a lot of copy and paste to save time).

The mystery of different minds working in different ways to achieve essentially the same end goal.

P.S. I guess for me, it is also tied in with issues like, knowing I would be better served by a function, instead of duplicating code, but I'm in a hurry, or more often than not, I have so much in my head, I don't want to risk forgetting something (possibly a nifty idea or three), while being diverted coding said function. Sometimes I go back later and create that function, especially during an update (if I'm reminded or notice or essentially need to re-use the code for a new bit).

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

I never understand why people do that, especially in a scripting language where the script is interpreted line by line.

 

It sometimes makes sense to declare several related variables (which may or may not be used) in one part of the script and assign values as and when needed. With a complicated script, this approach may simplify things. I would not do this with all variables - normally they would have to be family members.

Edited by czardas
Link to comment
Share on other sites

Well in C# it's said (in the community) that it's pointless to declare a variable with the default value e.g. object objectVar = null, as you're repeating what the compiler does behind the scenes. I also get it in regards to loops in AutoIt (doesn't apply to Java or C#) where for execution purposes it's best to declare outside a tight loop. Otherwise, I see it as good coding practice to declare and intialise, generally speaking.

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

Also, you don't always know what the value will be and getting the value may involve a long process. Again I don't think there is a black and white answer to this. I always think about memory usage: large array - long process - may not be needed ==> DO NOT INITIALIZE.

Edited by czardas
Link to comment
Share on other sites

Of course not. I realise this "rule" can't be followed 100% of the time, but a good 90% of the scripts I write can.

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 never understand why people do that, especially in a scripting language where the script is interpreted line by line.

 

You never understand?

It seems that you understand here:

 

If one of the variables is large array then it can take memory..

So it is better to declare this kind of variable only when necessary.

But I changed my mind.

I can declare all global variables at the top of the script without always having to assign them any value (like a large array).

And assign to them the value only when it first needed.

This should be a better way

 

 

 

You:

Yes, correct.

 

 

Or I did not understand exactly what you mean.

It's a little confusing me

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