Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Assertion

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Post Assertion

    When should you use assertions in your programs?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Assertion

    Moved from What's wrong section.

    Have you asked this with an internet Search engine?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jamesh (January 3rd, 2014)

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    273
    My Mood
    Bored
    Thanks
    39
    Thanked 76 Times in 64 Posts
    Blog Entries
    4

    Default Re: Assertion

    When to use assertions:
    • To prove a state, or to prove a state doesn't exist.
    • To check preconditions/postconditions in internal code (eg private methods of a library)
    • To provide feedback to the developers as changes are made to a programs structure
    • As part of the unit tests.


    When not to use assertions:
    • To prove correctness in a given state.
    • To resolve run time problems.
    • To check parameters or null objects
    • In a deployed product.


    All of this is debatable but a rule of thumb I read years ago is that assertions prove correctness during development whilst exceptions prove robustness during use. Much like logging they are designed to be turned off for deployment and I generally use them for pretty ridiculous sanity checks in case I happen to break something critical.

  5. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    jamesh (January 3rd, 2014)

Similar Threads

  1. what is Java assertion framework?
    By cloud9resident in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2012, 09:07 AM
  2. Assertion Question
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 15th, 2011, 02:48 PM