Jump to content

Best Practice?


Recommended Posts

What is the best practice when calling multiple functions that pass values around to each other.

Would you call each function and pass in the parameters?

Or set Global Variables and change them?

So

Func MyFirstFunc($var)
 ;Do stuff with $Var
         MySecondFunc($VAR)
Endfunc

Func MySecondFunc($iVar)

    Msgbox(0,"",$iVar)
EndFunc

Or

Global $iVar

Func MyFirstFunc()

 ;Do stuff with $iVar
     MySecondFunc()

Endfunc


Func MySecondFunc()

     Msgbox(0,"",$iVar)

Endfunc

The reason I ask is I have a script which is getting rather complicated passing information around, I was thinking of changing it to use Global values but I wanted to know if it's concidered bad practise

Edited by ChrisL
Link to comment
Share on other sites

I like passing on the parameters, but I guess it is a personal preference. Some people like to declare all variables at the beginning and use them throughout their script.

It is a person preference but I think it is far better to avoid global variable because it's safer. For example take your example using a global variable

Global $iVar
 
 Func MyFirstFunc()
 
;Do stuff with $iVar
      MySecondFunc()
 Endfunc
 
 
 Func MySecondFunc()
 
      Msgbox(0,"",$iVar)
 
 Endfunc

It works fine. Then over a period of time you add code to both functions

Global $iVar
 
 Func MyFirstFunc()
   $Firstcal = function3($iVar)
;Do stuff with $iVar
      MySecondFunc()
  $secondcalc = function4($iVar);this might not give the expected result because $iVar has been changed by MySecondFunc.
 Endfunc
 
 
 Func MySecondFunc()
   
      Msgbox(0,"",$iVar)
     $iVar += 13
 Endfunc

Using Global variables makes maintaining or altering code a lot more difficult IMO, but it might make it easier to write in the first place.

But I do find it difficult to avoid using global variables, especially when I use OnEvent mode and functions used for GuiRegisterMessage.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Cheers guys

martin you almost hit my problem on the head.

I have to keep adding to the functions, so what started out as passing 3 parameters is now up to 10

One of the functions is recursive so it passes the 10 paprameters back to the function when it finds a subfolder.

I'm going to re-write the functions and I was thinking shall I use Globals or keep passing parameters, after thinking about it I reakon I can simplify it without using Globals and out of the 3 replies that seems favorite.

Interesting point about onEvent, fortunately this script doesn't need a gui but I favour onEvent mode for gui's but I've never needed to pass in loads of parameters! (Yet)

Link to comment
Share on other sites

  • Moderators

"Managed" Globals aren't necessarily a bad thing.

But I certainly (If could be avoided, and I haven't had a situation yet that it couldn't) would use parameters myself, keep the variables localized, and let them be discarded as need be.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

ChrisL,

You mentioned you had up to 10 (expletive deleted!) parameters to pass. I used to use arrays when I had a lot of parameters to pass (only a mere 7 in the worst case) but I have now started using the Dictionary object method used by aGoriila's Hash.au3. Only one parameter to pass and you can use descriptive "keys" for each parameter rather than trying to remember which index number was what.

I recommend the UDF wholeheartedly - and as you can see it has a good pedigree! ;-)

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

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