r/PowerShell • u/tsclapper • Jul 29 '21
What was I thinking?
Have you ever written a sweet piece of code that you were proud of? Have you ever looked at that code 5 years later and wondered what idiot wrote that shit code?
16
u/coldflame563 Jul 29 '21
Im fairly confident an abhorrent piece of power shell code that I wrote is in heavy heavy use by my former company, written almost two years ago. Dear lord if I only could do it now. It’d be so much better it’s absurd
11
u/tsclapper Jul 29 '21
The more you know, the dumber you feel.
8
u/coldflame563 Jul 29 '21
So I’ve moved on to management and I didn’t realize how far I’ve fallen. I looked at a devs code today and was just so lost. Sigh.
6
4
3
6
u/Aertheron01 Jul 29 '21
I have code in production at a previous company, it's so horrendous, it's from the time I started my first steps in Powershell.
I've had ex colleagues ask me for help fixing it when it broke, and I gave them some pointers and wished them much luck.
My biggest advice was: trow it out and replace it with something you wrote yourself so you learn Powershell and know how to maintain it :)
4
Jul 29 '21
I know for a fact many scheduled scripts I've written 5+ years ago are still in use today.
4
u/letmegogooglethat Jul 29 '21
At my last job they were using scripts and such from a person that hadn't been there in like 5 years. They didn't have the expertise or time to figure it out, so they just kept using everything until something stopped working, then abandoned it and did it all manually. It was such a shit show. I don't know how they survived.
29
Jul 29 '21
I like going back to my old scripts and finding the chunks that I obviously copied and pasted from Stackoverflow without knowing how it worked in the slightest.
13
10
7
u/fuzzylumpkinsbc Jul 29 '21
I'll let you know in 4 years! but yeah I am reminiscent of the time I wrote in notepad++ a line for every distribution groups I wanted to get
3
0
Jul 29 '21
[deleted]
2
u/fuzzylumpkinsbc Jul 29 '21
I don't know about that, once you're past 3 groups it's just easier to create an array and write the for
61
u/cheffromspace Jul 29 '21
“Omg, who wrote this? What a dumbass…”
CreatedBy: cheffromspace
“Fuck…”
18
u/ApricotPenguin Jul 29 '21
That's why you don't put in the author's name.
Saves you from getting an email from /u/cheffromspace several years later, asking you to support/maintain/explain your code :D
3
u/gonzojester Jul 29 '21
This!
I've told many a newcomer this and they never listened.
Ego alone will drive a man insane because the helpdesk will get their hands on that script and expect you to support all changes to it.
hahahahahahahaha!!
3
21
4
u/Ytrog Jul 29 '21
You know version control like git is a thing right? 👀
6
u/ApricotPenguin Jul 29 '21
The original file may be under VC, but when the script is copied to whatever server it needs to be running on, you won't see the git history there
4
u/ExceptionEX Jul 29 '21
I tend to put the git repo location in the header, so years later I can find where an updated version is or should go.
And often the sting of looking at something I would bitch at someone for being written by me helps with being sympathetic to my juniors.
5
u/ApricotPenguin Jul 29 '21
That's.... a good idea on adding the git repo location to the script. I might just steal that idea. Thanks!
1
3
Jul 29 '21
If I even do remember what I wrote, I'm happy to explain briefly for a frustrated future admin. If the company wants my time, my rate is $80/hr.
4
u/jerrymac12 Jul 29 '21
Im afraid to go back and see what kinds of things i wrote...i keep saying ill be proactive and update my old code....i tell myself that at least.
4
2
u/stone500 Jul 29 '21
I've been working one a large scale 2008 R2 Server migration project for the last year and a half. For my scope, I'm in charge of migrating all the servers that host file shares, DHCP, and printers for our retail locations.
I've only started learning powershell about two years ago now, so I consider myself a novice. I have a little over a dozen separate scripts that all help automate these migrations. Since I wrote this for this specific scenario and nothing else, they are an absolute mess.
I don't do hardly any error handling. None of them are built into functions (I don't quite know how to do those yet). I literally just learned two weeks ago how to remote using CredSSP so I can solve the second-hop issue. I have scripts in my folders that I don't even use anymore and I haven't made time to clean them up. I'm using VSCode and I don't even know how to use the debugger. I add rows to my arrays like an imbecile using += statements.
I have a loooong way to go before I feel comfortable calling myself "proficient" in Powershell.
1
2
u/Fallingdamage Jul 29 '21 edited Jul 29 '21
Sometimes its messy, but it still works. In my case I usually come back to an old script and now have a better way to do the same thing. The end result is the same, the script itself might be 1kb smaller when im done with it.
There was an instance a few weeks ago actually where I went to reuse an old script for something different. I made a copy and started hacking at it and got more and more frustrated. Like "How the hell is this even working??"
You know those times where a script starts out well and ends up just being a PS1 full of partial scripts and 1-5 liners that you used to layout pieces of the actual script that you're working on? - Yeah, basically I opened my 'scratch paper' and then spent 20 minutes asking myself why I would ever write something so incoherent.
1
u/tsclapper Jul 30 '21
I remember when I learned how to use a hash table. I increased my code efficiency so much it makes my head spin. Not sure how that’s relevant to your comment, but that’s what popped into my head.
9
u/hbkrules69 Jul 29 '21
I like looking at 10 lines of vbscript that can now be done in a single PowerShell command
9
u/jpochedl Jul 29 '21
Yep, 10 lines of VBScript to one PS command.... and 40 lines of VBScript are replaced by three or four PS commands piped on a single line....
7
Jul 29 '21
That applies to so many things, try advent of code with PS and you often end up with crazy short solutions compared to other languages.
9
u/landob Jul 29 '21
I haven't really looked at it and wonder why the code was shit, but I have looked at it and go...you know....there was a way easier way of getting that done....
5
4
u/Volore Jul 29 '21
In the beginning I was just so amazed by the idea of automating anything.. I used a lot of x,y coordinate mouse clicks...... those scripts are painful to look at.
4
3
3
3
4
2
2
u/Soopercow Jul 29 '21
Complete opposite for me, I code less than I used to and scripts I wrote a while ago make me feel like Captain America looking at the helicarrier computer.
2
u/uptimefordays Jul 29 '21
"I've automated another routine task!" me.
"All your code is pretty much the same, isn't that interesting?" also me.
For real though this is totally normal and a good sign you're improving.
2
u/packetheavy Jul 29 '21
When you go to copy paste a function you wrote a month or two ago for a new script and end up rewriting the whole function to make it shorter or more efficient.
Also my own lack of comments: so much ‘what is this?’ or ‘how the heck does that even work?’ when I read back through my stuff.
2
u/get-postanote Jul 29 '21
Heck, one can look at the code they wrote last week... Same reaction. Each earlier year, is well, u know.. ;-}
I still have my old Cobol/Fortran code, and all I can say was, wow! WTH ;-}
2
2
u/TheGooOnTheFloor Jul 29 '21
I found out that some code I wrote 22 years ago is still in production. Since then I've changed companies 3 times and learned a couple of new programming languages (and smarter ways to do things). I cringe to think of what that code looks like compared to what I've done since then... (Under the terms of the employment then I couldn't take a copy of the code when I left, but I know I did things the hard way.)
But on the flip side, did you ever look at something you wrote 4 or 5 years ago and think "Wow, how did I come up with that solution?"
2
u/BuriedStPatrick Jul 29 '21
You know, I've never had that exact reaction to my old code. It's not like I don't have bad code out there and routinely run into it. It's just that I then remember what my options were back then in terms of technology and knowledge, and I usually just think to myself: "oh shit, I can improve this bit now that always bothered me!".
Now, if this trait could carry over to my personal life I'd be all set!
2
u/bofh Jul 29 '21
I’d be worried if I (or almost anyone else) didn’t feel that way. You should be learning new things in 5 years…
2
2
u/williamt31 Jul 29 '21
5 years? Hell I'm doing that now from stuff I wrote 3-6 months ago lol.
But then I only recently had the opportunity to use, learn and practice.
1
u/schuchwun Jul 29 '21
I wrote some python script to generate a BB engineering code to unlock hidden features. How the fuck did Britney Spears lyrics work?
1
9
Jul 29 '21
[removed] — view removed comment
7
u/Aertheron01 Jul 29 '21
Yes, I know what you mean.
But it doesn't have to be uglier. I've learned 3 things, so others understand better.
Make functions
Never use aliases
Add comments and comment-based-help
Especially functions help to keep code readable. You often just have to explain what it does, and give it a descriptive name.
1
0
Jul 29 '21
[deleted]
1
u/Aertheron01 Jul 30 '21
That's what comments are for :) To explain weird behavior to the uninitiated.
1
u/Lee_Dailey [grin] Jul 31 '21
howdy spikeyfreak,
something like the below, perhaps?
$Test = '123456789' $ScrambledTest = -join ($Test.ToCharArray() | Get-Random -Count $Test.Length) $Test $ScrambledTestoutput ...
123456789 294176835take care,
lee2
1
u/tsclapper Jul 29 '21
Oh heck no. I love writing a sweet piece of code and using it as a teaching aide. I work hard to make sure I don’t use aliases, they just make it unreadable to new coders.
128
u/the_alabama_hammer Jul 29 '21
I think you misspelled 5 days.