views:

50

answers:

1

Hi!

I always check the arguments of public functions and throw exceptions when something's wrong. (For private helpers I use assertions).

Like this:

if( a < 0 || a >= b )
    throw new IllegalArgumentException("'a' must be greater or equal to 0 and 
        smaller than b ");

But it always annoys me to write these error messages. The message seems redundant to me, as the message is just the negation of the statement

a < 0 || a >= b

.

It also often happens that I rename the variable with refactoring (in eclipse) and then the message does not reflect the changes. Or I change the conditions and forget to change the messages.

It would be great, if I could write something like:

assertArgument(a >= 0 && a < b);

This should raise an IllegalArgumentException with a message like

"violated argument assertion: a >= 0 && a < b."

In C you could write a macro (actually in C assert is just a macro). Is there a simple way to do something like this in java?

Thank you!

+3  A: 

In the C language you cannot use macros to do this, but in cpp (the C preprocessor) you can :-) If you really want to do so there is nothing that restricts you from running cpp on your java source before it gets compiled. That would let you use cpp style macros (you might need to strip the lines starting with #line from the cp output.)

To reproduce the conditional in the exception is IMHO including too much implementation detail. If your exception message describes the violation in the contract in terms of the contract (for instance "no parent object given", "amount cannot be negative") you don't need to change it every time the condition changes.

rsp
You're right about the preprocessor, but I would consider the C preprocessor as a part of C programming. Preprosessor statements are maybe not really part of the C language, as the C compiler never gets to see them, but they are really essential in writing C programs.
captaincomic
And thanks for the suggestions: Using a Preprocessor or runnning a Scipt (as Hamish Grubijan suggested) to change or check the code, could solve my (really not so deadly) problem.Altough I thought more of a simple and native Java solution, as it is also important that other people clearly understand whats happening. Then again I could share only the generated code... Could maybe Design by Contract help? I have no experience with DbC so far.
captaincomic
If are looking into preprocessors, you might want to look into M4 (http://www.gnu.org/software/m4/) DbC can help, but you would not normally run the instrumented code in production. Because your requirement is one-of a perl or python script that fixes up your exception argument is probably the quickest way to go.
rsp