• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How about this Assertion?

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stevi !

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

Good Luck,

Jose
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic