8. Lose publicLose ; and returnLose .classLose getX() / setX()public String className( Class c ) { return c.getName(); }o.setName( className( Map.class ));
9. Lose publicLose ; and returnLose .classLose getX() / setX()def className( Class c ) { c.name }o.name = className( Map )
10. Lose publicLose ; and returnLose .classLose getX() / setX()http://codenarc.sourceforge.net/http://plugins.intellij.net/plugin/?idea&id=5925
11. Lose publicLose ; and returnLose .classLose getX() / setX()def className( Class c ) { c.name }o.name = className( Map )It is a big deal at the end of the day
22. if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }Safe navigation operator : object?.method()Groovy Truth:null is falseEmpty String, Map or Collection is falseZero number is falseif ( list ), if ( string ), if ( map ), if ( o?.size()) ..
27. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))
28. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in Groovy
29. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in GroovyAlways specify if you use Java or Groovy Truth
30. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in GroovyAlways specify if you use Java or Groovy Truth
34. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?:defaultValueTakes defaultValue if value evaluates to false
35. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?:defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Strings
36. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?: defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Stringsint j = ( size != null ) ? size : -1int j = size ?: -1
37. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?: defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Stringsint j = ( size != null ) ? size : -1 // Accepts zero sizeint j = size ?: -1 // Doesn’t accept zero size
56. if ( o == null ) { throw new RuntimeException( “msg” ) }
57. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”
58. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”
59. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”
60. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”assert false, “Fatal error”
61. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”assert false, “Fatal error”Asserting code samples is a common practice