Syntactic Sugar

clip_image002

Well, I wish there was a way of getting away from SyntaticSugar static class initial point, and be able just to do plain code

Do(() => request.Headers = Headers).If(HeadersExist);

Or even better, this

(request.Headers = Headers).If(HeadersExist);

There’s always hope for next version of C# ;)

4 Comments

  • You can almost do this if you cast the action.

    Example:
    ((Action)(s => Console.WriteLine(s))).If("hello", s => !string.IsNullOrEmpty(s));

    public static class Extensions
    {
    public static void If(this Action action, T value, Func condition) {
    if (condition(value)) {
    action(value);
    }
    }
    }

  • @Joe,
    Yeap, that works as well, but the noise in front (casting would make it less appealing that anything else that already exists).

    @JV,
    I agree with you for some case, and for some disagree. Sometimes I want to see all the actions that suppose to happen, without diving into conditional flows. This is where it would be handy to have syntax like that.

  • This looks like you are turning c# into Yoda speak. To me this is less readable.

  • I agree with Mike. What difference is there really. You still have an "If". In general I like syntactic sugar, but only when it makes a clear difference in readability.

    If you want to get rid of the "if" look at other ways to change your design.

Comments have been disabled for this content.