Recently I came upon a groovy oddity. (At least it is perceived by me to be an oddity). Closures in a groovy class do not have access to a private method if that method is defined in the superclass. This seems odd paired against the fact that regular methods in a super class can access private method defined in the super class
Background
Groovy closures have the same scope access to class member variables and methods as a regular groovy method. In other words, closures are bound to variables in the scope they are defined. See the codehaus link for the official documentation:
http://groovy.codehaus.org/Closures
This implies that a closure will play by the rules of Object Orientation in the Java language. However, I found that closures do not have access to private methods that are defined in a super class.
The best way to demonstrate is through a short example from Grails. I have used TDD for the example. Note this is a dummy case with no business purpose. Later on I will offer a more reasonable scenario in the business context where I encountered this scenario.
Articles and thoughts on Java technologies, software engineering practices, and agile methods.
Analytics
Showing posts with label tdd. Show all posts
Showing posts with label tdd. Show all posts
Sunday, August 12, 2012
Monday, February 21, 2011
5 Handy Groovy Shortcuts
Groovy's main advantage over Java programming is the ability for a developer to implement a solution in fewer lines of code. Groovy's idioms produce concise, short, and clean code. Of course, on first look the code may look strange to a Java developer. But once you learn Groovy's shortcuts you realize the value of consciseness and how less noise produces easier maintenance of code. To me, this is simple: "Since there is less code to look at, there is less code to understand. Learn the shortcuts and idioms and produce less code to do more."
Over the last year, I adopted Groovy as an enterprise implementation language. Here are 5 Groovy programming shortcuts that I found to be very handy. They are listed as tests, in the model of TDD.
Spread Dot Operator
The spread dot operator calls a method on every item in a collection. Upon the invocations, it creates a new list from the return items. So instead of looping through a collection to make a call on each object, use the spread dot operator.
Let's say we want to get the first 3 letters of each username:
Over the last year, I adopted Groovy as an enterprise implementation language. Here are 5 Groovy programming shortcuts that I found to be very handy. They are listed as tests, in the model of TDD.
Spread Dot Operator
The spread dot operator calls a method on every item in a collection. Upon the invocations, it creates a new list from the return items. So instead of looping through a collection to make a call on each object, use the spread dot operator.
Let's say we want to get the first 3 letters of each username:
void testSpreadDot() {
def emails = ["nirav@yahoo.com", "bob@gmail.com", "jacob@msn.com"]
def firstThreeLetters = emails*.getAt(0..2)
assertEquals(firstThreeLetters, ["nir", "bob", "jac"])
}
This is very useful when utilizing several common design patterns or just plan old polymorphism. You are able to call an interface many times with minimal code.
Subscribe to:
Posts (Atom)