Syntax verification

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Syntax verification

Post by Lery154 »

On my form I have a button, then when pressed, does some action. For simplicity lets say it runs get-process.

I can create a function for get-process and instead on the Click action for the button, call that function.

I've seen this done in the samples.

Does that mean I should be doing it that way or does it not really matter?

Example:
PowerShell Code
Double-click the code block to select all.
$buttonhello_click={
get-process
}
or
PowerShell Code
Double-click the code block to select all.
$buttonhello_click={
gettheprocess
}
function gettheprocess {
get-process
}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Syntax verification

Post by davidc »

Either way is fine. It's a matter of preference.

David
David
SAPIEN Technologies, Inc.
User avatar
galaara98
Posts: 10
Last visit: Tue Nov 08, 2016 10:59 am

Re: Syntax verification

Post by galaara98 »

I tend to create variables and 'throw-away' functions only while debugging or teaching. In a known working production script I tend to inline, pipeline, and every other trick I can use to not create variables...

In my opinion it seems to lower the amount of RAM a script needs if you minimize storing any results of an action UNLESS you intend to repeatedly reuse something.

for instance generally I pipeline and allow auto garbage collection on as much as possible, but if I intent to execute multiple concepts against some object, I prefer creating a variable and storing the object there first.

(a case would be something like an exchange mailbox object if I intend to execute several command against it, rather than constantly using "get-mailbox", which each time it runs actually gets the mailbox object, again.)

so rule of thumb, if you intend to reuse, create your function, store results, etc. otherwise, go crazy with pipelines, inline, tricks, as much as possible [so long as its readable later, and you have a clue what you did :) ]

Aaron
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Syntax verification

Post by jvierra »

Aaron - nice dissertation but I fail to see how it hooks up to the original question. What was it about the question that sent you down this path?

I mostly agree with your thesis. If we use as many automatic variables as possible they are less likely get pinned in memory. I have also learned to not get too obsessive about this as it can lead to harder to manage scripts.

Like you I tend to be terse at the prompt and more disciplined when posting except when it is not needed.
User avatar
galaara98
Posts: 10
Last visit: Tue Nov 08, 2016 10:59 am

Re: Syntax verification

Post by galaara98 »

My thought process was based on the original post wondering whether to create a function to do something that can also be done on the fly.

My wandering expressions of solutions and philosophy are do to my ADD :) however the point was:

EITHER of the original styles would work...however I personally never bother to create a function unless I intend to reuse. I generally never set a return to a variable unless I intend to interrogate or change the object more than a couple times (or more than once if the creation of the object is RAM or time expensive)

Aaron

I wanted to add a PS here... there are a few times when I go overboard with variables, and that is when I am training or debugging. I often get very expansive when I want to show how something should work, knowing that I would get more compressed if my code went to prod.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Syntax verification

Post by jvierra »

And that was my warining: "Never say never"
.
Functions are legitimate to use for clarification of code. They are also brilliant at isolation of process steps. Functions are legitimate if you are careful to not overuse them. I have seen some wrap every line in a function.

We call it "Software Engineering" for a reason. Software including scripts need to be engineered whenever these will be persistently used.

PowerSHell as a replacement for CMD.EXE allso allows us to just do what is most comfrotable as long as it gets us through the day. I now mostly live at a PowerShell prompt.

When you posted "My ADD" did you mean your AD. Do you have and AD that I missed?
User avatar
galaara98
Posts: 10
Last visit: Tue Nov 08, 2016 10:59 am

Re: Syntax verification

Post by galaara98 »

ADD = Attention Deficit Disorder :) My brain wanders even as I think. it can be very handy when troubleshooting or designing a solution. Is sort of a downer when trying to communicate complex issues because my thoughts branch faster than I can type.

Aaron
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Syntax verification

Post by jvierra »

galaara98 wrote:ADD = Attention Deficit Disorder :) My brain wanders even as I think. it can be very handy when troubleshooting or designing a solution. Is sort of a downer when trying to communicate complex issues because my thoughts branch faster than I can type.

Aaron
My brain wanders because it just does that. It took me 40 years to be able to lead a meeting without wander off into never land.

In my youth they called it boredom. Now we feed everybody expensive drugs. I hope you got the better kind.
User avatar
galaara98
Posts: 10
Last visit: Tue Nov 08, 2016 10:59 am

Re: Syntax verification

Post by galaara98 »

I do uses some drugs because without it my ADD is so rampant even I personally cannot follow my own train of thought. But my Doc and I have set the dosage to be quite mild. In most cases I benefit from my thoughts constantly jumping to related topics. I am able to troubleshoot extremely rapidly because my own mind does troubleshooting chess several moves ahead on every possible track. I also find it enjoyable to theorize on solutions because again, without effort I can envision caveats, theoretical limitations, vulnerabilities to several proposed solutions at the same time.

Where I get into trouble is communication. I can't communicate what I am thinking fast enough before my thoughts have shifted and I come to new information... It makes talking with me very choppy because I often stop mid sentence and realize there is a better way. Only to stop mid sentence again because there is an even better way... My wife is used to it :) she just kicks my foot to stop me from rambling before I am done determine what I really want to say :O..

So personally I recommend to people with ADD, embrace it, let your mind flow, unless it overflows and drowns you...

just be "ADD", killing it with drugs is like poking out a 3rd eye... Why kill a gift unless it's hurting you ... but eh, that's just me.

Aaron
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Syntax verification

Post by jvierra »

Well ADD can be a bit of a life killer if it is too bad. If you can find a medication that helps and doesn't make you numb that is great.

I am glad they weren't forcing diagnosis w3hen I was in school. They would have anethatized me. My mind was way ahead and I could reqad in excess of 1700 WPM and do 4000+ in bursts. I woul not pay attention in calss but wqould read the textbook. The teachers hated that especisally when I would end up arguing with evrything they gort wrong. (I had very exellenjt reacll and always aced tests with no paying attention, no hoework and no studying. I constantly got 90+ on all tests.

I would have been medicated because I was a total distraction due to my inability to tolerate teachers who gave out misinformation, disinformation and damn lies.

Good luck and have a good wekend.
This topic is 10 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked