• Post Reply Bookmark Topic Watch Topic
  • New Topic

A better way to do a number of if statements  RSS feed

 
Jeremiah Logan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently doing some work for an insurance company, modifying some code in one of their apps to satisfy some new requirements. I know how I could do it, just wanted to bounce it off the community to see if there are any suggestions for a better way to do it. Currently, in the section I am working on, there is an ArrayList of categories that a user can choose from (about 50: i.e doctor visit, court costs, meeting, phone call, diagnostic, etc.). The current code concerning the criteria for what is added to the list is below:


So there is currently 1 category only (BACKUP_WITHHOLDING) that should only be shown to the user under specific circumstances (if ExposureType is either LEGALEXPENSE or WCINJURYDAMAGE). Otherwise, all other categories seem to be falling through. The requirement for this change is to add about 6 new categories, only visible if the state is CA and the ExposureType is Medical (still under the PolicyType == TC_COMP if clause). So, I could do something like this:



but it just seems kludgy to me. I'm just wondering if there is a more robust way of doing it, so these items are only added if the state is CA, that will also allow for any future state-specific additions without the whole thing becoming a mess. I would appreciate any (constructive) criticism.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremiah Logan wrote:but it just seems kludgy to me. I'm just wondering if there is a more robust way of doing it, so these items are only added if the state is CA, that will also allow for any future state-specific additions without the whole thing becoming a mess. I would appreciate any (constructive) criticism.

Well:

1. Welcome to JavaRanch.

2. I've broken up those enormous lines of yours as best I can because it makes the thread very hard to read. Please try to avoid them by editing your code before you include it. Thanks.

3. What types are typeKey.ExposureType and typeKey.Linecategory? If they're primitives (eg, ints) or compile-time constants, you could convert those 'orrible if...else monsters to switch blocks.

However, I fear it will be merely cosmetic "fluff". That code is awful (and I realise that you probably didn't write it).

I'd say it needs a serious bit of analysis to work out WHAT it's doing, and break it up into relevant, well-named methods.

And if you can use some import statements to reduce those colossal names, I suspect you'll be doing "your world" a favour too.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give just one example of what Winston means about breaking it up in to methods, take this line:

The idea is to work out what that condition actually means, from a business point of view. Then you move it all into a function/method, and this becomes something like:

The idea is to make the logic of the function as clear as possible to someone reading it, while hiding the detail away so you don't have to read it unless it's relevant. There are probably improvements that can be made by completely rearranging the logic, if you have a complete understanding of it, but this is one way to make things a bit better without having to do that.

By the way, those function headers aren't Java, though it's obviously written in something related to Java. What is it? I'm not sure Beginning Java is the best place for this, as it's going to confuse some people.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
function? var? This isn't Java. I suspect it's JavaScript?
 
Jeremiah Logan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestions. And Winston is correct, I am not the purveyor of this particular piece of ... code. To answer Bear's question, it is actually an antiquated form of GOSU (formerly GScript) which borrows a lot from Java (which led to my posting here, as there is almost nil for GOSU forums). The language is used in this particular Claims application as something of a "Java lite" scripting language. All in all the whole experience has been a massive headache, as this is a good example of the cobbled-together feel of the code.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Maybe this forum is all right as long as we stick to the subject above. Otherwise we have different fora where it might fit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!