Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a couple of approaches to a problem, and whilst I can get what I need to work I want to know if it is good practice to have a set of nested If-Then's inside a Case statement.

What I have works, but looks ugly.
Posted

Dalek, you're a mature developer, may need a serious answer.
I appreciate you're trying to refine technique for the elementary stuff.

Working with nested structures highly depend on the language. I used to avoid nesting with Pascal and allow myself 1-2 levels more with C#, due to several factors. Readability of C# is not one of them (it's actually worse); it's more of the ability to declare local variable anywhere in the body.

Generally, I try to avoid nesting like you describe by abstracting out nested parts to separate methods. Chances are, they will also can be reused. You may say that you cannot abstract out an if block as a function, because if may not be a predicate, but may have a body of different statements under the if block. In this case 1) you can always use a delegate as a parameter of such function; 2) if may give you a chance to re-view the code of the block and generalize/parametrize the actions under the branches. In other word, a mere attempt to reduce nesting might push you into better reuse and abstraction.

But here I wanted to mention a fun part. Do you want to know how you can eliminate switch block of an if block completely?

Tho is very useful if there too many switch cases of if branches. I more fight against switches and think about using this technique if the number of cases goes beyond 4 or so, and the branches are too long.
The technique is based on a dictionary. What was a case selector becomes a key of a dictionary, and a value of a dictionary is a delegate, used to "invoke some action in response to a key". Selection of action and a call to delegate instance ("action invocation") is done in a single call like result = Dispatcher.Invoke(key). Somebody demonstrated this technique, but mine is better. It is completely generic and has a multi-cast variant (a queue of delegates, invocation returns array of results). The funny part is that can server as a model of "another OOP". In fact, it was too parallel mechanisms of OOP in Object Pascal: one based in traditional VMT, another - on DMT ("Dynamic method table"). Internally, DMT should be what I describe. Also, sometime need to abuse OOP a bit -- keys could be types. I know at least one use case when such abuse if perfectly justified.

When you have many cases, it also essentially improves performance.

Maybe, I should publish it. If you're interested, I'll show you. This is C#, but with .NET there no essential language boundaries.
 
Share this answer
 
Comments
fjdiewornncalwe 18-Jan-11 11:49am    
This is a very interesting concept. I like it and can think of at least a half dozen practical implementations in projects I'm currently involved with right away. I would encourage you to write an article on the implementation of this. I understand the whole concept and would probably be able to implement it quite easily myself, but it would be hugely beneficial to everyone on the site to have that here.
Sergey Alexandrovich Kryukov 18-Jan-11 11:53am    
Thank you Marcus, I will try to publish it.
I already offered the idea (or part of it) 2-3 times in Answers. The implementation is ready. The only problem is that it's too little for a CodeProject typical article (no demo can be shown, for example) -- who cares though...
Sergey Alexandrovich Kryukov 29-May-11 23:10pm    
Marcus, Dalek, I published the article on this work.

Thank you very much for this question and the idea to write the article on the topic. I actually added many interesting improvements and additional generic class.

Please see:
http://www.codeproject.com/KB/dotnet/DynamicMethodDispatcher.aspx

--SA
wizardzz 18-Jan-11 14:19pm    
Interesting approach.
Sergey Alexandrovich Kryukov 29-May-11 23:11pm    
Thank you very much.
I just published my CodeProject article on the topic, please see.
--SA
You could have nested switch cases - that might actually be better than nested if's.
 
Share this answer
 
Comments
Dalek Dave 18-Jan-11 5:21am    
Thanks.
Sergey Alexandrovich Kryukov 18-Jan-11 10:33am    
I disagree -- a matter of personal taste though, not to argue to much.
--SA
Abhinav S 18-Jan-11 13:58pm    
You might also want to move some of the nested code into a separate method - would make reading code and maintainibility easier.
Generally I try not to have too much nesting inside Case statement. I make a separate method for each choice and work inside it. Readability is better preserved in that case.
 
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