Analyzing Code at Compile Time

Senior developers and software architects often champion coding standards, and they try to ensure consistent practices are followed in their teams. We can use Groovy’s power to automate code review for smells and poor practices. We’ll see here how to use compile-time metaprogramming to detect smells.

Naming variables is difficult and takes effort, but using single-letter variables is morally wrong. Rather than manually policing the code, we’ll leverage compile-time metaprogramming to inspect poor variable and method names.

AST/CodeAnalysis/smelly.groovy
 
def​ canVote(a) {
 
a > 17 ? ​"You can vote"​ : ​"You can't vote"
 
}
 
 
def​ p(instance) {
 
//code to print the instance...
 
}

The canVote method takes ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.