Dividing a function into separate validations and implementa

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
SUMMARY

The discussion focuses on the design choice made by Jon Skeet in C# regarding the separation of validation and implementation in the custom LINQ method `Where`. The method is divided into `Where` for parameter validation and `WhereImpl` for the actual filtering logic. This design allows for better code reuse and clearer separation of concerns, despite the potential for increased code length and overhead. The rationale behind this approach is to prevent `NullReferenceException` errors that may arise from deferred execution in LINQ queries.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with LINQ (Language Integrated Query)
  • Knowledge of exception handling in C#
  • Concept of deferred execution in LINQ queries
NEXT STEPS
  • Explore C# exception handling best practices
  • Learn about LINQ deferred execution and its implications
  • Investigate design patterns for separating concerns in C#
  • Study Jon Skeet's coding practices and principles in C#
USEFUL FOR

C# developers, software engineers, and anyone interested in best practices for method design and exception handling in LINQ.

SlurrerOfSpeech
Messages
141
Reaction score
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
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   Reactions: FactChecker
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.
 

Similar threads

Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
3
Views
2K
Replies
1
Views
3K