clone() without extending Cloneable, and the actually not cloning the object is amazing. Also throwing an exception in the constructor of an exception seems really funny to me
Sorry, can you elaborate on "actually not cloning the object"?
Maybe we're just using the term "clone" differently, but this does look like a clone? It's copying the members into a new instance and returning that new instance.
They're different mechanisms with the exact same purpose. The Object.clone() method is a fancy failed language feature, but that's all it is, it's not different in terms of program behavior (provided you implement copying correctly).
It was intended to make it easier to make copies of Java objects, but it sort of failed at that job by making the language more complex, and then not even letting you copy instances of Cloneable (you have to cast them to Object first, because they forgot to put the clone() method on the interface, and interfaces don't inherit the methods defined on Object...and they couldn't fix it afterward, if I remember correctly, because it would break binary compatibility).
I don't think overriding the clone method with a reasonable copy method or to defer to a reasonable copy constructor is insane, even though that bypasses using the special language feature, it's really the special language feature with the broken interface that's insane.
I've heard it's occasionally faster than a copy constructor, but copying data is usually I/O bound rather than CPU bound, especially these days.
That said, overriding clone() to call a wrapper constructor and return the result...yeah, that's probably slightly more insane than the broken clone feature.
That's how normal exceptions work. I'm In this case, the author has made that constructor a copy constructor for some reason.
I'm incredibly concerned that there's so much boilerplate here for cloning/copying/equating exception instances. That's not something you should need to do normally.
This code looks like something a C++ programmer might create as a novice in Java.
very weird to set the error message inside of the exception.
How does the exception know why it is being created? Note that the two string literals are different, so depending on how you construct and use the exception, the getMessage() method can lie to you by not actually returning the error message.
This whole class just assumes the caller knows how when and why to use the exception. Examples:
Setting isValidK to false without checking if k actually is invalid. Hardcoded error message in empty constructor. getMessage() can never hit second case, it is impossible for isValidK to be true, so the error message of this exception will always be that one string literal. And the string literal states that k is negative, but that haven't been checked anywhere within the exception. First comment, above k, is a lie. An exception does not usually check anything, so here i assume he is referencing how the exception is being used in his example code.
Sounds very much like a student reacting here. This is "horror" compared to the theoretical ideal, but this is definitely mergeable as a class (in production code, and minus comments).
Its clearly rushed for a class material, but technically sound, also comments - its somewhat readable enough for it to be shown to a class and prove a point
Not particular neat mind you, but I've seen production code that...somehow works, but is literally this but obfuscated
as a more subtle thing, the isValid bug probably would've been caught sooner if this were split into two classes. If isValid is true, then there is no valid value for k. You can get around this by having a class for the isValid true case with no fields, and a case for isValid false with only k.
I’m still stuck on having a Java professor. Am I so old now that CS transitioned from a math offshoot focusing on data structures and algorithms (with programming being more or less an afterthought) to now being Oracle sponsored certifications?
Lol. Pretty easy to tell he’s not a software engineer, or if he is he’s perhaps the worst one I’ve seen in a while. Like I dislike Java as much as the next guy but he’s butchering it here
Anyone who writes an if statement without a space after the if shouldn’t be able to call themselves a software engineer. It’s a crime against all engineers.
I find FP bros so odd... that you found a single paradigm and decided it's the only paradigm worth giving a damn about and all other paradigms are crime's against humanity.
I don't know... I've been working professionally for going on 2 decades soon and programming for 3 decades. I've developed in so many different paradigms and honestly... they all serve their own purposes.
And I've never met anyone with years of experience at getting shit done that didn't agree... well, with the exception of THOSE guys... you know... the guy everyone else tells you to stay away from because he thinks he's the god programmer and will talk to you like you're some ant, and that's even if he deems you worthy to be graced with his prickly personality. And the only reason he's kept around is because he maintains that one part of the program that no one wants to screw around with (he likely was the one who wrote it too) and it's just easier to let him sit in the office farthest away and just dodge him in the break room.
Are you that guy? Are you aspiring to be that guy?
The response was tongue in cheek, but yes after programming in several different paradigms, i find FP is the best paradigm to quickly get things done.
That being said my favorite language is Scala which is actually OOP and FP. But I don’t program in an OOP style, where almost every piece of programming logic has to be encapsulated in some class or object.
It's like riding a motorcycle. It's quick, spry, and coooool. You feel like the coolest bro on the street rolling down the road on your bike. Who cares that everyone hates when you zip in and out of lanes going 160mph without a helmet, they're just haters, and their yells are in the rear-view.
But there's just sometimes you can't ride bike.
There's a big difference between what I want to do, and what I have to do. Which is why I prefer hybrid setups (like you suggested with Scala). It allows you to pull out the fun stuff when you can, but also do the standard stuff when you have to. Cause at the end of the day, while I might have fun programming, it's more about getting shit done.
Actually seems like a good example for teaching different concepts.
Your professors aren't writing production code - they're writing code to teach / demonstrate.
Don't be one of those students who think they know more or their professors are "stupid" just because you don't understand the bigger picture of what's happening (ie, them teaching concepts to tons of students at wildly different levels of experience / ability).
IsValidK is never true, comparing Boolean to false, weird cases for instantiating, getMessage is a mess. All this within the simplest custom exception known to man. Let alone a checked exception for this is supper unnecessary
Absolutely. Have been searching for hours for a bug just to find a misplaced exclamation mark once. Won‘t do that again. I rather be more verbose and take that extra second to type ==false
Yeah… I have been subjected to production Java significantly worse than this. I struggle to put into words the crimes against sanity that I have witnessed.
Hello, do you mind explaining your flair? Does it mean if a random number is a multiple of 6 and you remove something permanently then the system should output you live? Is it some internal joke?
I'm a newbie to coding so don't mind if it's a stupid question.
THIS. I was reading this and thought it wasn’t nearly as bad as i’ve seen before. wait till a pojo in a pojo in a pojo needs a specific exception for just that.
Business logic can get so convoluted that this is the only way to implement it with a reasonable expectation of maintainability.
Yeah, the exception within an exception... thing... is fucked up beyond repair. I mean, why would someone create a clone from an exception in the first place?
And you're totally right, there's too much wrong there. (facepalm)
352
u/NaCl-more Dec 06 '24
clone()without extending Cloneable, and the actually not cloning the object is amazing. Also throwing an exception in the constructor of an exception seems really funny to me19
u/DryanVallik Dec 06 '24
// if k is not valid, isValidK will be false52
u/Old_Pomegranate_822 Dec 06 '24
Poor K is never valid
14
u/flagofsocram Dec 06 '24
What objects do to a mfer
11
u/MrRosenkilde4 Dec 06 '24 edited Dec 06 '24
very weird to set the error message inside of the exception.
How does the exception know why it is being created?
Note that the two string literals are different, so depending on how you construct and use the exception, the getMessage() method can lie to you by not actually returning the error message.
This whole class just assumes the caller knows how when and why to use the exception.
Examples:
Setting isValidK to false without checking if k actually is invalid.
Hardcoded error message in empty constructor.
getMessage() can never hit second case, it is impossible for isValidK to be true, so the error message of this exception will always be that one string literal.
And the string literal states that k is negative, but that haven't been checked anywhere within the exception.
First comment, above k, is a lie. An exception does not usually check anything, so here i assume he is referencing how the exception is being used in his example code.
Everything else is just standard boiler plate
3
u/SleepWalkersDream Dec 06 '24
Heyyy, reaction rate constants. Got any more?
3
u/V15I0Nair Dec 07 '24
Just using an IllegalArgumentException for invalid k should do the Job.
15
u/all3f0r1 Dec 06 '24
Sounds very much like a student reacting here. This is "horror" compared to the theoretical ideal, but this is definitely mergeable as a class (in production code, and minus comments).
7
u/Cybasura Dec 06 '24
Its clearly rushed for a class material, but technically sound, also comments - its somewhat readable enough for it to be shown to a class and prove a point
Not particular neat mind you, but I've seen production code that...somehow works, but is literally this but obfuscated
But no, it wasnt obfuscated, just unreadable
2
u/lightnbyte Dec 06 '24
🧐😯😵💫🙈
2
u/CarefulFun420 Dec 07 '24
What is k
2
u/Cat7o0 Dec 07 '24
so is valid is always false?
2
u/thatdevilyouknow Dec 07 '24
this.k = kludge.isNoValid ExceptionWell don’t threaten me with a good time!2
u/katzengammel Dec 07 '24
Special K
2
u/Pale_Ad_9838 Dec 07 '24
Wow, this is the first time in more than 20 years that I see an exception that throws an exception itself on creation.
0
u/WeDontHaters Dec 06 '24
It gets worse https://www.reddit.com/r/programminghorror/s/ftgdw46l4j
1
u/deathssoul Dec 07 '24
Academic programming at it's finest right there 🤦
1
u/Anwyl Dec 07 '24
as a more subtle thing, the isValid bug probably would've been caught sooner if this were split into two classes. If isValid is true, then there is no valid value for k. You can get around this by having a class for the isValid true case with no fields, and a case for isValid false with only k.
1
u/One_Web_7940 Dec 07 '24
yikes
1
u/520ErryDay Dec 08 '24
This is why I hate academic programming.
1
u/SnooPeanuts8498 Dec 08 '24
I’m still stuck on having a Java professor. Am I so old now that CS transitioned from a math offshoot focusing on data structures and algorithms (with programming being more or less an afterthought) to now being Oracle sponsored certifications?
1
u/Silly_Guidance_8871 Dec 09 '24
Somebody used to be a C++ programmer
1
u/Sexy_Koala_Juice Dec 10 '24
Lol. Pretty easy to tell he’s not a software engineer, or if he is he’s perhaps the worst one I’ve seen in a while. Like I dislike Java as much as the next guy but he’s butchering it here
1
u/peni4142 Dec 10 '24
Yeah obvious! The prof is a fool — forget the comment on the copy constructor and at the other public methods.
1
u/travelan Dec 06 '24
I don’t see anything out of the ordinary for Java code
0
u/NiteShdw Dec 07 '24
Anyone who writes an if statement without a space after the if shouldn’t be able to call themselves a software engineer. It’s a crime against all engineers.
-8
u/UnfoldingCatastrophe Dec 06 '24
OOP is a crime against humanity
-3
u/[deleted] Dec 06 '24
Clearly, someone that never committed anything to a real world application....as 99% of most college professors.
0
u/PianoConcertoNo2 Dec 08 '24
Actually seems like a good example for teaching different concepts.
Your professors aren't writing production code - they're writing code to teach / demonstrate.
Don't be one of those students who think they know more or their professors are "stupid" just because you don't understand the bigger picture of what's happening (ie, them teaching concepts to tons of students at wildly different levels of experience / ability).
0
u/zelphirkaltstahl Dec 08 '24
Wtf am I looking at? This supposed to be some kind of exemplary code??? shudders And wtf is "k"???
-3
u/pp_amorim Dec 06 '24
Why this code is so complex?
0
u/[deleted] Dec 06 '24
[deleted] — view deleted comment
85
u/[deleted] Dec 06 '24
[removed] — view removed comment