Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. The Laziness of LINQ

The Laziness of LINQ

Scheduled Pinned Locked Moved The Lounge
csharpdatabaselinq
46 Posts 38 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Agent__007

    Everyone prefers a minified version of code, you know...

    You have just been Sharapova'd.

    Z Offline
    Z Offline
    ZurdoDev
    wrote on last edited by
    #22

    Agent__007 wrote:

    Everyone prefers a minified version of code

    Exactly. I removed the space bar from my keyboard because I never use it. :-\

    There are only 10 types of people in the world, those who understand binary and those who don't.

    1 Reply Last reply
    0
    • C Chris Maunder

      We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

      return source.Select(t => new
      {
      Index = rand.Next(),
      Value = t
      })
      .OrderBy(p => p.Index)
      .Select(p => p.Value);

      Is it really that painful to do

      return source.Select(item => new
      {
      Index = randomiser.Next(),
      Value = item
      })
      .OrderBy(sortItem => sortItem.Index)
      .Select(sortItem => sortItem.Value);

      Or am I just too old and grumpy for my own good.

      cheers Chris Maunder

      M Offline
      M Offline
      Matthew Dennis
      wrote on last edited by
      #23

      Oh oh, I think he's complaining about me :(

      1 Reply Last reply
      0
      • I ImHere2Learn

        Why is that mate? It helps with little to no compromise, does it not?

        Dictionary> someDictionary = new Dictionary>()

        vs.

        var someDictionary = new Dictionary>()

        even

        var order = GetOrder(orderId)

        is hinting with (1) the variable name and (2) the function name and

        var users = account.Select(a => new { Name = a.FullName, Role = a.Role } )

        is simply cool. Or are you referring to things like the following?

        var money = 123m

        S Offline
        S Offline
        Steve Wellens
        wrote on last edited by
        #24

        http://weblogs.asp.net/stevewellens/can-the-c-var-keyword-be-misused[^]

        1 Reply Last reply
        0
        • P PIEBALDconsult

          I disagree, the single-letter name (in a tiny scope) clearly indicates that the reader needn't spend any extra time thinking about the value. It's a throw-away.

          W Offline
          W Offline
          Weylyn Cadwell
          wrote on last edited by
          #25

          I love when someone writes a library and uses single-letters for the parameters. It makes finding out what the function actually needs very difficult.

          1 Reply Last reply
          0
          • C Chris Maunder

            We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

            return source.Select(t => new
            {
            Index = rand.Next(),
            Value = t
            })
            .OrderBy(p => p.Index)
            .Select(p => p.Value);

            Is it really that painful to do

            return source.Select(item => new
            {
            Index = randomiser.Next(),
            Value = item
            })
            .OrderBy(sortItem => sortItem.Index)
            .Select(sortItem => sortItem.Value);

            Or am I just too old and grumpy for my own good.

            cheers Chris Maunder

            B Offline
            B Offline
            Bassam Abdul Baki
            wrote on last edited by
            #26

            Yes and yes.

            Web - BM - RSS - Math - LinkedIn

            1 Reply Last reply
            0
            • D Dominic Burford

              Quote:

              I am not a big fan of var. I think it encourages lazy programming, though used well it makes sense.

              Rarely is the "var" keyword used as it was intended. I blame this on lazy programmers blindly following ReSharper's suggestion to replace every single variable declaration with "var".

              "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

              V Offline
              V Offline
              Vark111
              wrote on last edited by
              #27

              Dominic Burford wrote:

              blindly following ReSharper's suggestion to replace every single variable declaration with "var".

              My Resharper doesn't do that, and I don't think I've changed any of the defaults :confused: Even obvious stuff like: string message = string.Format("Error in xyz method: {0}", ex.Message); doesn't get flagged by it.

              1 Reply Last reply
              0
              • W Wastedtalent

                I have to admit I use single characters for linq but I do at least try to to use the first letter of the object type (Person => p) etc. I am not a big fan of var. I think it encourages lazy programming, though used well it makes sense.

                M Offline
                M Offline
                maze3
                wrote on last edited by
                #28

                thought someone would mention this (but then read the dislike of var :( ) With

                return source.Select(t => new
                {
                Index = rand.Next(),
                Value = t
                })

                What is 't' short for? Table? With loops, 'i' and 'x' (I assume) come from iterator and Maths use of variables. (then subsequently y follows from 'x') with linq, it is a sort of replacement for the item working on. In the case above 'source' so maybe 's' or 'src' if 'source' is too long to type.

                1 Reply Last reply
                0
                • C Chris Maunder

                  We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

                  return source.Select(t => new
                  {
                  Index = rand.Next(),
                  Value = t
                  })
                  .OrderBy(p => p.Index)
                  .Select(p => p.Value);

                  Is it really that painful to do

                  return source.Select(item => new
                  {
                  Index = randomiser.Next(),
                  Value = item
                  })
                  .OrderBy(sortItem => sortItem.Index)
                  .Select(sortItem => sortItem.Value);

                  Or am I just too old and grumpy for my own good.

                  cheers Chris Maunder

                  B Offline
                  B Offline
                  Brady Kelly
                  wrote on last edited by
                  #29

                  If it's a single lambda parameter, and its use is limited to a few lines, I nearly always only use a one char parameter, like:

                  var bachelors = _people.Where(p => !p.IsMarried);

                  I'm also a one letter man with basic loops. i and j all the way.

                  No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde

                  D 1 Reply Last reply
                  0
                  • C Chris Maunder

                    We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

                    return source.Select(t => new
                    {
                    Index = rand.Next(),
                    Value = t
                    })
                    .OrderBy(p => p.Index)
                    .Select(p => p.Value);

                    Is it really that painful to do

                    return source.Select(item => new
                    {
                    Index = randomiser.Next(),
                    Value = item
                    })
                    .OrderBy(sortItem => sortItem.Index)
                    .Select(sortItem => sortItem.Value);

                    Or am I just too old and grumpy for my own good.

                    cheers Chris Maunder

                    D Offline
                    D Offline
                    DumpsterJuice
                    wrote on last edited by
                    #30

                    I think everyone is right here. The var keyword was put in specifically for LINQ. Yes - it does introduce a chance to code sloppy. Its a trade off. I like to use LINQ to slice over CSV Files, and other such collection queries. I don't like var though, outside on its own, without a leash. Where there's smoke, there's a Blue Screen of death.

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

                      return source.Select(t => new
                      {
                      Index = rand.Next(),
                      Value = t
                      })
                      .OrderBy(p => p.Index)
                      .Select(p => p.Value);

                      Is it really that painful to do

                      return source.Select(item => new
                      {
                      Index = randomiser.Next(),
                      Value = item
                      })
                      .OrderBy(sortItem => sortItem.Index)
                      .Select(sortItem => sortItem.Value);

                      Or am I just too old and grumpy for my own good.

                      cheers Chris Maunder

                      C Offline
                      C Offline
                      ClockMeister
                      wrote on last edited by
                      #31

                      Well, I wouldn't say you're TOO grumpy though I always considered those three (i,j,k) to be reserved as loop variants (Ever since FORTRAN IV) and still only use 'em for that even in my C# code! Yeah, variable names ought to be a bit more descriptive!

                      1 Reply Last reply
                      0
                      • L Lost User

                        PIEBALDconsult wrote:

                        clearly indicates that the reader needn't spend any extra time thinking about the value. It's a throw-away.

                        Which is fine when you're writing it, or just reading through the code - but not when you're trying to debug non working code (esp. when more complex than the example) you need to stop and decipher.

                        PooperPig - Coming Soon

                        J Offline
                        J Offline
                        James Curran
                        wrote on last edited by
                        #32

                        Quote:

                        (esp. when more complex than the example)

                        But that's the key. We're not talking about thing more complex.

                        Truth, James

                        L 1 Reply Last reply
                        0
                        • R Rob Philpott

                          Because LINQ is evil.

                          Regards, Rob Philpott.

                          H Offline
                          H Offline
                          Herbie Mountjoy
                          wrote on last edited by
                          #33

                          Hear hear!

                          I may not last forever but the mess I leave behind certainly will.

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            It compromises all over the place! It saves you a couple of seconds of typing, but makes it harder to maintain because you can't just glance at the code and see what type a variable is. When you see code like:

                            var order = GetOrder(orderId);

                            You can't trust that order is of type Order because you are relying on the previous programmer (who you know to be lazy) to have still returned an Order from his method and not something totally different but been too lazy to change the name! Seeing it as:

                            Order order = GetOrder(orderId);

                            Make it absolutely obvious at a glance - and will immediately throw a compiler error if you change the method return type. Or if the wrong using block has been added and GetOrder comes from the wrong assembly alltogether. Yes, these are extreme cases - but an extra second or two of your time and it would be clear. var has it's place, and it's vital for Linq - but that's where it should stay! :laugh:

                            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                            R Offline
                            R Offline
                            Rowdy Raider
                            wrote on last edited by
                            #34

                            The entire point of

                            var order = GetOrder(orderId);

                            is to be able to change the return type of your method, without having to track back through all the usages of that method and update all of that code as well. That said this is the one usage of var where I get a little uncomfortable for the same reasons you said. Generally speaking code using var as opposed to the actual type is easier to work on, less touch points while refactoring.

                            C 1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              I disagree, the single-letter name (in a tiny scope) clearly indicates that the reader needn't spend any extra time thinking about the value. It's a throw-away.

                              R Offline
                              R Offline
                              Rowdy Raider
                              wrote on last edited by
                              #35

                              Agree completey, and I will add that if x,y,z,etc bother you I highly doubt the other devs on the team will care if you refactor the 'x' into 'theNewFooToInsert'. Since the linq statement is fully enclosed the refactor can be done 100% using the tools built into VS or ReSharper... making it arbitrarily easy to do.

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

                                return source.Select(t => new
                                {
                                Index = rand.Next(),
                                Value = t
                                })
                                .OrderBy(p => p.Index)
                                .Select(p => p.Value);

                                Is it really that painful to do

                                return source.Select(item => new
                                {
                                Index = randomiser.Next(),
                                Value = item
                                })
                                .OrderBy(sortItem => sortItem.Index)
                                .Select(sortItem => sortItem.Value);

                                Or am I just too old and grumpy for my own good.

                                cheers Chris Maunder

                                S Offline
                                S Offline
                                Scott Corbett
                                wrote on last edited by
                                #36

                                This isn't isolated to LINQ. You see the same behavior in for/foreach loops as well. The only time I use single letters is when the variable is for an index.

                                Scott E. Corbett

                                1 Reply Last reply
                                0
                                • C Chris Maunder

                                  We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

                                  return source.Select(t => new
                                  {
                                  Index = rand.Next(),
                                  Value = t
                                  })
                                  .OrderBy(p => p.Index)
                                  .Select(p => p.Value);

                                  Is it really that painful to do

                                  return source.Select(item => new
                                  {
                                  Index = randomiser.Next(),
                                  Value = item
                                  })
                                  .OrderBy(sortItem => sortItem.Index)
                                  .Select(sortItem => sortItem.Value);

                                  Or am I just too old and grumpy for my own good.

                                  cheers Chris Maunder

                                  P Offline
                                  P Offline
                                  patbob
                                  wrote on last edited by
                                  #37

                                  I agree with you on the first Select because there's a little complexity in that lambda, but for the OrderBy and second Select, its one line of code, and if it isn't obvious what the parameter represents and how its being used, then a longer variable name isn't going to help the reader much. Personally, for those one-liners, I prefer the simple 'p' argument -- less for my eye to read and my brain to parse (and ignore), which means I reach understanding of the code a lot faster.

                                  We can program with only 1's, but if all you've got are zeros, you've got nothing.

                                  1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    It compromises all over the place! It saves you a couple of seconds of typing, but makes it harder to maintain because you can't just glance at the code and see what type a variable is. When you see code like:

                                    var order = GetOrder(orderId);

                                    You can't trust that order is of type Order because you are relying on the previous programmer (who you know to be lazy) to have still returned an Order from his method and not something totally different but been too lazy to change the name! Seeing it as:

                                    Order order = GetOrder(orderId);

                                    Make it absolutely obvious at a glance - and will immediately throw a compiler error if you change the method return type. Or if the wrong using block has been added and GetOrder comes from the wrong assembly alltogether. Yes, these are extreme cases - but an extra second or two of your time and it would be clear. var has it's place, and it's vital for Linq - but that's where it should stay! :laugh:

                                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #38

                                    OriginalGriff wrote:

                                    been too lazy to change the name!

                                    Ha! The other day I wrote a GetType sort of method, it returned a Type. But then I decided to have it return a ConstructorInfo instead -- but I didn't change the name! :-D (Until the following day.)

                                    1 Reply Last reply
                                    0
                                    • B Brady Kelly

                                      If it's a single lambda parameter, and its use is limited to a few lines, I nearly always only use a one char parameter, like:

                                      var bachelors = _people.Where(p => !p.IsMarried);

                                      I'm also a one letter man with basic loops. i and j all the way.

                                      No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde

                                      D Offline
                                      D Offline
                                      David Days
                                      wrote on last edited by
                                      #39

                                      Brady Kelly wrote:

                                      var bachelors = _people.Where(p => !p.IsMarried);

                                      Generally, that's how I code, also. I specifically don't code that way when the LINQ code involves joins across a series of sources/tables. The

                                      (a,b)=>

                                      can get really messy, esp. if you need

                                      (a,b)=> new {}

                                      multiple times.

                                      vuolsi così colà dove si puote ciò che si vuole, e più non dimandare --The answer to Minos and any question of "Why are we doing it this way?"

                                      1 Reply Last reply
                                      0
                                      • C Chris Maunder

                                        We've spent 20 years trying to teach developers not to use variables like i,j and k. So why is it that every time I see some LINQ code it's like this:

                                        return source.Select(t => new
                                        {
                                        Index = rand.Next(),
                                        Value = t
                                        })
                                        .OrderBy(p => p.Index)
                                        .Select(p => p.Value);

                                        Is it really that painful to do

                                        return source.Select(item => new
                                        {
                                        Index = randomiser.Next(),
                                        Value = item
                                        })
                                        .OrderBy(sortItem => sortItem.Index)
                                        .Select(sortItem => sortItem.Value);

                                        Or am I just too old and grumpy for my own good.

                                        cheers Chris Maunder

                                        _ Offline
                                        _ Offline
                                        _CodeWarrior
                                        wrote on last edited by
                                        #40

                                        I have always attributed this to the "it's a sample" mentatility. In samples you variables named myObj and newVar and stuff. You should not name things like that in production code, and neither should you use single letter small scope variables. While I have seen both in production code (really, you named the search parameters myString?), I discourage their use (even in linq) in favor of more descriptive names. Perhaps we should insist on better quality examples for teaching these technologies to help instill better habits in new and learning programmers.

                                        1 Reply Last reply
                                        0
                                        • J James Curran

                                          Quote:

                                          (esp. when more complex than the example)

                                          But that's the key. We're not talking about thing more complex.

                                          Truth, James

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #41

                                          I was I think the op was talking in general, giving a simple example .

                                          PooperPig - Coming Soon

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups