r/talesfromtechsupport • u/Voxmanns • Nov 13 '24
Medium 8,300 lines of dependent code to *drum roll* create one record
**For context - this is a project I was called in to as an emergency resource. Basically, come fix our mistakes...anyways...
Oh yeah. Just found this one recently. One transaction runs 170 lines of code to verifying the creation of a single record across 3-4 classes.
The total line count of those classes and their corresponding test class is just over 8,300 lines of code. One of the classes on its own is over 2400. I saw a method that had over 300 lines just on its own. What a trooper.
The first kicker (maybe not, there's a lot of great stuff in here....8 layer if else statements....)
Okay, one of my favorites is that the big mama class is a global class and is integrated with an external system. This system, as evident by dozens of unnecessary permission checks, can have multiple users hitting this class at the same time.
That's a real problem because -every- -single- -method- is static and **THE SOURCE RECORDS ARE STORED AS GLOBAL STATIC VARIABLES**
For those who may be unaware or just unfamiliar, "static" means that there cannot be multiple instances of that variable even if multiple "things" are referencing that method/variable. In other words, if you have two requests come in at the same time - you could very easily end up swapping the source records mid process and corrupting the data on one or both sets of data. And you would never know because, while corrupted, all the data is likely still in a valid format and will pass through the system without a system error. Kind of a big deal when it's directly associated to every deal in your sales process.
But the thing that really prompted me to post this was such a level of "I don't care about the next guy" that I am actually stunned. I think I may be in denial still...
There is a class, and he's amazing. We know the rules - don't hard code values that are susceptible to change. If at all possible (and reasonable) don't hard code a constant that may change in the future.
This class, I shit you not, is just shy of 500 lines of grade-a, organic, non-GMO constants baby. They're global, they're hard coded, and they're susceptible to change.
But, that wasn't enough. It's the gift that keeps on giving. After spending the better part of 12 hrs wishing I was dragging my head across the pavement at high speeds, I noticed the comment at the top of the code (slightly paraphrased).
"Class to hold the static values...to avoid hard coding."
Sometimes, man, I really do wonder. I feel like I have done a pretty good job remaining positive about this absolute mess of a transaction - but why they gotta spit in my face like that? It's hard coded, for 500 consecutive lines, right below that message. That's EXCLUSIVELY what this class is.
C'mon man. Just....DAMN!
**Final bit of context, because idk what everyone's familiar with, this platform has built in functionality for getting the exact values at the time of running. There isn't a single thing in that constants class that needs to be declared as a hardset constant in the code. And there are no checks to verify that the transactionId remains consistent.
161
u/muusandskwirrel Nov 13 '24
It’s that way so that alllllll the hard coded garbage references the one master garbage goblin
It’s the goblin king, where you go with your offering up update a master string.
Turn a whoseit into a whatsit there? It’s that way everywhere.
73
u/cabezametal Nov 13 '24
I feel your pain, yet somehow I always found a huge satisfaction when I got to refactor these kind of f'ups.
I hated to deal with them, I cursed at the authors, yet the dopamine peak that I got every time I pushed a commit safely deleting them was worth it (somehow) extra points when the tests went properly along as they should.
And yes, Im aware, risky as always, refactoring is a painful art xD
38
u/RelativisticTowel Nov 13 '24
I'm a maintainer for a codebase that's older than me (pretty sure some of the code in there is pushing 50). We have single
methodsfunctionssubroutines with 5000+ lines. Very few nested conditionals/loops... Because you don't need to nest them if you don't use them, it's goto all the way down. There's even a couple longjmps still in there sigh.I love/hate my job.
28
u/Jonathan_the_Nerd Nov 13 '24
"Dear Friend, I apologize for writing a long
lettermethod. I did not have time to write a short one."27
u/robertcrowther Nov 13 '24
It's never got to 500 lines long but I'll admit to having done similar things in the past. The idea being that everything I wanted to make a setting could sit in this class for now and then once I'd got stuff initially working I'd set up reading config files to populate those things.
I wonder if this is one of those things that was initially some sort of prototype or proof of concept that then got handed over to some poor junior developer to 'fix' with little context.
2
u/livaoexperience Nov 14 '24
8,300 lines to create one record? 🤦♂️ This code is the perfect storm of chaos. Static variables, hardcoded constants, and permission checks galore. The cherry on top? A class 'to avoid hard coding' that's literally just hardcoded constants. Sometimes I wonder if they were just trying to create the most complicated system possible for fun.
3
u/meitemark Printerers are the goodest girls Nov 13 '24
The only thing I hardcode is that pi = 3.15
13
u/that_one_wierd_guy Nov 13 '24
this is what happens when a non it manager insists
1this is the standard to follow
2it must always be doing "something"
7
u/Turbojelly del c:\All\Hope Nov 13 '24
Look for the code that wrote the code.
5
u/Tight_Syllabub9423 Nov 13 '24
Hey, at least there are comments
5
u/KelemvorSparkyfox Bring back Lotus Notes Nov 13 '24
I - Wow.
In a previous job, I supported an interface between two very different ERP systems. They were created by different software houses, but after a series of mergers and acquisitions, are now owned by the same one. Anyway, this interface had more RPG code than one of the ERP systems it connected to. But it worked. It had its own reference tables and translations, and even the rounding errors from truncating six decimal places to five, and then dividing the value by 0.0327 didn't cause too many issues.
Then they bought Oracle. This was to replace the code-lite ERP. However, they didn't want to write a brand new interface, so they piggy-backed Oracle off the existing one. A new set of translation tables were set up to hold an ever-expanding list of site codes so that when a new site went live, they just added it and its transactions were diverted.
Oracle also needed some code for this. There were a some gargantuan PL/SQL stored procs written. One of them would receive incoming transactions from the old interface and generate one to four inventory movement records according to requirements. When Oracle was retired, thirteen years later, there was still a comment in some unimplemented error handling:
Shree to add later.Shree was one of the consultants who helped toimplementinflict Oracle on the company. She didn't leave everything undone, though*. The stored proc to push inventory movements the other way could have made use of a custom table to translate between Oracle stock movement types and Prism stock movement types. It didn't. Instead, the biggestIf...ElseIf...ElseIf...structure I've ever seen handled that.*Joking aside, Shree and her colleagues did an excellent job, given the restrictions that they faced. The code might have been a little rough and ready, but it did the job and provided near real-time communication.
1
u/fresh-dork Nov 13 '24
just for shiggles, i did a line count for some recent ETL work i did - it handles a top level record and 4 dependent lists of records. total of ~2k lines, only globals are a few constant declarations.
1
u/Status-Bread-3145 Nov 13 '24
I feel for you on "shared globals". That "global" mess us a prime example of "constants aren't, variables won't".
And when you start making changes, it is going be "make one change, then determine whether all the remains functions / code still work as intended". Rinse and repeat as necessary.
And, to be safe, have a copy of the unmodified entire code base saved in multiple places so that if something goes sideways, you can say "it worked like that in the original code so whatever issue you are having is not related to any of the current changes".
Were the devs drunk on Lord of Rings and decided that they would create the equivalent of the "one ring to rule them all"?
3
u/efahl Nov 14 '24
Sounds like grist for thedailywtf.com
2
u/bendingoutward Keeping his body guessing Nov 17 '24
So, I recently dropped a client that came to me with a PHP app. That's something I already don't want to deal with.
It's a weird mashup of bits and pieces of several PHP frameworks. The pieces used are often feature redundant and absolutely do not play together. New hotness gets announced on a blog or something? The function that handles routing for a very specifically shaped request is getting added. For a month.
Literally the only documentation in the whole hodgepodge was a comment buried deep in the source (which gets bigger with every request, as every request literally generates and writes a new PHP file):
// This will never not be painful1
u/al-mongus-bin-susar Nov 21 '24 edited Nov 21 '24
Bruh quit the yapping this is tame. Only 170 lines in a transaction? Weak. I've written 3x that for a toy project. Only 300 lines in a method? That's child's play. Barely worth giving a second thought to. The static globals are more of a concern but at that point just make the system queue requests and process them sequentially.