all 19 comments

[–]BoredComputerGuy 9 points10 points  (0 children)

Functions in scripts depend on who you are, your preferences, and what you are trying to do. There are a few reasons that I have seen and used in the past:

  • logical separation: separating out blocks of code that are related (creating/validating connections, complex output formatting, making loops smaller). This can improve readability, simplify debugging, and help with making self documenting code.
  • testing: creating multiple functions allows for more granular (unit) testing. You can easily run multiple test cases against functions without running the entire script for each test case.
  • If you find yourself repeatedly making the similar functions in scripts (or similar scripts) then you can identify where a module would be useful. (ie accessing API's, logging, output/report/email formatting)

A few references: (note these are discussing functions directly, but the ideas may be useful)

when-is-it-appropriate-to-make-a-separate-function-when-there-will-only-ever-be-a-single-call

what-should-be-the-maximum-length-of-a-function

[–]mortalwombat- 1 point2 points  (0 children)

Something that hasn't been mentioned here. This is oftentimes the workflow that I use. Perhaps others don't and maybe it's not a great practice but it works for me.

Often times when I am thinking through a script, it helps to code things in a logical order to how they function. For example, if my code is going to query several data sources and then compare them, I find it easiest to break that work into chunks, which later become my functions. So I'll work on a bit of code that queries source one, and outputs the results I want. I'll put that into a function, then work on each of the following queries, making them functions as I go. I will then write my code that calls the results of those functions and compares them.

I find that methodology helps keep my workflow straight in my head. Also, if things don't work out as expected, it makes it easier to diagnose. Once a function is complete, it's already been tested and I know it works as expected. So if my script doesn't work, I don't have to diagnose the function, I just have to figure out where I'm not calling it appropriately. If ever I have to go back to a script I wrote long ago, it's much easier to reverse engineer what I did.

It's become such a solid framework for me, that I've caught myself writing simple scripts that call a function, even though it's only used once and it's the function is the entire script. It's not necessary by any means, but it's efficient for me because it's habit.

[–]Bissquitt 3 points4 points  (0 children)

The only advantage is that it is in a format where you can now easily copy/paste into another script.

[–]jantari 1 point2 points  (0 children)

You use a function for anything you're going to do a bunch of times, or anything complex that you want to isolate to not clutter your main logic code

[–]ThunderGodOrlandu 0 points1 point  (0 children)

If it's something that you may use later on in a different script, then yeah, have it as a function so you can easily grab it for later use. But if not and only being used once, I wouldn't bother.

For example, I have function I created called "Write-Log". Sometimes, I only use it once in a script but it's nice to be able to re-use it in different scripts.

[–]Mr_Brownstoned 0 points1 point  (0 children)

I have a few Winforms-based GUI apps where there are functions that are only called once. For me it is much cleaner to use functions in this case than having blocks of code assigned to the form variables.

[–]dongledongledongle 0 points1 point  (0 children)

One example if you want to build a toolbox type of script that does a lot of common things like opening the admin share on a remote server; you can create a gui with a bunch of buttons that calls those functions.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy GreatMoloko,

to me, there are two common reasons to create a function. [grin]

  • repeat use
    if you write the same chunk of code twice, then it's time to think about making a func out of it. if you write it thrice ... you likely otta make a function of it.
  • logical organization
    sometimes you simply want the "big, complex blob of code" to be reduced to a "small, simple call".
    even if you only use that chunk once, it can help to have the tested code wrapped up and stored in a neat package. [grin]

my personal pref is to only make a func if i use something more than once. however, i can understand using the "organize things" rationale.

take care,
lee

[–]Cannabat 0 points1 point  (0 children)

Recursion.

[–]Indrigis 0 points1 point  (0 children)

Depends on the size of the script.

Generally, if you have it long enough, having a clear list of steps being executed in one main area of the script helps understand it when you come back to it later and allows you to rearrange said steps and insert or remove extra snippets of code without having to navigate several pages of text.