Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:01:16+00:00 2026-05-22T16:01:16+00:00

Really my question is Can the code sample below be even smaller? Basically the

  • 0

Really my question is “Can the code sample below be even smaller? Basically the code sample is designed to first look through a list of objects, find the most granular (in this case it is branch) and then query backwards depending on what object it finds.

1 – If it finds a branch, return the findAllBy against the branch

2 – If it finds a department, return the findAllBy against the department

3 – If it finds an organization, return the findAllBy against the organization

The goal is to find the most granular object (which is why order is important), but do I need to have two separate blocks (one to define the objects, the other to check if they exist)? Or can those two executions be made into one command…

def resp
def srt = [sort:"name", order:"asc"]

def branch = listObjects.find{it instanceof Branch} 
def department = listObjects.find{it instanceof Department}
def organization = listObjects.find{it instanceof Organization}

resp = !resp && branch ? Employees.findAllByBranch(branch,srt) : resp
resp = !resp && department ? Employees.findAllByDepartment(department,srt) : resp
resp = !resp && organization ? Employees.findAllByOrganization(organization,srt) : resp

return resp

What I’m thinking is something along the lines of this:

def resp
resp = Employees.findAllByBranch(listObjects.find{it instanceof Branch})
resp = !resp ? Employees.findAllByDepartment(listObjects.find{it instanceof Department}) : resp
resp = !resp ? Employees.findAllByOrganization(listObjects.find{it instanceof Organization}) : resp

But I believe that will throw an exception since those objects might be null

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:01:17+00:00Added an answer on May 22, 2026 at 4:01 pm

    You can shorten it up a bit more with findResult instead of a for in loop with a variable you need to def outside:

    def listObjects // = some predetermined list that you've apparently created
    def srt = [sort:"name", order:"asc"]
    
    def result = [Branch, Department, Organization].findResult { clazz -> 
        listObjects?.find { it.class.isAssignableFrom(clazz) }?.with { foundObj ->
            Employees."findAllBy${clazz.name}"(foundObj, srt)
        }
    }
    

    findResult is similar to find, but it returns the result from the first non-null item rather than the item itself. It avoids the need for a separate collection variable outside of the loop.

    Edit: what I had previously didn’t quite match the behavior that I think you were looking for (I don’t think the other answers do either, but I could be misunderstanding). You have to ensure that there’s something found in the list before doing the findAllBy or else you could pull back null items which is not what you’re looking for.

    In real, production code, I’d actually do things a bit differently though. I’d leverage the JVM type system to only have to spin through the listObjects once and short circuit when it found the first Branch/Department/Organization like this:

    def listObjects
    def sort = [sort:"name", order:"asc"]
    
    def result = listObjects?.findResult { findEmployeesFor(it, sort) }
    
    ...  // then have these methods to actually exercise the type specific findEmployeesFor
    
    def findEmployeesFor(Branch branch, sort) { Employees.findAllByBranch(branch, sort) }
    def findEmployeesFor(Department department, sort { Employees.findAllByDepartment(department, sort)}
    def findEmployeesFor(Organization organization, sort { Employees.findAllByOrganization(organization, sort)}
    def findEmployeesFor(Object obj, sort) { return null } // if listObjects can hold non/branch/department/organization objects
    

    I think that this code is actually clearer and it reduces the number of times we iterate over the list and the number of reflection calls we need to make.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question below is not really a programming question, but more of how can
My question has to do with facetting. In my example code below, I look
I guess that this is a really stupid question, knowing that .net-code can be
It's a really basic question but i can't think at the second. How do
This is probably a really silly question bu tI can't seem to find an
My question really has three related parts which I hope you can help me
Really stupid question, sorry, but I can't find it on google (I'm sure it's
I have really basic question. How can I get form id by input element
This is a really simple question, but I can't seem to find the answer.
This feels like a really basic question, but I can't figure it out anyway..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.