Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How about this Assertion?

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there !

Quick Question:

Is it appropriate to use an Assertion to validate a switch's default(that should never ever happen) when the switch block itself is inside a public method ? (and possibly switching one of the public method's arguments)

Thanks in advance. your replies are very important!
Sincerely,
Jose
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,

That sounds like a valid use of Assertions to me.

It would help you discover during development if you've changed the assumptions of valid values passed in via the parameter (e.g., you hadn't updated the switch to include a value you discovered you needed).

Of course, all caveats about Assertions apply -- they are a tool to help discover defects in development, and production applications should disable Assertions.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stevi !

I needed to know If that case was right ! thanks for the reply!

Good Luck,

Jose
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that it's inside a public method is not necessarily a problem. But if it's switching on one of the method arguments, then I would say it's generally better to throw an IllegalArgumentException for something that should never happen:

Note that not only does the JavaDoc explain what the legal values are, but the exception message does too, and tells you what the erroneous value was. This can be very helpful later when debugging.

In a case like this, since the method is public (and I'm assuming the class is too), there really isn't any way to be sure that no one will call this method with an incorrect value. Unless, perhaps, you control all code that will ever use this class, and will always make sure that your assertion will be correct. But that's unlikely, a lot of work, and unreliable. Better to code in a check that will always be there, even if assertions are disabled. So an assertion is not appropriate here. Plus, a bad input value is exactly what IllegalArgumentException is for - might as well use it when it's appropriate.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Better to code in a check that will always be there, even if assertions are disabled.


Even better to code the method in a way that no check is needed, for example by using enums instead of int values.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stevi Deter:

Of course, all caveats about Assertions apply -- they are a tool to help discover defects in development, and production applications should disable Assertions.


I totally disagree. Production applications should fail fast (if they fail at all). http://en.wikipedia.org/wiki/Fail-fast
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

I totally disagree. Production applications should fail fast (if they fail at all). http://en.wikipedia.org/wiki/Fail-fast


I agree with the principle of fail-fast, but I don't think Assertions are the right way to do it.

Actually, I generally don't use Assertions at all. But I do aggressively check parameters and throw plenty of IllegalArgumentExceptions and similar as relevant.

My clients will tolerate a nicely formatted error message that we just can't go forward, but they won't put up with an app up and dying, a la Assertions failing.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't use assertions, either - simply because they are disabled by default. We don't want that behavior.

Using assertions shouldn't hold you from showing nicely formatted error messages, though. You can catch AssertionErrors just as you would catch IllegalArgumentExceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic