Click here to Skip to main content
15,887,967 members
Articles / Programming Languages / C#
Tip/Trick

The Elegant Art of Programming

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 May 2012CPOL 24.7K   3   23
This is an alternative for "The Elegant Art of Programming"

This is an alternative to the original poster's "solution 1", which really does need some help.

C#
public static bool
AllTheSame<T>
(
  this System.Collections.Generic.IList<T> List
)
where T : System.IEquatable<T>
{
  if ( List == null )
  {
    throw ( new System.ArgumentNullException ( "List" , "List must not be null" ) ) ;
  }

  bool result = true ;

  if ( List.Count > 1 )
  {
    T first = List [ 0 ] ;

    if ( (object) first == null )
    {
      for ( int i = 1 ; result && ( i < List.Count ) ; i++ )
      {
        result = (object) List [ i ] == null ;
      }
    }
    else
    {
      for ( int i = 1 ; result && ( i < List.Count ) ; i++ )
      {
        result = first.Equals ( List [ i ] ) ;
      }
    }
  }

  return ( result ) ;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB, acknowledged pedant and contrarian

---------------

"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]

"Typing is no substitute for thinking." -- R.W. Hamming

"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon

"linq'ish" sounds like "inept" in German -- Andreas Gieriet

"Things would be different if I ran the zoo." -- Dr. Seuss

"Wrong is evil, and it must be defeated." –- Jeff Ello

"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw

“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

"Omit needless local variables." -- Strunk... had he taught programming

Comments and Discussions

 
SuggestionHow about a linq'ish approach? Pin
Andreas Gieriet18-May-12 4:46
professionalAndreas Gieriet18-May-12 4:46 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult18-May-12 5:31
mvePIEBALDconsult18-May-12 5:31 
AnswerRe: How about a linq'ish approach? Pin
Luc Pattyn18-May-12 6:27
sitebuilderLuc Pattyn18-May-12 6:27 
GeneralRe: How about a linq'ish approach? Pin
Andreas Gieriet18-May-12 21:14
professionalAndreas Gieriet18-May-12 21:14 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult19-May-12 5:01
mvePIEBALDconsult19-May-12 5:01 
AnswerRe: How about a linq'ish approach? Pin
Luc Pattyn19-May-12 5:17
sitebuilderLuc Pattyn19-May-12 5:17 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult18-May-12 7:38
mvePIEBALDconsult18-May-12 7:38 
GeneralRe: How about a linq'ish approach? Pin
Andreas Gieriet19-May-12 2:41
professionalAndreas Gieriet19-May-12 2:41 
Well, I personally don't agree with my word game: "linq'ish" --> German "linkisch" ~ "inept"...

I find Linq and the respective C# functions (Where, Select, etc.) quite handy: you can chain them and they do late evaluation for iterating.
And I'm also a supporter for using iterators instead of indexed access whenever adequate. And I prefer many small functions over larger functions. And I employ idiomatic style where useful. And, and, and, ...

We both seem to prefer different coding styles.

Regrading "elegant": The title is badly chosen and misleading in my opinion - I would rather go for "expressive" or alike. What is elegant coding anyways (Straight to the point? Simple? ...)? I don't know. I have other expectations on coding style than being "elegant": correct, complete, robust, maintainable, documented, concise, expressive, not copy-paste, no code-bloat, universally usable, reasonable performant, "good-enough", little complexity per function...

The problem at hand is not a plain foreach problem; it is rather "get the first element (if there is any) and compare to the rest of the elements (if there are any)", so I chose to go the iterator way. See also alternatives to that further down in this comment.

To my suggestion: No, this function is programmed intentionally as it is. The changes I would agree to get the same level as linq functions: check the null source and allow for a null comparer (takes the default comparer of that type) - and maybe the formatting (adding blocks instead of the while-if-single-liner). The rest is ok for my standards.

So, I'm ok with the following code for production (place into an appropriate extension class):
C#
  1  /// <summary>
  2  /// Checks if all values are equal by means of the equality comparer.
  3  /// </summary>
  4  /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  5  /// <param name="source">The sequence to check for all equal elements.</param>
  6  /// <returns>True if empty, has only one element, or all are equal; false otherwise.</returns>
  7  /// <exception cref="ArgumentNullException">source is null</exception>
  8  public static bool AllTheSame<TSource>(this IEnumerable<TSource> source)
  9  {
 10      return AllTheSame<TSource>(source, null);
 11  }
 12  /// <summary>
 13  /// Checks if all values are equal by means of the given comparer.
 14  /// </summary>
 15  /// <typeparam name="TSource">The type of the elements of source.</typeparam>
 16  /// <param name="source">The sequence to check for all equal elements.</param>
 17  /// <param name="comparer">An IEqualityComparer<TSource> to compare values.</param>
 18  /// <returns>True if empty, has only one element, or all are equal; false otherwise.</returns>
 19  /// <exception cref="ArgumentNullException">source is null</exception>
 20  public static bool AllTheSame<TSource>(this IEnumerable<TSource> source,
 21                                         IEqualityComparer<TSource> comparer)
 22  {
 23      // Check preconditions and do default handling.
 24      if (source == null) throw new ArgumentNullException("Value cannot be null.", "source");
 25      var equalityComparer = comparer ?? EqualityComparer<TSource>.Default;
 26              
 27      // Check if any element is different to the first one and return out of the loop if so.
 28      var it = source.GetEnumerator();
 29      if (it.MoveNext())
 30      {
 31          var first = it.Current;
 32          while (it.MoveNext()) if (!equalityComparer.Equals(first, it.Current)) return false;
 33      }
 34      return true;
 35  }


You can always improve or modify to your taste (but maybe for the cost of runing twice over the first element):
C#
 27      ...
 28      var first = source.FirstOrDefault();
 29      foreach (var item in source) if (!equalityComparer.Equals(first, item)) return false;
 30      return true;
 31  }

Or:
C#
 27      ...
 28      var first = source.FirstOrDefault();
 29      return source.All(e => equalityComparer.Equals(first, e));
 30  }


BTW: You can not easily combine IEnumerable<T> and index-based for loops, and thus, Linq and index-based for do not easily combine neither.

If you stick on index-based for loop, then you have to implement different functions for the different more specific kind of collections (e.g. IList<T>, IDictionary<K, V>, HashSet<T>, etc.). You may not use any of the Linq functions since they return IEnumerable<T> that allows to chain them easily. An index-based loop needs an end-condition up-front, (something like Count()) and IEnumerable<T> is not random-access (i.e. does not allow to access an element by index). IEnumerable<T> goes hand in hand with iterators, but not with indices.

You can of course call Count(), but that already iterates over all elements to simply find out how often to move ahead in the sequence of elements...

As I said further above, we both have different coding styles and approaches. Luckily, there is no "one and only right" (dogmatic?) solution Wink | ;-)

Cheers
Andi
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult19-May-12 5:20
mvePIEBALDconsult19-May-12 5:20 
Questiona problem Pin
Luc Pattyn17-May-12 14:44
sitebuilderLuc Pattyn17-May-12 14:44 
AnswerRe: a problem Pin
PIEBALDconsult17-May-12 16:58
mvePIEBALDconsult17-May-12 16:58 
GeneralRe: a problem Pin
Luc Pattyn17-May-12 17:14
sitebuilderLuc Pattyn17-May-12 17:14 
GeneralRe: a problem Pin
PIEBALDconsult17-May-12 17:34
mvePIEBALDconsult17-May-12 17:34 
GeneralRe: a problem Pin
PIEBALDconsult18-May-12 6:20
mvePIEBALDconsult18-May-12 6:20 
AnswerRe: a problem Pin
Luc Pattyn18-May-12 6:30
sitebuilderLuc Pattyn18-May-12 6:30 
GeneralRe: a problem Pin
PIEBALDconsult18-May-12 7:16
mvePIEBALDconsult18-May-12 7:16 
Questionargh Pin
Luc Pattyn17-May-12 3:56
sitebuilderLuc Pattyn17-May-12 3:56 
GeneralRe: argh Pin
PIEBALDconsult17-May-12 11:15
mvePIEBALDconsult17-May-12 11:15 
AnswerRe: argh Pin
Luc Pattyn17-May-12 11:34
sitebuilderLuc Pattyn17-May-12 11:34 
GeneralRe: argh Pin
PIEBALDconsult17-May-12 11:36
mvePIEBALDconsult17-May-12 11:36 
QuestionAll(q=> q == q)? Pin
CodingBruce16-May-12 13:20
CodingBruce16-May-12 13:20 
GeneralRe: All(q=> q == q)? Pin
PIEBALDconsult16-May-12 13:57
mvePIEBALDconsult16-May-12 13:57 
AnswerRe: All(q=> q == q)? Pin
John Brett17-May-12 1:01
John Brett17-May-12 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.