In most programming languages there is a way to reuse code. Code that can be called from another part of a program is usually referred to as a function or method depending on your language. So you may have wondered is there something similar in OGNL? In fact there is.

In the [Language Guide](http://commons.apache.org/proper/commons-ognl/language-guide.html) you will find that it is called *Pseudo-Lambda Expressions,* but we will simply call them functions.

To show how they can be used, I'll go back to a previous article in my series on OGNL, [Looping in OGNL take 2](https://www.pingidentity.com/en/resources/blog/post/looping-in-ognl-take-2.html), and use that code as an example. We want to look for duplicated code, which is a good candidate for a function.

See the complete list of articles in my OGNL series at the end of this post.

The original code is listed below:

#groupCnOnly = new java.util.ArrayList(),  
 #groups = #this.get("ds.LDAP.memberOf")!=null?#this.get("ds.LDAP.memberOf").getValues():{},  
 #groups.{  
 #group = #this,  
 #group = new javax.naming.ldap.LdapName(#group),  
 #cn = #group.getRdn(#group.size() - 1).getValue().toString(),  
 #groupCnOnly.add(#cn)  
 },  
 #this.get("ds.LDAP.memberOf")!=null? new org.sourceid.saml20.adapter.attribute.AttributeValue(#groupCnOnly):null

Looking at the code we get the attribute ds.LDAP.memberOf three times and we test for a null value twice. To simplify, the first thing we do is declare a variable to hold the value from the get:

#testVal = #this.get("ds.LDAP.memberOf")

Instead of using the expression #this.get("ds.LDAP.memberOf") we use #testVal. This reduces some of the duplicate code, but what do we do about the testing for a non-null value expression #this.get("ds.LDAP.memberOf")!=null? This can be our function. The function syntax looks like the following:

#notNull = :[#this != null]

Inside the square brackets [ ] is the code in our function. We assign that code to a variable that we use to call the function. The colon : [ at the open square bracket is also part of the syntax. Taking the proposed code changes we end up with the following:

#notNull = :[#this != null],  
 #testVal = #this.get("ds.LDAP.memberOf"),  
 #groupCnOnly = new java.util.ArrayList(),  
 #groups = #notNull(#testVal)?#testVal.getValues():{},  
 #groups.{  
 #group = #this,  
 #group = new javax.naming.ldap.LdapName(#group),  
 #cn = #group.getRdn(#group.size() - 1).getValue().toString(),  
 #groupCnOnly.add(#cn)  
 },  
 #notNull(#testVal)? new org.sourceid.saml20.adapter.attribute.AttributeValue(#groupCnOnly):null

The function must be declared before it is used, so we put it at the top. The first use of the function is initializing the groups variable:

#groups = #notNull(#testVal)?#testVal.getValues():{},

We pass in the testVal variable initialized earlier. The variable passed becomes the this variable in the function.

In PingFederate it will look like:

![](https://images.pingidentity.com/image/upload/f_auto,q_auto,w_auto,c_scale/ping_dam/content/dam/picr/dia/bl/support/07_basics_of_ognl_01.png) 

Once we execute it:

![](https://images.pingidentity.com/image/upload/f_auto,q_auto,w_auto,c_scale/ping_dam/content/dam/picr/dia/bl/support/07_basics_of_ognl_02.png) 

Stay tuned for more about OGNL. In the meantime please leave a comment on this post and let me know what topics you would like to see. Follow me on Twitter: [@jdasilvaPI](https://twitter.com/jdasilvapi)

****************************************

**OGNL Blog Series:**

1. [Introduction to OGNL](https://www.pingidentity.com/en/resources/blog/post/introduction-to-ognl.html)
1. [A simple OGNL expression](https://www.pingidentity.com/en/resources/blog/post/a-simple-ognl-expression.html)
1. [Declaring variables in OGNL](https://www.pingidentity.com/en/resources/blog/post/declaring-variables-in-ognl.html)
1. [Method calls in OGNL](https://www.pingidentity.com/en/resources/blog/post/method-calls-in-ognl.html)
1. [Arrays in OGNL](https://www.pingidentity.com/en/resources/blog/post/arrays-in-ognl.html)
1. [OGNL: What about those curly braces?](https://www.pingidentity.com/en/resources/blog/post/ognl-what-about-those-curly-braces.html)
1. [Looping in OGNL](https://www.pingidentity.com/en/resources/blog/post/looping-in-ognl.html)
1. [Looping in OGNL take 2](https://www.pingidentity.com/en/resources/blog/post/looping-in-ognl-take-2.html)
1. [So what exactly is #this in OGNL?](https://www.pingidentity.com/en/resources/blog/post/so-what-exactly-is-this.html)
1. [A continuing look at #this variable in OGNL](https://www.pingidentity.com/en/resources/blog/post/a-continuing-look-at-this-variable-in-ognl.html)
1. [Functions in OGNL](https://www.pingidentity.com/en/resources/blog/post/functions-in-ognl.html)
1. [Misc Topics in OGNL](https://www.pingidentity.com/en/resources/blog/post/misc-topics-in-ognl.html)

*John DaSilva develops training and solutions at Ping Identity.*