Jump to content

Coding Best Practice Question


Recommended Posts

I know we have a general "best practice" and a lot of things are open to personal preference.

A long time ago I tried to adhere to some of the best practices like calling my arrays $aXXX and my strings $sXXXX and commenting and formatting with indentation etc.

One thing I am doing now is trying to pass some stuff onto others so I am heavily commenting and trying to ensure variables are declared at the top so that they are easy to see and modify.

This leaves me in a bit of a perplexed state however as I have one situation where I normally do not declare a variable at the top, and that is if its not a static value but instead a value that will be determined later in the script.  So in that case, is the best practice still to declare it early on with no value or just in the working part of the script?

My examples below are not great, because they do not really show the full effect of things like $hWnd and just other small items like checking for returns on a function, or a input-box etc.

 

Example 1 - Declare only static values at the top, let "working values" be declared later as they are used in the script. 

#Include <MsgBoxConstants.au3>
Local $sMsgTitle = "Important Question" ;Used as Title for our Message Box
Local $sMsgBody = "Would you like to continue the script?" ;Used as Body of Message Box

;Assume this could be way down in the script burried below

$iMsgBox = MsgBox($MB_YESNO, $sMsgTitle, $sMsgBody) ;Message Box User Must Answer Yes or No
If $iMsgBox = $IDYES Then ;If Answer is Yes Then
    ;Continue
Else ;If Answer is NOT Yes Then
    Exit ;Exit
EndIf

Example 2 - Declare everything and if its a "working value" just leave it blank.

#Include <MsgBoxConstants.au3>
Local $sMsgTitle = "Important Question" ;Used as Title for our Message Box
Local $sMsgBody = "Would you like to continue the script?" ;Used as Body of Message Box
Local $iMsgBox ;Declare varible to hold results from messagebox

;Assume this could be way down in the script burried below

$iMsgBox = MsgBox($MB_YESNO, $sMsgTitle, $sMsgBody) ;Message Box User Must Answer Yes or No
If $iMsgBox = $IDYES Then ;If Answer is Yes Then
    ;Continue
Else ;If Answer is NOT Yes Then
    Exit ;Exit
EndIf

 

I was trying to be more like Example 2, but after a while having a dozen variables that have no value and can not be changed at the top, I am starting to think its better to go with Example 1 so that only variables the user can/needs to change to alter the script are at the top and the rest is more of just for logic in the script.

This results in less lines of code and less redundant commenting, and if you have more than one variable doing the same thing like multiple message boxes or input boxes its just less confusing in my opinion.

So I couldn't really from searching find my answer, so figured I would ask.

Edited by ViciousXUSMC
Link to comment
Share on other sites

Just my opinion:

  • Technically these are all global variables
  • Only declare global variables if its required throughout the script, these should be declared at the top of your script.
  • Global variables should never be declared in functions
  • Local variables should only be declared in functions.
  • In Example 1 I would have used:
  • Global $g_iMsgBox = ... ;~ Outside of a function
    ;~ Or
    Local $iMsgBox = ... ;~ Inside of a function

    Here is another example showing what I mean, hope it makes sense:

#Include <MsgBoxConstants.au3>

Global $g_sMsgTitle = "Important Question" ;Used as Title for our Message Box, can be used within other functions _MsgBox1, MsgBox2
Global $g_iMsgBox ;~ Declare varible to hold results from messagebox until exit or re-declared
_MsgBox1() ;~ This will set the $g_iMsgBox global variable
Global $g_sMsgBox1 = $g_iMsgBox = $IDYES ? "Yes" : "No" ;~ Set global string variable "Yes/No" to be used in _MsgBox2() based upon $g_iMsgBox variable

Global $g_sMsgBox2 = _MsgBox2() = $IDYES ? "I meant to select Yes" & @TAB & @TAB : "I meant to select No" & @TAB & @TAB ;~ Sets the $g_sMsgBox2 variable based upon the result of _MsgBox2() function Tabs just to show MsgBox Title :)
MsgBox(4096, $g_sMsgTitle & " Answer", $g_sMsgBox2)

;Assume this could be way down in the script burried below
Func _MsgBox1()
    Local $sMsgBody = "Would you like to continue the script?" ;~ Used as Body of Message Box will not be retained after leaving the function
    $g_iMsgBox = MsgBox($MB_YESNO, $g_sMsgTitle, $sMsgBody) ;~ Message Box User Must Answer Yes or No
EndFunc

Func _MsgBox2()
    Local $sMsgBody = "Did you mean to select: " & $g_sMsgBox1 ;~ Used as Body of Message Box will not be retained after leaving the function
    Return MsgBox($MB_YESNO, $g_sMsgTitle, $sMsgBody) ;~ Returns the result of the MsgBox
EndFunc

 

Edited by Subz
Link to comment
Share on other sites

Also, it looks like you're confusing Static and Constant, see here for an explanation in the context of AutoIt.

Link to comment
Share on other sites

Yes I have seen the best practices page, that is where I picked up some of my good habits.

This is more a personal question to do with how the "flow" and "readability" of a script should be structured.

Let me post another example that maybe makes more sense.

 

Would you declare a variable $hWnd at the top of your script? or simply use it as needed for logic down in the script itself?

;100 Lines of code

$hWnd = WinWait("Title", "Text")
WinActivate($hWnd)

V.S.

Global $hWnd

;100 Lines of code

$hWnd = WinWait("Title", "Text")
WinActivate($hWnd)

Once you add comments and have 20+ variables like this it starts to really inflate the script to declare every variable at the top and when you comment what they do and why they are there as the #1 reason I put variables at the top for my scripts is that is the place I change things like name of a file, or regular expression for a string.  AKA that is my "working area" for things that are user input and not just logic for the script.

Link to comment
Share on other sites

Generally you declare variables as close to where they're first used as you can. Never call a function that uses a global variable unless you've made sure that you have declared it first, Au3Check won't find that type of error.

Try to avoid using Local in the Global scope, there's no difference between the 2, so use the appropriate keyword so that other people looking at your code know what you're doing.

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

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...