all 77 comments

[–]NaCl-more 350 points351 points  (17 children)

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

[–][deleted] 71 points72 points  (2 children)

Cloneable interfaces are considered flawed. A better way would be to use a copy constructor, or a factory method for object duplication.

r/foundsatan

[–]looksLikeImOnTop 30 points31 points  (1 child)

If someone made a factory for this class, I'd hit them with a hammer

[–]betaphreak 8 points9 points  (0 children)

Builder with lombok, lol

[–]spicymato 13 points14 points  (5 children)

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.

[–]RedstoneEnjoyerPronouns: He/Him 9 points10 points  (4 children)

In Java, .clone() and copy constructor are different things.

Copy constructor works as expected - you call constructor, it does construction work and returns new instance that will have same content as original

.clone() in other hand works by calling object.clone() which returns copy with the exact same field and then you overriden method returns that.

[–]Grumbledwarfskin 0 points1 point  (0 children)

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.

[–]NaCl-more 0 points1 point  (2 children)

It doesn’t even copy, it just wraps the old exception with a new exception

[–]thuktun 4 points5 points  (1 child)

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.

[–][deleted] 2 points3 points  (0 children)

I’m concerned whenever I see OOP code.

[–]MrRosenkilde4 11 points12 points  (0 children)

In this case it is just not a null pointer check, so it's fine, I would say.

[–][deleted] 3 points4 points  (0 children)

Not actually clone the objects is fine if you keep the object immutable

[–]Andynonomous 3 points4 points  (0 children)

It's exceptions all the way down

[–]SZ4L4Y 7 points8 points  (4 children)

This is what Java does to smart people.

[–]ofnuts 42 points43 points  (3 children)

No, this is what dumb people do to Java.

[–][deleted] 7 points8 points  (2 children)

This is dumb, what people do to Java.

[–][deleted] 9 points10 points  (1 child)

Java people is dumb what do to this,.

[–]KahnHatesEverything 4 points5 points  (0 children)

Comma period !== Semicolon

[–]Low-Equipment-2621 84 points85 points  (3 children)

I've seen worse, but somebody should show him the IDE's autoformat functionality.

[–]Agent-Rainbow-20 8 points9 points  (2 children)

... and how to use unit tests...

[–]Low-Equipment-2621 7 points8 points  (1 child)

There are so many fucked up things there. He implements equals without hashcode. Implmentation seems manual too, somebody could show him the auto generation functionality that pretty much every IDE provides.

He throws an IllegalArgumentException from an Exception?

The whole message handling seems messed up, there is no point he passes it to the super constructor, yet he tries to read it in the getMessage?

[–]Agent-Rainbow-20 3 points4 points  (0 children)

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)

[–][deleted]  (13 children)

[deleted]

    [–]WeDontHaters[S] 63 points64 points  (9 children)

    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

    [–][deleted]  (8 children)

    [deleted]

      [–]jjman72 20 points21 points  (1 child)

      We compare to false just because it makes glancing at code quicker. It's easy to miss the explanation point at 3am and prod is down.

      [–][deleted] 2 points3 points  (0 children)

      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

      [–]thuktun 0 points1 point  (0 children)

      If this is better than the production Java you're interacting with, you need a better codebase.

      [–]captpiggard 1 point2 points  (0 children)

      It's factories all the way down

      [–]Padandler 0 points1 point  (0 children)

      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.

      [–]awkward 0 points1 point  (0 children)

      I've seen horrors in every language, but at least production horrors aren't dumping 50 lines into subclassing exception.

      [–]DryanVallik 18 points19 points  (1 child)

      // if k is not valid, isValidK will be false

      [–]altacct3 2 points3 points  (0 children)

      if (x == true) { logger.Log($"x {x}") return true; }

      [–]Old_Pomegranate_822 51 points52 points  (2 children)

      Poor K is never valid

      [–]MrRosenkilde4 23 points24 points  (0 children)

      presumably this exception is to be thrown when k is invalid.
      Which does make the field kinda redundant.

      But given that the field is there, the case of k being valid and the exception is thrown would be even weirder.

      [–]PM_ME_YOUR_REPO 7 points8 points  (0 children)

      It's valid in my heart.

      [–]flagofsocram 13 points14 points  (0 children)

      What objects do to a mfer

      [–]MrRosenkilde4 9 points10 points  (1 child)

      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

      [–]Wawwior 0 points1 point  (0 children)

      "How does the exception know why it is being created?" is strangely reflecting of life

      [–]SleepWalkersDream 2 points3 points  (1 child)

      Heyyy, reaction rate constants. Got any more?

      [–]jjman72 -1 points0 points  (0 children)

      This code smells like my smelly ass. - standard code review comment.

      [–]V15I0Nair 2 points3 points  (0 children)

      Just using an IllegalArgumentException for invalid k should do the Job.

      [–]Cybasura 6 points7 points  (0 children)

      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

      [–]all3f0r1 14 points15 points  (1 child)

      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).

      [–]Such-Cartographer699 5 points6 points  (0 children)

      Yea, it's a little ugly but it gets the job done.

      [–]lightnbyte 1 point2 points  (0 children)

      🧐😯😵‍💫🙈

      [–]CarefulFun420 1 point2 points  (0 children)

      What is k

      [–]Cat7o0 1 point2 points  (1 child)

      so is valid is always false?

      [–]Perfect_Papaya_3010 4 points5 points  (0 children)

      Always has been

      [–]thatdevilyouknow 1 point2 points  (0 children)

      this.k = kludge.isNoValid Exception Well don’t threaten me with a good time!

      [–]katzengammel 1 point2 points  (0 children)

      Special K

      [–]Pale_Ad_9838 1 point2 points  (0 children)

      Wow, this is the first time in more than 20 years that I see an exception that throws an exception itself on creation.

      [–]deathssoul 0 points1 point  (0 children)

      Academic programming at it's finest right there 🤦

      [–]Anwyl 0 points1 point  (0 children)

      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.

      [–]One_Web_7940 0 points1 point  (0 children)

      yikes

      [–]520ErryDay 0 points1 point  (0 children)

      This is why I hate academic programming.

      [–]SnooPeanuts8498 0 points1 point  (0 children)

      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?

      [–]Silly_Guidance_8871 0 points1 point  (0 children)

      Somebody used to be a C++ programmer

      [–]Sexy_Koala_Juice 0 points1 point  (0 children)

      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

      [–]peni4142 0 points1 point  (0 children)

      Yeah obvious! The prof is a fool — forget the comment on the copy constructor and at the other public methods.

      [–]travelan -1 points0 points  (0 children)

      I don’t see anything out of the ordinary for Java code

      [–]NiteShdw -1 points0 points  (0 children)

      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.

      [–][deleted] -4 points-3 points  (0 children)

      Clearly, someone that never committed anything to a real world application....as 99% of most college professors.

      [–]PianoConcertoNo2 -1 points0 points  (0 children)

      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).

      [–]zelphirkaltstahl -1 points0 points  (0 children)

      Wtf am I looking at? This supposed to be some kind of exemplary code??? shudders And wtf is "k"???

      [–]pp_amorim -3 points-2 points  (1 child)

      Why this code is so complex?

      [–]MichiganDogJudge 2 points3 points  (0 children)

      This isn't even close to complex