8. Lose public Lose ; and return Lose .class Lose getX() / setX() public String className( Class c ) { return c.getName(); } o.setName( className( Map.class ));
9. Lose public Lose ; and return Lose .class Lose getX() / setX() def className( Class c ) { c.name } o.name = className( Map )
10. Lose public Lose ; and return Lose .class Lose getX() / setX() http://codenarc.sourceforge.net/ http://plugins.intellij.net/plugin/?idea&id=5925
11. Lose public Lose ; and return Lose .class Lose getX() / setX() def className( Class c ) { c.name } o.name = className( Map ) It is a big deal at the end of the day
20. if (( o != null ) && ( o.size() > 0 )) { .. } if ( o?.size()) { .. }
21. if (( o != null ) && ( o.size() > 0 )) { .. } if ( o?.size()) { .. } Safe navigation operator : object?.method()
22. if (( o != null ) && ( o.size() > 0 )) { .. } if ( o?.size()) { .. } Safe navigation operator : object?.method() Groovy Truth: null is false Empty String, Map or Collection is false Zero number is false if ( list ), if ( string ), if ( map ), if ( o?.size()) ..
27. assert “false” assert “ “ Object.asBoolean() Object => Boolean? Groovy : o asboolean Java : Boolean.valueOf( String.valueOf( o ))
28. assert “false” assert “ “ Object.asBoolean() Object => Boolean? Groovy : o asboolean Java : Boolean.valueOf( String.valueOf( o )) “false”, “null”: false in Java, true in Groovy
29. assert “false” assert “ “ Object.asBoolean() Object => Boolean? Groovy : o asboolean Java : Boolean.valueOf( String.valueOf( o )) “false”, “null”: false in Java, true in Groovy Always specify if you use Java or Groovy Truth
30. assert “false” assert “ “ Object.asBoolean() Object => Boolean? Groovy : o asboolean Java : Boolean.valueOf( String.valueOf( o )) “false”, “null”: false in Java, true in Groovy Always specify if you use Java or Groovy Truth
34. int j = ( o.size() > 0 ) ? o.size() : -1 def j = ( o.size() ?: -1 ) Elvis operator: def j = value ?:defaultValue Takes defaultValue if value evaluates to false
35. int j = ( o.size() > 0 ) ? o.size() : -1 def j = ( o.size() ?: -1 ) Elvis operator: def j = value ?:defaultValue Takes defaultValue if value evaluates to false Be careful with zero values and empty Strings
36. int j = ( o.size() > 0 ) ? o.size() : -1 def j = ( o.size() ?: -1 ) Elvis operator: def j = value ?: defaultValue Takes defaultValue if value evaluates to false Be careful with zero values and empty Strings int j = ( size != null ) ? size : -1 int j = size ?: -1
37. int j = ( o.size() > 0 ) ? o.size() : -1 def j = ( o.size() ?: -1 ) Elvis operator: def j = value ?: defaultValue Takes defaultValue if value evaluates to false Be careful with zero values and empty Strings int j = ( size != null ) ? size : -1 // Accepts zero size int j = size ?: -1 // Doesn’t accept zero size
39. public String foo( int j, int k ){ …} public String foo( int j ){ foo ( j, 1 ); } public String foo(){ foo ( 1, 1 ); }
40. public String foo( int j, int k ){ …} public String foo( int j ){ foo ( j, 1 ); } public String foo(){ foo ( 1, 1 ); } Overload
41. public String foo( int j, int k ){ …} public String foo( int j ){ foo ( j, 1 ); } public String foo(){ foo ( 1, 1 ); } def foo ( int j = 1, int k = 1 ) { .. }
42. public String foo( int j, int k ){ …} public String foo( int j ){ foo ( j, 1 ); } public String foo(){ foo ( 1, 1 ); } def foo ( int j = 1, int k = 1 ) { .. } def foo ( int j = 1, intk ) { .. }
43. public String foo( int j, int k ){ …} public String foo( int j ){ foo ( j, 1 ); } public String foo(){ foo ( 1, 1 ); } def foo ( int j = 1, int k = 1 ) { .. } def foo ( int j = 1, intk ) { .. } def foo ( intj, int k = 1 ) { .. }
44. public String foo( int j, int k ){ …} public String foo( int j ){ foo ( j, 1 ); } public String foo(){ foo ( 1, 1 ); } def foo ( int j = 1, int k = 1 ) { .. } def foo ( int j = 1, intk ) { .. } def foo ( intj, int k = 1 ) { .. } def foo ( int j = f1(), intk = f2()) { .. }
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