More fluent C# / .NET

后端 未结 21 1180
名媛妹妹
名媛妹妹 2020-12-23 22:51

A co-worker of mine came up with this and I wonder what others think? Personally, I find it interesting but wonder if it is too big a departure? Code examples below. Extensi

相关标签:
21条回答
  • 2020-12-23 23:28

    That is some extension method abuse if I ever saw it!

    0 讨论(0)
  • 2020-12-23 23:28

    I think the question of readability is subjective and I personally have no issue with what you've done. I would consider using it if your organization "approved" it.

    I think the concept is sound and if you changed "With" to "Let" it would be more "functional" or "F#-ish". Personal opinion.

    Page.FindControl("LocationDropDownList")    
        .CastAs<DropDownList>()    
        .Let(d => d.Visible = true)  
        .Let(d => d.SelectedValue = "123");
    
    0 讨论(0)
  • 2020-12-23 23:30

    Amongst my rules of thumb for writing clear code is: put all side effects in statements; non-statement expressions should have no side effects.

    Your first version of the program clearly follows this rule. The second version clearly violates it.

    An additional thought: if I were to read code like the code you've displayed, I would naturally assume that the purpose of the code was to build up a lazily-evaluated structure which represented those operations -- this is exactly why query comprehensions in C# 3 are built in this way. The result of the query expression is an object representing the deferred application of the query.

    If your intention is to capture the notion of "execute these side effects in a deferred manner at a later moment of my choosing", then this is a sensible approach. Essentially what you're building up is a side-effecting monad. If your intention is merely to provide a different syntax for the eagerly executed code, then this is just confusing, verbose and unnecessary.

    0 讨论(0)
  • 2020-12-23 23:31

    If this was a language feature:

    With(Page.FindControl("LocationDropDownList") as DropDownList)
    {
        Visible = true;
        SelectedValue = "123";
        if(isAdmin)    
          Add(new ListItem( "111"));
    }
    

    You would win something:

    • avoid redundancy of the mutated object
    • all language features available in the "With" block

    Above tries to emulate the style without reaping the benefits. Cargo Cult.

    (Note: I do understand the various arguments against it, but It'd still be nice)


    Incidentally, some of my C++ Win32 UI Helpers contain setters that use chaining similar what you want to achieve:

    LVItem(m_lc, idx).SetText(_T("Hello")).SetImg(12).SetLParam(id);

    In that case, I least win the "no redundancy", but that's because I don't have properties.

    0 讨论(0)
  • 2020-12-23 23:37

    I predict the whole "fluent interface" fad will be the "hungarian notation" of the 2000's. I personally think it doesn't look very clean and it runs the risk of becoming very inconsistent if you have multiple developers each with their own preference.

    0 讨论(0)
  • 2020-12-23 23:39

    Looks like your co worker is a Lambda Junkie.

    0 讨论(0)
提交回复
热议问题