views:

454

answers:

3

On a recent Dot Net Rocks podcast, Jon Skeet mentioned possible abuses of LINQ syntax. What examples have people seen where crazy things are being done with LINQ?

+3  A: 

Here are my own abuses - purely for the sake of having a laugh at a geek night, and demonstrating what the compiler actually does with query expressions.

Arguably my "LINQ to Mandelbrot" is a bit abusive too :)

I was particularly thinking of abuse of the syntax by the way, but there are always plenty of ways to abuse the very presence of LINQ - doing things "the LINQ way" when there are simpler approaches available without using LINQ. For instance, getting to the nth element of an array:

// Sensible (we know that people implements IList<Person>)
Person x = people[10];
// Insane
Person y = people.Skip(9).First();

I suspect there will be more cases of abuse like this than abusing the power of query expressions, partly because many devs won't realise that abusing query expressions will even work :)

Jon Skeet
Why would anyone ever do Skip.First when they could do ElementAt? ;)
Joe Chung
+13  A: 

It has to be a ray-tracer implemented in a single LINQ expression. Clever, beautiful, and scary all at the same time!

Greg Beech
The fact that it's all in one expression is scary - but quite possibly if it were broken up into separate expressions (which is easily doable due to composability) it could be clever, beautiful and the most readable way of implementing a ray-tracer. Maybe. Anonymous types might hurt it, admittedly.
Jon Skeet
I'm not sure what to say. On one hand, it's an insane abuse of language syntax. On the other, someone obviously knew what they were doing and is flaunting a talent few people have.
280Z28
+2  A: 

Honestly, it's got to be cases where people chose the LINQ syntax where the code to do so:

  • Was the same or longer than a simple loop
  • Offered no performance or correctness advantage (readability/maintainability) over a simple loop
280Z28