Explicit Java Statements

I am wondering why the assert keyword is used so little in Java? I almost never saw them, but I think this is a great idea. Of course, I prefer brevity:

 assert param != null : "Param cannot be null"; 

to verbosity:

 if (param == null) { throw new IllegalArgumentException("Param cannot be null"); } 

My suspicion is that they are not being used enough, because

  • They arrived relatively late (Java 1.4), by this time many people had already established their Java programming style / habit
  • They are disabled at runtime by default, WHY OH WHY ??
+64
java assertions
Nov 18 '08 at 14:38
source share
10 answers

theoretically, for testing invariants , assumptions must be true for the code to finish correctly.

In the example shown, validation of the valid input is performed, which is not a typical use for approval, since it is usually provided by the user.

Statements are usually not used in production code, because there is overhead and it is assumed that situations in which invariants fail were caught as coding errors during development and testing.

Your point of view that they come "late" in java is also the reason that they are not considered more broadly.

In addition, modular test environments allow some need for software statements to be external to the code under test.

+62
Nov 18 '08 at 14:45
source share

This is an abuse of claims to use to validate user input. Calling IllegalArgumentException for invalid input is more rights, because it allows the calling method to catch an exception, throw an error and do whatever it needs (request input again, exit, whatever).

If this method is a private method inside one of your classes, this statement is good because you are just trying to make sure that you did not pass a null argument to it by accident. You check the statements, and when you checked all the paths and did not call this statement, you can turn them off so you don’t waste resources on them. They are also useful as comments. assert at the beginning of the method is good documentation for maintainers that they must follow certain preconditions, and assert at the end with postcondition documents what this method should do. They can be just as helpful as comments; moreso because with the statements they actually test what they document.

Claims are for testing / debugging, not error checking, so they are disabled by default: to prevent people from using claims to validate user input.

+56
Nov 18 '08 at 15:03
source share

From Programming with Claims

By default, runtime statements are disabled. Two command line switches allow you to selectively enable or disable claims.

This means that if you do not have full control over the runtime, you cannot guarantee that a confirmation code will even be called. Statements are intended for use in a test environment, not for production code. You cannot replace exception handling with claims, because if a user starts your application with claims disabled (by default), all error handling code disappears.

+19
Nov 18 '08 at 15:01
source share

In Effective Java, Joshua Bloch suggested (in the Check Validation Parameters section) (something like a simple rule for adoption), for public methods, we will check the arguments and raise the necessary exception if found to be invalid for non-public methods (which are not displayed, and you, as the user of them, must ensure their accuracy), we can use the statement instead.

Yc

+18
Nov 18 '08 at 18:21
source share

@Don, you are disappointed that the statement is disabled by default. I was there too, and thus wrote this little javac plugin that builds them (i.e. it yields bytecode for if (!expr) throw Ex , and not that stupid assert bytecode.

If you include fa.jar in your classpath when compiling Java code, it will do its magic and then say

 Note: %n assertions inlined. 

@see http://smallwiki.unibe.ch/adriankuhn/javacompiler/forceassertions and alternatively on github https://github.com/akuhn/javac

+9
Mar 20 '09 at 13:14
source share

I’m not sure why you would bother to write statements, and then replace them with standard ones, if this is a condition statement, why not just write conditions, as if in the first place?

Claims are for testing purposes only and they have two side effects: large binaries and poor performance when turned on (which is why you can turn them off!)

Claims should not be used to check conditions, because it means that your application’s behavior is different at runtime, when clauses are enabled / disabled - this is a nightmare!

+2
Nov 09 '09 at 13:40
source share

Statements are very limited: you can only test logical conditions, and you need to write code each time for a useful error message. Compare this with JUnit assertEquals (), which allows you to generate a useful error message from the inputs and even show two inputs side by side in the IDE in the JUnit runner.

In addition, you cannot search for statements in any IDE that I have seen so far, but each IDE can search for method calls.

+1
Nov 18 '08 at 14:57
source share

Statements are useful because they:

  • catch programming errors early
  • document code using code

Think of them as code self-esteem. If they fail, it should mean that your program is broken and should stop. Always turn them on during unit testing!

In Pragmatic Programmer, they even recommend allowing them to work in production.

Leave approval incl.

Use statements to prevent impossibility.

Note that statements throw an AssertionError if they fail, and therefore do not fall into the catch Exception.

+1
Jul 29 '13 at 8:12
source share

In fact, they arrived in Java 1.4

I think the main problem is that when coding in an environment where you yourself do not control jvm parameters directly, as on eclipse or J2EE servers (in both cases you can change jvm parameters, but you need to look deeply to find where it can be to make) is easier (I mean that it takes less effort) to use if exceptions (or, even worse, to use nothing).

0
Nov 18 '08 at 14:44
source share

As others have claimed: statements are not suitable for validating user input.

If you are interested in verbosity, I recommend that you check out the library I wrote: https://bitbucket.org/cowwoc/requirements/ . This will allow you to express these checks using very little code, and even you will get an error message on your behalf:

 requireThat("name", value).isNotNull(); 

and if you insist on using statements, you can also do this:

 assertThat("name", value).isNotNull(); 

The result will look like this:

 java.lang.NullPointerException: name may not be null 
0
Apr 6 '17 at 17:28
source share



All Articles