Improve syntax in code block

Dear all,

I'm looking to make my program code more concise is there a better way to write the below code block?


if (((hour == hours) && (min == mins))) {

					if (!weekday) {
						value = s.getWeekend();
					} else {
						if (scenarioNumber == 1) {
							value = s.getScenario1();
						} else if (scenarioNumber == 2) {
							value = s.getScenario2();
						} else if (scenarioNumber == 3) {
							value = s.getScenario3();
						} else if (scenarioNumber == 4) {
							value = s.getScenario4();
						} else if (scenarioNumber == 5) {
							value = s.getScenario5();

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well the most obvious improvement would be to modify all of these getScenario1()...getScenario5() methods to be
     getScenario(int scenarioNumber) { }
and then you can replace all of those ifs with:
     value = s.getScenario(scenarioNumber) ;

That assumes that each scenario has something in common.
If they are completely different then of course it just pushes the if inside getScenario().

But presumably they do have something in common, since they're all in the "s" class?

Other options would be:
 - Use reflection to lookup the method name from a string (super ugly)
 - Create multiple classes derived from a base class, each with a "getScenario" method (overkill perhaps but ultimately cleaner)

Any help?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I would also refactor the call to s.getWeekend() into the new method described by Doug above... but perhaps rename it if getScenario() doesnt quite describe the weekend case.
CPColinSenior Java ArchitectCommented:
Hi AndyC1000,

That code looks clear and concise enough to me. You don't need to worry.
Jim CakalicSenior Developer/ArchitectCommented:
I agree with Doug that the preferable ("ultimately cleaner" and more object-oriented) refactoring would appear to be Replace Conditional with Polymorphism. It's hard to say without greater context whether, in fact, this would be helpful or not.  I'll let the refactoring page speak for itself. I think you'll see a fairly strong resemblance between the structure of your problem above and the structure of the code example there.

Best Regards,
Jim Cakalic
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.