Monday, May 25, 2009

Ternary operator pattern (aka inline conditions)

ternary operator
String id = request.getParameter("aaa")==null? "" : request.getParameter("aaa");

can be refactored to
String id = request.getParameter("aaa");
if(id==null) id ="";


which can be furthered reduce to

String id = Utility.isNull(request.getParameter("aaa"));

public String isNull(String input){
if(input==null) return "";
return input;
}

No comments:

Post a Comment