This was in university. We had to create a desktop software that would sell tickets in an amphitheater. The closer you were to the stage, the pricier the tickets were.
At the end, we needed to present our project to the class. So I went up there with my teammates, showed the pricing feature based on distance from the stage, pretty proud of our algorithm which I implemented the previous night (of course).
At the end, the teacher points out that the pricing is wrong. The closer you are to the stage, the cheaper it gets. And the further back you are, the more expensive the tickets are.
I responded that it was because the presented shows at the amphitheater are always really bad. So you WANT to be sitting at the far back lol
This reminds me of a class I had in university where we got to program a UAV (underwater autonomous vehicle). It was a robot that looked like a torpedo and could dive underwater, though they removed the diving fins when the students programmed it, so it just scooted around on the surface.
It was in my first year, a project meant to get the students excited about real world applications. I programmed the 9 GPS waypoints in a for loop. It successfully navigated to each of them, then stopped at the last one, as it was supposed to. Went off without a hitch, right?
But then I started thinking... Why did it stop? I never told it to stop, I realized while watching it in the bay. Turns out it had incremented to the 10th waypoint, which didn't exist, and crashed the program with an array index out of bounds exception. I suppose that's one way to stop the robot 😆 Professor didn't notice the error, and since it had navigated successfully I got full credit
Reminds me I had to do an elevator at uni. Was provided a physical rig with frame, motor with slotted disc, and brake. We had to do the motor driver, sensors, and control such that it respected limits to the first 3 derivatives of position. Yeah not a typo, turns out the 3rd is known as "jerk".
Anyway we half arsed the physics - it looked smooth but it was all heuristic. But while demoing to the lecturer it backfired and slowed too fast, stalling the motor just before it reached the correct floor. I knew it wasn't going to move on until it decided it was there and that it wasn't going to fix itself.
But the guy was pretty impressed and started asking questions, and the guy he was asking was standing away from the rig. So I waited until I was out of his line of sight then reached up and spun the slotted disc until the brake clicked in.
This is actually how many professional concert theatres work. The seats right at the front have worse sound and a worse view of the orchestra, and they are the cheapest seats. I know this first hand because I just bought seats right at the front to a Seattle Symphony show lol
Wouldn't the fact that it is assigned to not trigger any unused warnings? I just figured it was because they declared it but didn't initialize it to 0.0.
It is given a value, that is never used anywhere, but my guess was that it didn't like that it wasn't initialized at the point of declaration. Everyone is saying it's an unused variable warning. This is C# right? Not too familiar with it, but with C and C++ compilers I remember using, the warning goes away if you even assign anything beyond the initial declaration.
That's Java. Honestly, I don't know how it works there. In C# it should cause warning about unused variable and, depending on IDE, a suggestion to merge declaration and assignment.
Somebody got caught in a floating point rounding error and tried to escape by all means, slowly losing his mind over the sheer absurdity of 2 + 2 = 3.9994
Well yeah, that's because money is fixed-point, not floating point. You can store it with an integer number of cents, instead of fractional amounts of dollars. But if you're doing anything measuring real values (like physics simulations), that's what floats are for.
Everytime I heard this I thought, well duh a float should never be used for pricing, why would anyone think that was a good idea? But I finally found someone lmao
Tbf it's better to store the absolute number and number of significant figures as different integers if the floating point math is going to be a problem.
Ignoring specific types existing for this purpose or using integers - double is generally considered a default floating point type in most fields (gamedev being a notable exception).
This was done in 2018, probably my first Java course, and probably one of my first programming course. Back then, no AI, only stack overflow. Having my code compiled was the challenge, I wasn’t at the point where I knew anything about clean code
several reasons. - it allows much cleaner readability and understanding if its uses in multiple places ( a.isSamePrice(b) is easily understood from a 'business logic' perspective vs 'Math.abs(price-this._price)< 0.01Math.abs(price-this._price) < 0.01' ) - it centralizes the logic into 1 place, which means that the exact same logic is used everywhere, instead of random differences like 0.01, 0.0001, different whitespace, or forgetting abs etc. - the centralization also makes it more searchable and easy to see all the places its used (if you have any half-competent IDE) - the centralization also makes it more testable as you can just test this one method and then have all the places using it verified as not being the trouble in any future code debugging
In any small / new codebase these benefits will be neglectable, but for any codebase that is medium size, and either worked on over a long period or by multiple people the benefits would feel obvious
In this instance - where "same" does not actually mean same, it means "close enough" - I would say that wrapping it in a function is a smart move. For example, the threshold may decide to be changed, either through policy, or something external like inflation causing pennies to be eliminated. Additionally, the logic might need to be tweaked, here are a couple of things off the top of my head:
Corporate might decide that tickets which are $10,000 and $10,001 should be considered to be the same.
Is free the same as a penny? What if you are buying 10,000 of them?
Finally, code should tell a story, and function names give you the prose to tell that story with. You can also use the function as a location to rationalise this behaviour for future explorers - or better yet, the comments in your test suite for this function can document the intent.
In this case, the "close enough" is because of floating-point fuzziness, not because of an arbitrary threshold. This is the typical way to check equality for floating-point: You subtract one from the other and use abs() to guarantee positivity, then check if the result is less than an acceptable "epsilon"
That said, it's actually too lax; it'll detect $6.10 and $6.09 as being the same price, for instance.
885
u/ChienChevre Apr 10 '26
Just to add to the story:
This was in university. We had to create a desktop software that would sell tickets in an amphitheater. The closer you were to the stage, the pricier the tickets were.
At the end, we needed to present our project to the class. So I went up there with my teammates, showed the pricing feature based on distance from the stage, pretty proud of our algorithm which I implemented the previous night (of course).
At the end, the teacher points out that the pricing is wrong. The closer you are to the stage, the cheaper it gets. And the further back you are, the more expensive the tickets are.
I responded that it was because the presented shows at the amphitheater are always really bad. So you WANT to be sitting at the far back lol
489
u/FrancisBitter Apr 10 '26
The “return false” is a fantastic punchline.
263
u/SchattenMaster Apr 10 '26
plot twist: all prices are unique
257
u/SchattenMaster Apr 10 '26
btw even a half decent IDE would flag the shit out of this code with the constant return and the unused variable
104
u/flofoi Apr 10 '26
double priceplease don't
37
u/Poat540 Apr 10 '26
Self documenting code
44
u/Sure-Opportunity6247 Apr 10 '26
Somebody got caught in a floating point rounding error and tried to escape by all means, slowly losing his mind over the sheer absurdity of 2 + 2 = 3.9994
12
u/limeyNinja Apr 10 '26
That feeling when you forget to replace the placeholder.
6
u/whoisrodi Apr 10 '26
return false. ok bro
25
u/DrCatrame Apr 10 '26 edited Apr 10 '26
I guess the question is if they will bedisplayedas same price (i.e. printed with "%.2f" format), in that case seems fine to me.EDIT: wops.. i didn't notice it's just return false
5
u/Ksorkrax Apr 10 '26
Computer says "no".
4
u/Sulungskwa Apr 10 '26
Official code they wrote in Office Space when they were trying to do that scam thing
3
u/kleetus_mactavish Apr 10 '26
Only if it's free would double the price be the same as price.
4
u/homerdulu Apr 10 '26
Plot twist at the end!
2
u/AdreKiseque Apr 10 '26
Kid named
==operator?1
u/yuehuang Apr 11 '26
Perfect for the optimizer to inline the function.
1
u/sur0g Apr 12 '26
Never use floats for counting money, kids
1
u/Latter_Brick_5172 Apr 13 '26
Why store the price as float in the first place? Why not as an int where each digit is a cent? T-T
1
u/MisterMeow35 Apr 14 '26
The code is literally: ah, fuck it.
1
u/Schmittfried May 06 '26
TDD, not even once.
1
u/microkool Apr 10 '26
Same same... but different... but still same.
1
u/Reithaz Apr 11 '26
This code compiles to: bruh.exe
0
u/Certain-Flow-0 Apr 10 '26
Decimal: Am I a joke to you?
0
u/Karmabyte69 Apr 11 '26
I feel like a 1 liner in this case is much more readable and the correct coding practice.
-11
u/LetUsSpeakFreely Apr 10 '26
0
u/[deleted] Apr 10 '26
[deleted] — view deleted comment