Intellij Idea hint: Condition is always false - can that be true here? (Java) -
i have following code:
public string testexitpoints() { boolean myboolean = false; try { if (getboolean()) { return "exit 1"; } if (getboolean()) { throw new runtimeexception(); } } { myboolean = true; } if (getboolean()) { return "exit 2"; } return "exit 3"; } public static boolean getboolean() { random rand = new random(); return rand.nextint() > 100; }
now intellij idea gives me second , third invocation of getboolean()
following hint:
condition 'getboolean()' 'false'
now understanding, not true, since getboolean()
can either true
or false
, depending on generated random value. missing here, or bug in intellij idea?
it's not bug. it's feature :)
if in ide, tell 2nd , 3rd call getboolean() false, not first one.
idea assumes (in case incorrectly) method, being parameterless , called "get"..., return same value.
if case, , first call true, other never accessed (because of return).
if first call false, others.
idea tries smart w.r.t. coding practices, it's not infallible.
if change method have parameters (or rename doesn't getter)
public boolean getboolean(int x) { random rand = new random(); return rand.nextint() > 100; }
the warnings go away (even if invoke same argument times).
(note that, if getter, if it's non-final field it's still wrong, may change in multithreaded environment!)
Comments
Post a Comment