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;
}
}
CodePudding user response:
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?
CodePudding user response:
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.