FluentLayout / Cirrious.FluentLayout

FluentLayout for Xamarin.iOS - sample uses MvvmCross
Microsoft Public License
149 stars 54 forks source link

Should Plus and Minus (and others) take optional parameters? #17

Closed slodge closed 8 years ago

slodge commented 9 years ago

Inspired by Softlion's latest pull - #16 - I've been thinking about optional parameters.

Part of me doesn't really like optional parameters as they don't feel very Fluent....

However, they do help reduce the code size down....

With this in mind, I'm wondering... should Plus and Minus take optional parameters?

If they did then we could have:

    public FluentLayout Plus(nfloat? constant)
    {
        if (constant.HasValue)
            Constant += constant.Value;
        return this;
    }

from https://github.com/slodge/Cirrious.FluentLayout/blob/7f2b8eeae730c5fff281c3bd6af943b8f4ed3734/Cirrious.FluentLayout/FluentLayout.cs#L56

and this would then mean that "advanced" methods could just pass optionals straight through - e.g.:

    public static FluentLayout AtTopOf(this UIView view, UIView parentView, nfloat? margin = null)
    {
        return view.Top().EqualTo().TopOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin));
    }

would become:

    public static FluentLayout AtTopOf(this UIView view, UIView parentView, nfloat? margin = null)
    {
        return view.Top().EqualTo().TopOf(parentView).Plus(margin);
    }
gshackles commented 8 years ago

While I think this makes some sense from the perspective of internal code cleanup, I don't think it makes for a good external-facing API. To me, it doesn't make a whole lot of sense for a Plus or Minus method to be called unless you have a value to pass into it, so forcing that onto the caller makes more sense to me. Feel free to reopen if you disagree :smile: