Using Lambdas to Avoid Gnarly IF Statements
Do you have a function in your C# code somewhere that takes a parameter - the type doesn't matter - and then executes another function based on that parameter's value?
public void what_to_do(string param)
{
if (param == "this")
DoThis();
else if (param == "that")
DoThat();
else
DoSomethingElse();
}
Whenever I see "else if", my spider-sense starts tingling. Adding another "else if" is just too easy and the result can be cumbersome. I do use "else if" (and "switch" - it's just as vulnerable to this idea), but I don't always like it. The good news is that there is a fix - if you're willing to think "functionally". The bad news is it took me so long to piece it all together.
When did this epiphany come? At the latest MichiPUG meeting, while Mark Ramm was demonstrating a Python library for implementing generics, he displayed an interesting technique for avoiding the Python version of the above code. Here's my rendition of what he showed us:
whatToDoDictionary = {'this':do_this, 'that':do_that}
def what_to_do(param):
if param in whatToDoDictionary:
whatToDoDictionary[param]();
else:
do_something_else();
The key to this bit of Python is the values in the whatToDoDictionary. Instead of your standard string or integers, the values are functions. When we call the what_to_do method, we look to see if the parameter is present as a key in the dictionary. If it is, we simple execute the value portion of the key/value pair. Voilà! No more "else if"!
Can we do this in C#? Sure. Let's do it using lambdas.
Dictionary<string, Action> dict =
new Dictionary<string, Action>();
public Lambda()
{
dict.Add("this", () => do_this());
dict.Add("that", () => do_that());
}public void WhatToDo(string param)
{
if (dict.ContainsKey(param))
dict[param]();
else
do_something_else();
}
The concept is exactly the same. Build a dictionary of lambdas (functions in Python), see if the parameter passed to the WhatToDo(string) is in the dictionary and then execute the lambda if it is.






1 Comments:
Just a note that the following chunk of code
public Lambda()
{
dict.Add("this", () => do_this());
dict.Add("that", () => do_that());
}
I was so caught up in using a lambda, I neglected to mention that the most readable code would look something like the following:
public Lambda()
{
dict.Add("this", do_this);
dict.Add("that", do_that);
}
Much better.
By
Darrell Hawley, at Wed Dec 17, 06:28:00 PM
Post a Comment
<< Home