Tuesday, March 10, 2015

What is better inversion in method names of inversion operator in usage ?

What is better inversion in method names of in usage ? What criteria we could use measure that ?

For private methods it might be not a problem, but mostly affects API and public methods

@Override
public void visitToken(DetailAST aAst) {
DetailAST conditionExpressionAst = aAst.findFirstToken(TokenTypes.EXPR);
switch (aAst.getType()) {
case TokenTypes.LITERAL_RETURN:
if (isNonEmptyReturn(aAst)) {
DetailAST inversionAst = getInversion(conditionExpressionAst);
if (isAvoidableInversion(inversionAst)) {
log(inversionAst);
}
}
break;
case TokenTypes.LITERAL_WHILE:
case TokenTypes.LITERAL_DO:
case TokenTypes.LITERAL_IF:
DetailAST inversionAst = getInversion(conditionExpressionAst);
if (isAvoidableInversion(inversionAst)) {
log(inversionAst);
}
break;
case TokenTypes.FOR_CONDITION:
if (isNonEmptyForCondition(aAst)) {
inversionAst = getInversion(conditionExpressionAst);
if (isAvoidableInversion(inversionAst)) {
log(inversionAst);
}
}
break;
default:
final String exceptionMsg = "Unexpected Token Type - "
+ TokenTypes.getTokenName(aAst.getType());
throw new IllegalStateException(exceptionMsg);
}
}
code:
/**
* Checks if return statement is not empty
* @param aReturnAst
*/
private static boolean isNonEmptyReturn(DetailAST aReturnAst) {
return aReturnAst.findFirstToken(TokenTypes.EXPR) != null;
}
/**
* Checks if condition in for-loop is not empty
* @param aForConditionAst
*/
private static boolean isNonEmptyForCondition(DetailAST aForConditionAst) {
return aForConditionAst.getFirstChild() != null;
}

Some code could not be written without "NOT"/"NON" , Example:

No comments:

Post a Comment