Cindy's Segment - a Whimsical View of the World

Segment 7:
Asserting Yourself in Public
Or -
Testing Your Skills

Bill Bixby - the Magician

Ladies and Gentlemen!! Come One! Come All! Welcome to the most Amazing show in all JavaLand!

Presenting the one and only Master Coder ready to Razzle you! Ready to Dazzle you! Ready to Astound you with his feats of skill and daring. Ready to AMAZE you with magic such as has NEVER been seen before in all JavaLand!!

Come right on in!

ClassFile Castle

The Magnificent Assertion Show

Step right up and let me tell you about Assertions. How they can do your work for you. How they can cure everything from flow headaches to achy interfaces. All you need to know is the secret. And the secret is . . . that the new keyword Assert translates to "Make Sure".

What every Master Coder knows is that Asserts are like marking the deck. You put these little magical markers in your code and suddenly you know where all the cards and all the problems are.

Did you know that there are TWO- count them TWO- syntax forms for Assertions? The simple and easy form Assert ThisIsTrue (read this "Make Sure this is True") which spits a simple AssertionError if not true, and the more dramatic, Assert ThisIsTrue : DramaticExplanation which explodes with an AssertionError containing your Dramatic Explanation if not true!! .

Of course like all magic, Assertions have a price, and therefore you need to KNOW how to control them, KNOW how to make them work for you, KNOW how to make them disappear on command! But you, too, can learn this magic and amaze your co-workers and your boss!

Now you see them - Now you don't!

OK now - the first bit of magic is learning the "-source 1.4" command. If you want to do this magic you MUST compile using these mystical words. Otherwise the power will not be invoked! This keeps lesser mortals and compilers from being stunned by the power within.

The next bit of magic is remembering that Assertions cause an Error - not an Exception. This is to keep true IDIOTS from trying to recover when an Assert sends its magical message. One trick at a time please. End the program already, and start it again to see a different trick.

Now - time for the show. No matter how many bits of magic you sprinkle over your code, even if you compile it with the magic words - your Asserts are invisible until you wave your magic wand. Not just invisible - they REALLY do not cause ANY impact to ANYTHING - performance, CPU ANYTHING! They are TRANSPARENT to the JVM!!

Your wand is composed of combinations of -ea and -da, -esa and -dsa parameters when you start your application. (EnableAssertions, DisableAssertions, EnableSystemAssertions, DisableSystemAssertions). You can combine them at different power limitations - className, packageName or no limit levels. Your imagination is your boundary!

  • java -ea MyApp (turns ALL your classes on).
  • java -ea:com.magic.SomePackage MyApp (turns just the SomePackage on).
  • java -ea:com.magic.SomePackage -da:com.magic.sillyClass MyApp (turns just the SomePackage on - except the named class)
  • java -esa MyApp (turns just System Classes on)
  • java -ea - esa MyApp (turns EVERYTHING on)
  • And the beauty of it all is that - to make all those Asserts just DISAPPEAR - all you have to do is . . . nothing. Just call your app with none of these parameters and POOF!! the Asserts have disappeared.

    Test your Feats of Skill and Daring!

    How do you make Assertions work for you?

    The first trick is to locate all the places in your code where you KNOW something is true.

  • The last statement in a nested If series where you know everything else MUST meet . . . some conditions. Put "Assert ThisConditionStatement" there and as long as it IS TRUE - no problem, but as soon it is NOT TRUE your program throws an error.
  • A switch statement where you KNOW that one of the options will always work - therefore the default clause should NEVER get fallen into - put "Assert false : WhatValueFellInHere" in that default clause and you will KNOW if it somehow happens. Note: Did you READ that correctly? That should read "Make sure 'this is false' is a true statement".
  • Any place that "is reachable" but could should never be reached because of your fantastic logic - put "Assert false"
  • The second trick is remember these rules of Magic:

  • Never trust a method to begin clean
  • Never trust a method to end clean
  • Never trust an instance to be clean
  • When your method begins - you KNOW what the input parameters should be - after all you coded all the calls (at least the non-public ones). Make sure that you did them correctly with an Assert. A little secret here though - if it is a PUBLIC method - you should not use Asserts to check that some outsider sent a correct parameter. Blast them with a REAL Exception!!

    When your method is ending - you KNOW the things that should be true at that point. Just before you return a value - Make Sure that you are returning the correct value with an Assert.

    When you modify an instance - you KNOW the things that should be true always about it. When you publically construct an instance - check that your instance still makes sense. When you publically modify your instance - check that instance again.

    Warning: This is for professionals only! Do not try this at home! (with your less that 1.4 JDK).

    Magic Lessons - what NOT to do.

    We already talked about not using magic Asserts to check that public methods are sending the proper things. When amateurs and the general public call a method poorly, they NEED to ALWAYS get real Exception - not an Assert.

    Be Careful to not shoot yourself in the foot with your own wand! Make sure that you do not create the power to make transparent something that your program always needs.

    When you sprinkle your magic through your code - take care that you do not cause any side effects to your code. You might diminish the wonder of the other Asserts that you have added.

    Well - unless of course the only side effects are things that relate to other Asserts that you WANT to happen to help the other Asserts.

    Be Aware that for the very FEW, there are powerful ClassLoader methods out there that can increase your powers.

    If you are Brave and Talented you can gain the powers of:

  • public void setClassAssertionStatus (String className, boolean enabled)
  • public void setDefaultAssertionStatus(boolean enabled)
  • public void setPackageAssertionStatus(String packageName, boolean enabled)
  • public void clearAssertionStatus()
  • They can manipulate the contents of the 3 magic enabling fields:

  • private boolean defaultAssertionStatus = false;
  • private Map packageDefaultAssertionStatus = new HashMap();
  • private Map classAssertionStatus = new HashMap();
  • Study the use of these well, and you will be a Master among Masters!!


    Now take this new found knowledge - and use it wisely and well. And may the magic lead you to the error of your ways and guide you to the inner truths!

    Copyright 2002. Cindy Glass. All rights reserved.

    Graphics by Pauline McNamara and Stephanie Grasson

    Next month - "Abnormal Psychology"- or - "Volitile and Transient Moods"