Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello,

Sorry for the subject of this question but its really hard to think about better subject.

I'm in programming sometimes in situation where I have too many variables and its really uncomfortable when I have for example 14 variables and I have to compare everyone with each other using if statement.

Do I have to use if's even if I work with so many variables or there is another way?


Thanks for any answer and sorry my English.
Posted
Updated 16-Apr-12 15:41pm
v3
Comments
Sergey Alexandrovich Kryukov 16-Apr-12 17:23pm    
This is basically an interesting question, but a bit to general and abstract, for a Quick Questions & Answers forum. I voted 4 for the question.
--SA

As I put in my comment to the question, this question is interesting. The programming avoiding ifs is a whole big topic which evoked a lot of thinking on the conceptual level.

You may find some of the articles on related topics interesting:
http://programmingwithoutifs.blogspot.com/[^],
http://www.ewernli.com/noif[^],
http://aigamedev.com/open/review/schematic-table-conditionals/[^].

According to the Wikipedia article on conditional programming, dynamic dispatch can be considered as an alternative execution choice approach. Please see:
http://en.wikipedia.org/wiki/Conditional_%28programming%29[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^].

However, this is pretty hard to understand. I would suggest my own CodeProject article, where I put forward my approach to the dynamic dispatch which helps to avoid if and switch statements:
Dynamic Method Dispatcher[^].

This work includes detailed motivation, full source code and detailed usage description and samples. You may also find it all interesting.

—SA
 
Share this answer
 
Comments
Nelek 16-Apr-12 17:49pm    
Nice answer and good links. +5
Sergey Alexandrovich Kryukov 16-Apr-12 18:08pm    
Thank you, Nelek.
--SA
VJ Reddy 16-Apr-12 20:29pm    
Nice answer and good links. 5!
Sergey Alexandrovich Kryukov 16-Apr-12 21:01pm    
Thank you, VJ.
--SA
RaisKazi 16-Apr-12 21:42pm    
My 5.
I think it really depends on the type of conditions you are attempting to run.
If they it’s the same condition for all variables, ( null, less than etc ) then adding all the variables in to a list and running the if against all items in the list might work nicer.

If however you have several different conditions you’d like to run against them, you could create a list of func and a list of pairs ( variable + tests ) and run a loop like
Foreach item in my list
Foreach item in the test…

Now you’re creating more work in the infrastructure…… I don’t think there is 1 answer fits all when it comes to general questions like this. But they are sure fun to deal with when you have specifics to figure out which best fits



my 2 cents anyway
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900