{"id":23510,"date":"2015-07-23T02:53:46","date_gmt":"2015-07-22T21:23:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23510"},"modified":"2015-07-24T13:59:52","modified_gmt":"2015-07-24T08:29:52","slug":"closure-delegate-using-groovy-with-method-and-decorating-code-with-multiple-assignments","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/closure-delegate-using-groovy-with-method-and-decorating-code-with-multiple-assignments\/","title":{"rendered":"Closure Delegate using Groovy &#8220;with&#8221; Method and decorating code with multiple assignments"},"content":{"rendered":"<p>Groovy is an expressive language, that is\u00a0what we are going to talk about making the usage of &#8220;with&#8221; method for specifying closure delegate and making our code more succinct (brief and clearly expressed).<\/p>\n<p>In my recent project, I was writing a code to\u00a0resolve tenant and setting some properties to object tenant.<\/p>\n<pre>resolver.tenant.subdomain = \"india\"\r\nresolver.tenant.username = \"Tarun\"\r\nresolver.tenant.group = \"ttn\"\r\nresolver.tenant.resource = \"user\"\r\n.....\r\n.....<\/pre>\n<p>As there are many other properties it becomes cumbersome to write resolver.tenant as a\u00a0lot of properties are being set on\u00a0same object tenant. As we are repeating ourselves here and properties that are associated with tenant are overshadowed\u00a0by resolver.tenant.<\/p>\n<p>Here tenant is our object of ResolvedTenant class.<\/p>\n<p>One of the way to make it more expressive is to use closure delegate :<\/p>\n<pre>def tenantClosure = {\r\n\u00a0\u00a0subdomain=\"india\"\r\n\u00a0\u00a0username=\"Tarun\"\r\n\u00a0\u00a0group=\"ttn\"\r\n\u00a0\u00a0resource=\"user\"\r\n}\r\n\r\ntenantClosure.delegate = resolver.tenant\r\n\r\ntenantClosure()\r\n<\/pre>\n<p>But using\u00a0groovy <em>&#8220;with&#8221;<\/em>\u00a0method will make code much easier to understand and write, making code more compact and readable. Let&#8217;s try it out.<\/p>\n<pre>resolver.tenant.with {\r\n    subdomain = \"india\"\r\n    username = \"Tarun\"\r\n    group = \"ttn\"\r\n    resource = \"user\"\r\n}<\/pre>\n<p>Here object on which <em>&#8220;with&#8221;<\/em> is being called, is a object used to define closure delegate and all the properties within that object can be accessed directly within &#8220;<em>with&#8221;<\/em>\u00a0closure. We can also invoke methods bind to object in <em>&#8220;with&#8221;<\/em> closure.\u00a0As you can see\u00a0we have saved ourselves declaring closure delegate and calling closure using &#8220;<em>with&#8221;<\/em> Method.<\/p>\n<p>Decorating or making it more groovier\u00a0using multiple assignments and &#8220;with&#8221;, we can reduce entire block to single line :<\/p>\n<pre>resolver.tenant.with {\r\n\u00a0\u00a0(subdomain, username, group, resource) = [\"india\",\"Tarun\",\"ttn\",\"user\"]\r\n}<\/pre>\n<p>For further reference, take a look over multiple assignments blog : <a href=\"http:\/\/www.tothenew.com\/blog\/multiple-variable-assignment-in-groovy\/\">Multiple Variable Assignment by\u00a0Ankur Tripathi<\/a><\/p>\n<p>For those\u00a0who are not much familiar with\u00a0goodness of multiple assignments and &#8220;with&#8221; Method in groovy.<\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Groovy is an expressive language, that is\u00a0what we are going to talk about making the usage of &#8220;with&#8221; method for specifying closure delegate and making our code more succinct (brief and clearly expressed). In my recent project, I was writing a code to\u00a0resolve tenant and setting some properties to object tenant. resolver.tenant.subdomain = &#8220;india&#8221; resolver.tenant.username [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":14},"categories":[7,1],"tags":[358,885,9,2013,2014],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23510"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23510"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23510\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}