Post

Lambda expression and small “around” method to reduce DRY

When you are acting as façade or in any other “forward-like” role sometimes error handling may come pretty boring stuff to deal with. For instance if you are acting as front-end client proxy class for some REST Api behind the scenes, code below may look familiar – that you have to replicate yourself all the time and catch for instance in this case IO error and response content format error (if they return invalid formatted Json object).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
protected void Operation1()
{
    try
    {
        // some black magic happens here..
    }
    catch (HttpRequestException e)
    {
        // handle IO issue
        return ;
    }
    catch (JsonReaderException e)
    {
        // handle response content issue
        return ;
    }
}

protected void FancyOperation2()
{
    try
    {
        // some black magic happens here..
    }
    catch (HttpRequestException e)
    {
        // handle IO issue
        return ;
    }
    catch (JsonReaderException e)
    {
        // handle response content issue
        return ;
    }
}

We are know that repetition is anything about to remember it better, but for me as design practitioner and DRY principle defender this seems to be too much. There is definitely a space to improve and reduce some of the duplicates. The most simplest solution is to utilize Lambda expressions and refactor these methods to use “around” method (for me it drives small similarities with Lisp macros). So what we could do better in this case is to create new method that receives Action and does all boring stuff like handling the error cases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
protected void Try(Action action)
{
    try
    {
        return action();
    }
    catch (HttpRequestException e)
    {
        // handle IO issue
        return ;
    }
    catch (JsonReaderException e)
    {
        // handle response content issue
        return ;
    }
}

So Operation1 and FancyOperation2 now looks much better and cleaner.

1
2
3
4
5
6
7
8
9
10
11
12
13
 protected void Operation1()
{
    Try(() => {
        // some black magic happens here..
    });
}

protected void FancyOperation2()
{
    Try(() => {
        // some black magic happens here..
    });
}

I guess it’s a bit better now.

Happy Coding! [eof]

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.