Elvis and NullSafe: description of patterns

Ruslan Shevchenko rssh at gradsoft.com.ua
Sat Apr 25 07:36:47 PDT 2009


It;s about patterns for applicability of elvis Proposal in
http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000047.html


First naive approach was to count all condition expressions, which looks
like  (a != null ? a.method(..) : b).
  This approach is criticized, because semantics of substitution with
elvis operators _<code>a?method() ?: b</code>_ is differ from original:
when _a.method()_ will return _null_, than result of expression will be
_b_, not _null_.
<IMHO>
  From other side, when people will write software with elvis operator, I
believe they will slightly change semantics of their functions to use
elvis, so this approach still can be used as estimation.
</IMHO>

 After helper discussion with Stephen in this list, I want to describe
patterns which will used for counting of number of applicability for
elvis proposal in next 'release or run' of javachecker coin rules, to
ensure, that they are more or less correct.

rules can be reviewed now: (see
http://redmine.gradsoft.ua/repositories/entry/javachecker/trunk/JavaChecker/etc/checkers_elvis.def)

So, patterns are:

Elvis itself, i. e.
  a==null ? a : b
  a!=null ? b : a
  null==a ? b : a
  null!=a ? a : b

NullSafe method call (as condition statement)

if (a==null) {
  a.process();
}

and

if (a==null) {
  x=a.process();
}


NullSafe method call (as condition expression)
  a!=null ? a.method(...) : null
  a==null ? null: a.method(...)
  null==a ? null: a.method(...)
  null!=a ? a.method(...) : null

 NullSafe field access. (as method calls, but with field access expression
instead method call)

  NullSafe array index access (as method calls, but with array index
expression instead method call)

 Complex null-safe expression. example is

 public static Integer qqq(Z[] z, int i)
 {
   if ( z!=null && z[i]!=null && z[i].y!=null) {
     return z[i].y.a;
   } else {
     return null;
   }
 }


We use some approximation, i. e. count all if patterns, where
* if condition expression is ConditionAnd of conditions all of which are
check for nulls.
* argument of first elementary condition from condition expression is
contained in block, which evaluated when if condition experssion is
evaluated to true.

Hope, this is more close to reality, than first naive proposal.

Below of run for jetty, main results and javachecker custom version  will
be available later (hope tomorrow) after adding loop index. handling
issues.


*       :       0
all bit ands    :       121
all conditional expressions     :       277
all catchs      :       281
all ifs :       764
all integer literals    :       2903
big integer literals(undescores)        :       25
byte literal    :       52
catch in finally        :       3
elvis   :       43
elvis1  :       33
instanceof switch       :       8
loop with remove        :       0
multi catch     :       22
nullsafe call of fields and methods     :       44
object switch   :       31
rethrow clause  :       34
string in switch        :       19
widening operator (semantics)   :       10
widening operator (syntax)      :       20
Files:187







More information about the coin-dev mailing list