REUSING FUNCTIONS

Reuse is the greatest overall problem in computer programming. The assembly languages understood by processors are fine for the purpose of programming computers to do anything and everything you want — there’s nothing that can’t be done on this level. The problem is that after a very short time, small and large blocks of functionality start recurring, and programmers start trying to find ways to avoid wasting time by re-implementing code that’s already been written. If you think about it, that’s why functions in programming languages were invented in the first place: because they provide a common place for a block of code, a piece of functionality, that will presumably be used more than once.

Of course functions are not the end of the line when it comes to reusability. Especially in object oriented programming, the step beyond functions is made very quickly. Classes are used as building blocks together with interfaces and even larger modules. There are some mechanisms that work on the function level and promote reuse, and object oriented programmers have invented a slew of patterns to go along with these, many of which also work on the function level.

As a built-in language feature, C# only supports overloading of functions as a direct means of modularization on a functional level. C# 4.0 has both named and optional parameters so that the overload resolution process becomes quite complex, especially when taking other related mechanisms like inference of generic ...

Get Functional Programming in C#: Classic Programming Techniques for Modern Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.