вс, 4 апр. 2021 г. в 13:24, André Warnier (tomcat/perl) <a...@ice-sa.com>: > > Hi. > I have a question which may be totally off-topic for this list, but this has > been puzzling > me for a while and I figure that someone here may be able to provide some > clue as to the > answer, or at least some interesting ponts of view. > > In various places (including on this list), I have seen multiple occurrences > of a certain > way to write a test, namely : > > if (null == request.getCharacterEncoding()) { > > as opposed to > > if (request.getCharacterEncoding() == null) { > > Granted, the two are equivalent in the end.
Some programming languages have rules, in what order an expression is evaluated. E.g. the left side is evaluated first, the result is stored in a register (memory) of a CPU, then the right side is evaluated and the result is stored, then it is followed by a comparison and a conditional jump. Thus the two variants are not equivalent. (Well, as null is a zero and not really a specific value, maybe it does not need evaluation and a memory register to store it.) In Java the Java Language Specification dictates the evaluation order, "15.7.1 Evaluate Left-Hand Operand First". I vaguely remember that in the C language the evaluation order in such expressions is unspecified. https://docs.oracle.com/javase/specs/ If one side of an expression can have unexpected side effects (like a function call or a null pointer dereference can have), I prefer them to be evaluated first. Thus my preference is for "(request.getCharacterEncoding() == null)". Otherwise, another point of view to consider is readability of the code. If the function call is some lengthy expression, " (null == request.getCharacterEncoding()) " may be more readable when formatting the code results in wrapping the lengthy expression, splitting it into several lines. I think that I should also mention the well-known construct when a comparison is done by calling the "equals()" method on some constant value: CONSTANT_VALUE.equals(someFunction()) In this case the "CONSTANT_VALUE" is known to be non-null, and thus calling its method cannot result in a NullPointerException. (In more complex cases the static method "Objects.equals()" helps to compare two values in a null-aware way). Best regards, Konstantin Kolinko --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org