• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

K&B book page 381: Could we use assertion in this scenario?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We know assertion usually disabled before deploy time, if we use it like this, could be not good for production running:

Do Use Assertions, Even in Public Methods, to Check for Cases
that You Know Are Never, Ever Supposed to Happen

This can include code blocks that should never be reached, including the default of a switch statement as follows:
switch(x) {
case 1: y = 3;
case 2: y = 9;
case 3: y = 27;
default: assert false; // We're never supposed to get here!
}
If you assume that a particular code block won't be reached, as in the preceding example where you assert that x must be either 2, 3, or 4, then you can use assert false to cause an AssertionError to be thrown immediately if you ever do reach that code.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The assert keyword came out with Java 4. Assertions are disabled by default because Sun didn't want to break any older code by introducing a new keyword by default.

It is a good idea to enabled assertions during development to help you debug your code. I plan to enable assertions during development and disable assertions in code that I deploy.

If there is deployed code that is having any problems, I plan to turn assertions back on in the deployed code -- I won't even have to re-compile since enabling or disabling assertions is done in the JRE.

Kaydell
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Kaydell. From my point, it better to put a System.out.println() there, instead of an assert, in the place. at deploy time, you will see the result immediately from the log, no assertion on/off required, and many production run is not really repeatable.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes I use a System.out.println(). I use this generally for stubs that are not finished but not critical things that must be implemented. I use a call to println() for status info and for temporary debugging status.

Assertions still have their place. I use assertions for things that I think are finished, to stop software immediately when the unexpected happens.

A third alternative is to throw an exception myself. When you do this, the exception happens both during development and during deployment. Throwing an exception might be handled and the program might recover or in case of something really serious, I may want the software to stop.

For example, in database work, if something really weird happen, I'd rather that the software just stop and print the error to the log file. In this case, stopping is better than corrupting data.

Kaydell
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point Kaydell. Thanks.
 
Hey cool! They got a blimp! But I have a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic