1

I'm just starting to learn OOP w/ Java and ran into a problem, which got me to thinking, albeit probably too much. I've searched for an answer for some time, but finding the right question to ask is quite often the biggest challenge.

I've created a Class Triangle with two methods isTriangle() and typeOfTriangle(). My constructor has 3 parameters (side1, side2, side3).

This is my first experience where I realized a user could pass in parameters that would create an object that wasn't actually a Triangle as in the case where two sides added together are less than the third. I added logic to the constructor that would check to see if the actual parameters made a triangle. This did not at all seem like the right way to do it.

Question: Should the Triangle class be responsible for determining if the actual parameters create a viable Triangle? If so how? would that logic actually go in with the constructor?

Do you need to create the object first (non triangle) then determine if what you have created is viable with myTriangle.isTriangle(); ?

Doesn't seem as if the client/user should be responsible for determining if the Triangle class returned a viable Triangle. Shouldn't this be assumed? Although I've always heard assumption is the mother of all screw ups.

I didn't include code as its pretty rudimentary and I'm sure you all get the idea. Just not sure "where" this check should happen. Seems as if the class should not return anything if it's not a Triangle. I'm most likely way overthinking this.

Thanks for taking the time to read my post.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Pacific27
  • 25
  • 3
  • Possible duplicate of http://stackoverflow.com/questions/14394590/preventing-instantiation-of-a-class-if-argument-to-constructor-is-illegal – SomeDude Jan 13 '16 at 18:47
  • @svasa Thank you for the reference. Went a little more in depth on what chrylis touched on. Had a tough time formulating the question to ask so again thanks for the link. – Pacific27 Jan 13 '16 at 21:13

3 Answers3

1

It's up to you whether it makes more sense to permit or forbid "invalid" triangles; for example, if you're doing calculations, it probably does make sense to forbid them.

If you have these sorts of preconditions, you should check them in your constructor and throw an exception (IllegalArgumentException or a custom subclass of it is typically the right choice).

If, on the other hand, it somehow makes sense to permit an "invalid" triangle, then you could certainly have an isValid() method that checked.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • IllegalArgumentException is kind of what I had in mind, although I didn't know what it was, so thanks for posting. I would prefer to keep this logic inside the class so that the class itself takes care of it. Yes, depending on the situation / scenario / specifications there could be multiple ways to handle this, so again thanks for mentioning that. – Pacific27 Jan 13 '16 at 21:25
0

If I was attempting this I would validate the user input first to make sure the lengths given can form a triangle (see http://www.virtualnerd.com/tutorials/?id=Geo_05_01_0003). Once you have confirmed the values are valid, then call your triangle constructor with the arguments.

This way you know your Triangle objects will always be valid Triangles, as you have validated your user input.

Dave0504
  • 1,057
  • 11
  • 30
  • Thanks for posting link. I need to change my isTriangle() method to reflect that side1 + side2 <= side3 does NOT make a Triangle as per your link. Currently have side1 + side2 < side3. Your solution will definitely safeguard an "invalid" Triangle from being created. I'd prefer to keep the logic inside the Class, but there will most likely be situations where your solution could be more advantageous. – Pacific27 Jan 13 '16 at 21:35
0

Checking inside the constructor is not the best way because the object is created even if you notice its not a triangle. Checking after using the constructor has the same problem so I would check before using the constructor. This constructor is very simple so any of this ways wouldnt have almost any consequence on the project however if you do this a lot and with more demanding constructors it could be advantageous not to create the object and save efficiency time.

EDIT: A user just commented on this answer stating that an exception thrown in the constructor can prevent the object from being instantiated wich makes checking inside the constructor a viable option.

sharp_c-tudent
  • 463
  • 5
  • 17