• Post Reply Bookmark Topic Watch Topic
  • New Topic

A LOT of if statements  RSS feed

 
kristof vanhaeren
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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 ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
  • 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"?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!