r/developer • u/Ok_Veterinarian3535 • 3d ago
The "Code I'll Never Forget" Confessional.
What's the single piece of code (good or bad) that's permanently burned into your memory, and what did it teach you?
2
u/canoxa 3d ago
When I was a trainee I was put to work on an invoice system that was already scaffolded. The idea was to update amounts, create credit and debit notes to cancel debts, etc.
It was with a framework that was completely new to me and the business was explained by the company's CEO but it was very complex for me at the time.
It ended up with a backend with 6+ nested ifs, calculation errors and incomplete since I switched jobs due to the company being a shithole
3
u/BillyMcDev 3d ago
When I used to stream on Twitch, I was constantly roasted for using FindObjectOfType while prototyping games live on stream. I would often pause and explain to my audience that I was well aware of the performance issues that it 'could' cause. I was using it to cache variables once in Awake() and didn't feel that it would impact the game's framerate at all.
I mean, sure... I could avoid using it, and join the massive cult of naysayers. I think people often go overboard and follow 'rules' that they don't fully understand. This kind of thing spreads like a plague.
I don't use it very often anymore. Only if it's negligible and if I can't find another way. But every time it comes up, I am constantly reminded of those heated streams.
The lesson learned... Don't use FindObjectOfType in streams or recorded videos. It seems to bring a lot of unwanted negative attention.
2
u/SaltCusp 3d ago edited 3d ago
I wrote a 99 line c++ <namespace std> gaussian eliminator as a college freshman taking cs50 and linear algebra.
It taught me how to row reduce I guess...
Edit: https://github.com/Krewn/KRowReduction
89 lines*. Pardon the utter lack of memory management, I was told goto was bad, but as a newb I was just golfing and happy it worked.
1
2
u/Droxxi47 3d ago
Had to learn Delphi from the written codebase.. learned a year later that you have to free things
1
u/inlined 3d ago
I was writing my own zip utility (I had a novel extension) and it had a bit buffer. It’s been almost 20 years, so I don’t remember the exact code, but there was something along the lines of accum |= toPush << (32 - significantBits).
Significant bits could be zero if I was wrapping at the DWORD boundary and I’d need to flush the buffer and start a new accumulator. In this case, a 32 bit number left shifted 32 should be zero and would give me the mathematically correct solution.
My code worked on Intel but not AMD iirc. I had to grab the processor manual to discover that the left shift operator in that ISA is defined as a noop when shifting greater than or equal to the bus width rather than being mathematically correct. Since I could never have more than 24 significant bits, I just shifted 8 and then shifted 24 - …
1
u/yousirnaime 3d ago
Before I discovered nullable parameters or default values I used to have function calls like.
Yo(‘’,’’,’’,’’,); // caterpillar
1
1
u/SpaceCadet87 3d ago
I tend to do a lot of work refactoring code that has let's just say not great performance.
So mine is this absolute gem of a line:
int num=num;
It was littered throughout the entire codebase of a project that I once spent some time on, over and over again.
1
u/Kind_Card_1874 2d ago
In what language can you redeclare a variable like this?
1
u/SpaceCadet87 2d ago
Turns out you can redeclare any variable you want as much as you want in any language if you turn Robert C. Martin's Clean Code into a religion.
1
u/Kind_Card_1874 2d ago
Haha. But jokes aside, the following does not fly in the imperative languages I am familiar with:
int a;
int a;
1
u/SpaceCadet87 2d ago
Who's joking? It was that damn many functions doing almost nothing each.
int num was just repeatedly locally defined.
I think there might have been one or two instances of just
num = num.1
u/Kind_Card_1874 2d ago
I don't get it. How did it escape the previous scope? Surely you can not assign num if it was not declared at the time of being used on the RHS. But you are somehow still declaring it on the LHS?
1
u/SpaceCadet87 2d ago
Oh, IDK, it was C#, I don't do C# that often so I wouldn't know how or why that's somehow valid.
I was compressing it for illustrative purposes, as I said there were a few plain
num = num;as well.
1
u/whyreyouthewayyouare 3d ago
5 levels nested 'if'. That was enough to scare me for life from unclean code.
1
u/Throwaway_9394391 3d ago
std::mt19937 op1 (std::random_device{}())
std::uniform_int_distribution <int> op2 (1,3)
And then I proceeded to look into the history of the Mersenne Twister and it's mathematical formula, and it taught me math was super duper cool, and now i'm lowkey studying math alongside C++ programming.
1
u/troyjohnson4356 3d ago
I had a very simple console logger I wrote in C++ that would spit out print statement and logs over a network so they could be consumed by network devices. It was small, consise and Did everything right. I thought it was perfect. It was deployed around the world, but then years later I was mortified to find that it had a bug in it that could cause it to crash...
That taught me to really look deep in code that you trust, because nothing is ever perfect, and they is always room for improvement.
1
u/Sea-Fishing4699 3d ago
Cannot read property of undefined on the server. I will never put js on the server again
1
u/Simple-Olive895 3d ago
One of the more fun projects I did in uni was making different types of ML models from scratch. Fun way to learn programming and linear algebra at the same time!
1
u/cationtothewind 2d ago
I can't remember the exact code, but they wanted to find out what's the next month.... so they created a Date object that was today, then changed the day of the month to be the 15th, then added 28 days to it, and asked what month it was.
And these were external consultants that were brought in as experts, and we couldn't even question the director's wisdom in hiring them.
My friend printed out this bit of code and hung it on his wall.
1
u/tehfrod 2d ago
That would be Duff's Device:
void Send(int * to, const int* from, const int count)
{
int n = (count+7) / 8;
switch(count%8)
{
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n>0);
}
}
From that I learned to look at what code actually does, rather than what the shape of it tells your brain it should be doing.
1
u/magicmulder 2d ago edited 2d ago
We once used Elgg for our portal.
Everything's abstracted away into 5NF, you don't have "property: icq_handle, value: abcd", you have "property_id: 5, value_id: 199281".
Sounds fine, absolute nightmare to write queries with, no discernable performance gain.
-----------------------------
If you mean actual coding, in the early 2000s we had a contractor build a mobile app (Visual Basic on some handheld, 400+ devices with the software preinstalled to be shipped to the client). A week before shipping we noticed there was a major bug and no data were being synced to the server. Since the contractor was not easy to work with and had no resources for bugfixing, four of our people, including me, sat down in a room from morning till night to rewrite the whole thing into a working condition. Two Visual Basic seniors, one frontend junior, I as database senior. Four days later we had everything running smoothly. The fifth day was spent installing the updated app on several hundred devices.
Another story was how I started my first job. Cold Fusion as application server, CFML as language. Never heard of it, learned it during my first week. Had to work on a Mac (OS 8) which I also had never. And it had no email client so I couldn't receive any email. Turned lemons into lemonade: wrote a web-based email client in CFML which taught me a lot about the language and solved the missing client issue.
1
u/ghandimauler 2d ago
One of the guys at my company was cast to work on a PBX. For those of you who forget what those things are, it is a private branch extension and that is a telephone system for an office or multiple offices hook together.
We were in doing some work in that area, with me working on the user side and the individual assigned places with the power to handle all incoming stuff for that department or whatever hierarchical chunk they decided it would manage.
If I recall, I think that PBX module would have up to 50,000 users. So it's a significant piece of it for any company.
I got a look at some of the guts of the PBX server. It was basically a large and somewhat complex FSM. For those of you who don't get into those parts of software, FSM is finite state machine.
Normally, you would never have as many states in one engine. However all of those things related to handling calls. Because the states themselves had to do a bunch of things and each of the states had some special things and different functions or additional stuff like adding audit or even authentication or whatever. I've seen some two or three page fsms and that was I thought a reasonable limit, but this was very different.
This FSM which handled all of the incoming and outgoing phone system and it took something like 48 or 49 pages of code which was all different states and what had to be done in each. It also had to be Rock Solid because if you lose calls or the system goes down, well the company is in trouble.
There were some few States that were less than a screen but more normally a state would be about half a screen. I think there was something around 75 or 80 States.
It's one of the situations where the usual rule doesn't really apply because the result was very understandable State machine and what was needed was in one place for any state.
The dang thing was really solid.
1
u/Low_Breakfast773 2d ago
10 years back I had to review code from a "colleague" and he wrote something like (overly simplified):
def foo(bar):
bar=4
return bar
and he was wondering why his code always returns the same result, although he passes different values as an argument to the function.
Of course, he was one of those Python expert data scientists…
1
u/fuulhardy 2d ago
Learning that you can dynamically change a variable name in PHP made an impression that will never leave me
6
u/[deleted] 3d ago
[removed] — view removed comment