• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A LOT of if statements

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a model, that exists out of 12 fields (questions):



now in the controller I need to check for every field if its value is true, but I cannot use a loop (as far as I know), so I'm just using 12 if statements.
Sure there must be a much cleaner solution to this problem?

I thought of introspection, but that just slows everything down and it's even less readable for some simple like this imho.


I would like to know what a nice and simple solution could be to this problem, apart from introspection

Please note that every field (question) is mapped to a group of radiobuttons through Spring binding
grtz
[ November 20, 2007: Message edited by: kristof vanhaeren ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the Booleans in an array or List and then you could use a loop.
 
kristof vanhaeren
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, if i create an Array in the model like:

Boolean[] questions = {question1, question2, question3, ...};

will the array hold the right values for every item if some field is updated.
let's say that question3 is updated with setQuestion3(), will question3 in the array hold the correct value at any time?

And am I not negelecting the purpose of my model this way?
As the model should just be "some kind of representation"?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne meant you should get rid of the variables question1, question2... altogether, and just use an array of Boolean. Everywhere you currently use "question1", use "questions[0]".
 
kristof vanhaeren
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see

but atm the radiobuttons are mapped to the model via Spring binding like:



will this work with:



?

I would try this out, but I have no access to the code right now


thanks
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not 100% sure of your goals, but in the past when I couldn't control what the API spec was, I've used varargs to do some things like this - wrap them into an array and loop over it, return false if any one of them is false, something like that.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic