0

I know that the assert keyword should be used when we want to make a bug come out, but is it ok to use it in situations like the one that follows? The redirectAdd method only receives args whose length is more than 3, I added it for anyone who will read the method in the future to give some logical context, is it ok or it's better if I remove it and add a comment instead?

The code I'm referring to:

private static boolean redirectAdd(Player player, String[] args, ItemStack mainHandItem) {
    assert args.length > 3;
    if (args.length == 4) {
        //more actions & another return
    } else if (args.length == 5) {
        //more actions & another return
    } else if (args.length == 6) {
        //more actions & another return
    } else {
        player.sendMessage(ChatColor.RED + "There are too many arguments! The last should be " + args[5] + "");
        return false;
    }
}
Giopav
  • 5
  • 2
  • Depends on what you want to have happen. If the assert triggers (i.e. the condition evaluates to `false`), an `AssertionError` (unchecked exception) is thrown and unless you handle it somewhere up the stack chain, the thread terminates. IOW, `assert` is for checking conditions that should NEVER be violated, and where violation is cause to take drastic action. It is NOT for conditions that may occur during normal execution. – Jim Garrison Dec 10 '22 at 19:30
  • `assert` is useful for checking your own code, to make sure you yourself didn’t make a mistake. The method in your question is `private`, so assert is probably appropriate, since only your own code can call it. For public methods that might be called by other code that you don’t control, assert should not be used; in that case code should check the argument and throw IllegalArgumentException if there’s a problem (or NullPointerException if the argument was null but the method does not allow it to be null). – VGR Dec 10 '22 at 22:48

2 Answers2

1

Short answer:

Assert should not be used for other purposes than debugging, especially not in production code. The reason for this is that these assert checks can be disabled. They are disabled by default and need to be enabled by using the -ae compiler flag (thanks @user16320675 for pointing this out).

For long answer and further information, see here: What does the Java assert keyword do, and when should it be used?

LarsJaeger
  • 100
  • 7
  • 1
    "compiler flag" is correct, but for enabling - from the [documentation](https://docs.oracle.com/en/java/javase/19/docs/specs/man/java.html#standard-options-for-java): "*By default, assertions are disabled in all packages and classes.*" – user16320675 Dec 10 '22 at 19:55
  • So to draw up conclusions: Use them only where the result of the program would not change if they were or were not there (ex. help programmers with the logic). Am I right? – Giopav Dec 10 '22 at 22:34
-1

The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.