Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I habitually put assert()-statements in my code to check for programmer errors. When an assert() triggers it crashes the application immediately so the programmer error can be fixed. Problem is: assert() doesn't actually exist in the Android runtime (ART)! You can try it yourself - put assert(false) in your Game constructor and run it - nothing happens! Not in debug mode, not on the emulator nor on the physical device.
- So I added two new static functions into our Utilities-class - require() and expect().
- */
- import android.util.Log;
- public abstract class Utils {
- public static void expect(final boolean condition, final String tag) {
- Utils.expect(condition, tag, "Expectation was broken.");
- }
- public static void expect(final boolean condition, final String tag, final String message) {
- if(!condition) {
- Log.e(tag, message);
- }
- }
- public static void require(final boolean condition) {
- Utils.require(condition, "Assertion failed!");
- }
- public static void require(final boolean condition, final String message) {
- if (!condition) {
- throw new AssertionError(message);
- }
- }
- }
- /*
- requires() will throw an AssertionError and thus be functionally equivalent to the original assert(). Your application will crash, and the debugger will give you a stack trace to show where the fault was.
- expects() logs the error but won't crash the app. This is good for documenting non-critical expectations.
- Ergo: whenever you see an assert() in my Java-articles, replace it with Utils.requires() in your own code.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement