la-yumba / functional-csharp-code

Code samples for Functional Programming in C#
MIT License
584 stars 183 forks source link

public static field vs public property with get for Functions #10

Closed darkato42 closed 5 years ago

darkato42 commented 5 years ago

In Chapter07's solution, you had Remainder defined as

public static Func<int, int, int> Remainder = (dividend, divisor)
     => dividend - ((dividend / divisor) * divisor);

why don't we have them as public property? what's the difference?

public static Func<Dividend, Divisor, int> Remainder
{
    get { return (dividend, divisor) => dividend - (dividend / divisor) * divisor; }
}

In expression body:

public static Func<int, int, int> Remainder => (dividend, divisor)
     => dividend - ((dividend / divisor) * divisor);
la-yumba commented 5 years ago

Well this seems a rather vacuous question.

If you had it as a property, then you may equally ask "why not have it as a field?"

In relation to your question "what's the difference", the answer is: exactly one character (in the declaring class, which is irrelevant), and no difference in the consumer code.

So, I generally tend to prefer the simplest possible solution that gives me the desired effect, and would say that a field is simpler than a property.

On Fri, Feb 1, 2019 at 7:55 PM Yulin Wu (Jimmy) notifications@github.com wrote:

In Chapter07's solution, you had Remainder defined as

public static Func<int, int, int> Remainder = (dividend, divisor) => dividend - ((dividend / divisor) * divisor);

why don't we have them as public property? what's the difference?

public static Func<Dividend, Divisor, int> Remainder { get { return (dividend, divisor) => dividend - (dividend / divisor) * divisor; } }

In expression body:

public static Func<int, int, int> Remainder => (dividend, divisor) => dividend - ((dividend / divisor) * divisor);

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/la-yumba/functional-csharp-code/issues/10, or mute the thread https://github.com/notifications/unsubscribe-auth/AGhHaMx3cvS4Ryzzh0v2XwqDyglxGehiks5vJI2dgaJpZM4ae4wR .

--

Enrico Buonanno

email: enrico.buonanno@gmail.com skype ID: enrico.buonanno WhatsApp: +34 632 333 865 Tel: +39 333 385 5948

http://goog_395621968 [image: https://www.manning.com/books/functional-programming-in-c-sharp?a_aid=functional-programming-in-c-sharp&a_bid=ad9af506] https://www.manning.com/books/functional-programming-in-c-sharp?a_aid=functional-programming-in-c-sharp&a_bid=ad9af506

atifaziz commented 5 years ago

In relation to your question "what's the difference", the answer is: exactly one character

Actually, you would need to add the readonly modifier as well to make the field match the property:

public static readonly Func<int, int, int> Remainder = (dividend, divisor)
     => dividend - ((dividend / divisor) * divisor);

So now the difference is more than one character. :wink:

la-yumba commented 5 years ago

Yes, that's correct.

And, since it's functional programming, you never want to reassign global variables, so that in real life you'd always want to specify readonly, although in the book I probably omitted that, when it was not particularly relevant to the topic at hand, to make the examples less verbose.