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

  • SEARCH
  • Home
  • 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 6817457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:03:30+00:00 2026-05-26T21:03:30+00:00

I have a fairly large chunk of code that produces/returns an arraylist of search

  • 0

I have a fairly large chunk of code that produces/returns an arraylist of search results that is constructed from a number of datasources. I have been asked to include a date restriction method whereby users can build the array based off 2 date fields. If one of these date fields is populated, lets say its called dateFrom then the array will return data from after that date specified. Similarly if the dateTo is populated then it will populate the array with preceeding data and if both then if will obviously populate based off that range.

This is all nice & easy to do with a couple of simple nested if statements or a switch case, but my problem is that this will also mean that I will have to repeat hefty chunk of code. Is there a way I can prevent the same basic arraylist construction code being repeated 4 times? I know this is horrible psuedo code but it gets my point accross:

if(condition1){
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else if (condition2) {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else if (condition3) {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
}

return ArrayList();

Its worth noting that although this is more a generalised Java question I am using JSF2 framework and this method occurs in a ViewScoped bean & is implemented using the @PostContruct tag.

Cheers

  • 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-26T21:03:30+00:00Added an answer on May 26, 2026 at 9:03 pm

    I suggest moving the if statement to another method to decrease coupling and then moving the setItems to another method and sending the results of getmethods as parameters. (better if you modify the fields of an object)
    just pseudocode:

     return createArray();
    
     private List createArray()
     {
         if(condition1){
            setItems(getMethod(),getMethod(),getMethod());
            return ArrayList();
         }
         if (condition2) {
            setItems(getMethod(),getMethod(),getMethod());
            return ArrayList();
         } 
         ...
     }
    
     private void setItems(Object var1, Object var2, Object var3)
     {
         ...
     }
    

    If you want to get rid of the if completely, you can use polymorphism and dependency injection, but it can be overkill in this case…

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

Sidebar

Related Questions

I have several fairly large XML files that represent data exported from a system
I have a fairly large makefile that creates a number of targets on the
I have a fairly large webapp that I run on a Media Temple server.
I have a fairly large application that uses WPF for its user interface. I
I have a fairly large SQL statement which has a number of inner joins
I have a Python program that processes fairly large NumPy arrays (in the hundreds
I have a fairly large Indesign file with a text field that needs to
I have a fairly large ivy.xml containing a number of configurations which are the
I have fairly large C++ library with several sub-libraries that support it, and I
I have a fairly large CRUD WinForm app that has numerous objects. Person, Enrollment,

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.