Sign in to follow this  
Followers 0
ChrisL

Best Practice?

9 posts in this topic

#1 ·  Posted (edited)

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

Share this post


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


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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.

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

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


[center]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.[/center]

Share this post


Link to post
Share on other sites

I think the Best practice is using both methods. For example parameters that doesnt change - Globals. For frequently changing pars use parameter passing.

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0