Honest functions – definition and understanding
A few days after his conversation with Julia about pure functions and side effects, Steve was eager to learn more. He called Julia to ask what he should study next.
Julia: Let’s talk about honest functions, null
, and Option
types,’ she suggested. ‘These concepts are crucial for writing clear, predictable code.
Steve: Sounds great! But what exactly are honest functions?
Julia: An honest function provides a clear, unambiguous contract between the function and its callers, resulting in code that’s more robust, less prone to bugs, and easier to reason about.
So what exactly is an honest function?
In the simplest terms, an honest function is one where its type signature fully and accurately describes its behavior. If a function claims that it will take an integer and return another integer, then that’s precisely what it will do. There are no hidden gotchas, no exceptions thrown out of the blue, and no subtle changes to global or static state that aren’t reflected in the function’s signature.
Consider the following C# function:
public static int Divide(int numerator, int denominator) { return numerator / denominator; }
At first glance, this function seems to fulfill its promise. It takes two integers and returns their quotient. However, what happens when we pass zero as the denominator? DivideByZeroException
is thrown. The function was not entirely honest with us; its signature did not warn us about this potential pitfall. An honest version of this function would explicitly signal the potential for failure in its signature.
So, how does this concept of honesty tie into the broader context of functional programming and the software development industry? In functional programming, functions are the building blocks of your code. The more clearly and honestly these functions describe their behavior, the easier it is to build larger, more complex programs. Each function serves as a reliable component that behaves just as its signature describes, allowing developers to confidently compose and reuse these functions.
As Julia explained honest functions, Steve’s eyes lit up with understanding.
Steve: So it’s like when I tell my teammates I’ll deliver a feature by Friday, I should actually do it?
Julia: Exactly! In programming, our functions should be just as reliable.
Now, let’s dive deeper into the benefits of using honest functions:
- Improved readability: Honest functions make the code more straightforward to read and understand. There’s no need to dive into the implementation to grasp what the function is doing. The function’s signature is a contract that accurately describes its behavior.
- Enhanced predictability: With honest functions, surprises are drastically minimized. The function’s behavior is precisely what is described in the signature, leading to fewer unexpected bugs and exceptions at runtime.
- Increased reliability: By minimizing surprises and explicitly handling potential error scenarios, honest functions result in a more robust code base that can withstand the rigors of real-world usage.
Consider a code base where every function is honest. Any developer, familiar or not with the code, could look at a function signature and know immediately what it does, what it needs, and what it might return, including any potential error conditions. It’s like having a well-documented code base without the need for verbose documentation. That’s the power and promise of honest functions.
In the following sections, we’ll explore how to implement honest functions in C# and how to deal with potential dishonesty, such as nulls or exceptions. Fasten your seatbelts, because our journey into the world of honest and dishonest functions is just beginning!