Dividing a function into separate validations and implementa

  • Thread starter SlurrerOfSpeech
  • Start date
  • Tags
    Function
In summary: If an exception is thrown, it's because something went wrong with the call, not because there was no data to work with.
  • #1
SlurrerOfSpeech
141
11
So I'm reading a C# book in which the author (Jon Skeet) implements a Where function like

Code:
public static IEnumerable<T> Where<T> ( this IEnumerable<T> source, Func<T,bool> predicate ) 
{
    if ( source == null || predicate == null ) 
    {
       throw new ArgumentNullException();
    }
    return WhereImpl(source, predicate);
}

public static IEnumerable<T> WhereImpl<T> (IEnumerable<T> source, Func<T,bool> predicate ) 
{
    foreach ( T item in source ) 
    {
        if ( predicate(item) )
       {
           yield return item;
       }
    }
}

Now, I fully understand how this works, and that it's equiavlent to

Code:
public static IEnumerable<T> Where<T> ( this IEnumerable<T> source, Func<T,bool> predicate ) 
{
    if ( source == null || predicate == null ) 
    {
       throw new ArgumentNullException();
    }
    foreach ( T item in source ) 
    {
        if ( predicate(item) )
       {
           yield return item;
       }
    }
}

which brings up the question of why would one separate these into 2 functions given that there would be memory/time overhead and of course more code. I always validate parameters and if I start writing like this example then I'll be writing twice as much code. Is there some school of thought which holds that validation and implementation should be separate functions?

Additionally, in another thread I made, someone said that `Exception`s should only be thrown if there has been an "extenuating circumstance" or something like that. In this case, an `Exception` is thrown because of an argument, which doesn't quite classify as an emergency.
 
Technology news on Phys.org
  • #2
The reason to check early for null parameters is that the method returns a Linq expression that may be evaluated in a completely different context far away, making the resulting NullReferenceException at that point hard to relate to the original call.

Jon Skeet usually have very good reasons for doing what he is doing, so I would venture a guess that he separates into two methods because he reuse the WhereImpl in other situations where he knows the parameters are well defined.
 
  • Like
Likes FactChecker
  • #3
I wonder what happens if no suitable item is in the source...
SlurrerOfSpeech said:
Additionally, in another thread I made, someone said that `Exception`s should only be thrown if there has been an "extenuating circumstance" or something like that. In this case, an `Exception` is thrown because of an argument, which doesn't quite classify as an emergency.
A call of the function without source or predicate is something that should never happen.
 

Related to Dividing a function into separate validations and implementa

1. What is the purpose of dividing a function into separate validations and implementations?

Dividing a function into separate validations and implementations allows for better organization and readability of code. It also makes it easier to troubleshoot and debug any errors that may occur.

2. How do you determine which parts of a function should be validated and which should be implemented?

The validation portion of a function should consist of checks and conditions to ensure that the input is valid. The implementation portion should contain the actual code that performs the desired task or calculation.

3. Can a function be divided into more than two parts?

Yes, a function can be divided into as many parts as necessary. For example, a complex function may require multiple validations and implementations to properly execute.

4. Are there any best practices for dividing a function into separate validations and implementations?

One best practice is to keep the validation and implementation portions as small and specific as possible. This makes it easier to understand and modify the code if needed. It is also important to ensure that the two portions are properly connected and work together seamlessly.

5. Is it necessary to divide a function into separate validations and implementations for every function?

No, it is not necessary for every function. This technique is most useful for larger and more complex functions where it can improve organization and efficiency. For smaller functions, it may not be necessary and could potentially add unnecessary complexity.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
765
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top