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.
  • C Offline
    C Offline
    Chris Maunder
    wrote on last edited by
    #1

    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 S C S A 24 Replies 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
      PIEBALDconsult
      wrote on last edited by
      #2

      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.

      L W R 3 Replies 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
        Super Lloyd
        wrote on last edited by
        #3

        Haha, if I touch the code, I refactor it just in the opposite direction! sortItem => i ! ;p

        All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

        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
          Camilo Reyes
          wrote on last edited by
          #4

          Sorry, `item` is about as meaningful as `t`. Except typing out item is more verbose.

          C 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
            Steve Wellens
            wrote on last edited by
            #5

            We can only hope they are stuck maintaining their own code. I loathe the lazy use of var.

            I 1 Reply Last reply
            0
            • S Steve Wellens

              We can only hope they are stuck maintaining their own code. I loathe the lazy use of var.

              I Offline
              I Offline
              ImHere2Learn
              wrote on last edited by
              #6

              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

              D OriginalGriffO S S 4 Replies 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

                A Offline
                A Offline
                Agent__007
                wrote on last edited by
                #7

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

                You have just been Sharapova'd.

                Z 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

                  V Offline
                  V Offline
                  V 0
                  wrote on last edited by
                  #8

                  It was still taught in school 10 years ago. I actually like the single letter counters as it is an indication in itself. Personally I mostly use it in for loops. as PIEBALDconsult said: it's a throw away.

                  V.
                  (MQOTD rules and previous solutions)

                  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
                    CPallini
                    wrote on last edited by
                    #9

                    Do you find LINQ too much terse and add verbosity on purpose? :-D On the serious side, I fully agree with our PIEBALD.

                    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.

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

                      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 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

                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu Peter
                        wrote on last edited by
                        #11

                        Obviously, you never were a script kiddie...

                        Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                        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
                          Mycroft Holmes
                          wrote on last edited by
                          #12

                          You are not winning with this one you grumpy old bugger :laugh: :laugh: :laugh: 3 keystrokes wasted, I only have so many let to use so I'll settle for i etc.

                          Never underestimate the power of human stupidity RAH

                          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

                            W Offline
                            W Offline
                            Wastedtalent
                            wrote on last edited by
                            #13

                            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.

                            D M 2 Replies 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

                              D Offline
                              D Offline
                              Dominic Burford
                              wrote on last edited by
                              #14

                              Quote:

                              var money = 123m

                              One of my pet peeves :thumbsdown:

                              "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

                              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.

                                D Offline
                                D Offline
                                Dominic Burford
                                wrote on last edited by
                                #15

                                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

                                Kornfeld Eliyahu PeterK V 2 Replies 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

                                  Kornfeld Eliyahu PeterK Offline
                                  Kornfeld Eliyahu PeterK Offline
                                  Kornfeld Eliyahu Peter
                                  wrote on last edited by
                                  #16

                                  Dominic Burford wrote:

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

                                  At that suggestion I removed ReSharper for good...

                                  Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                                  "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                  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

                                    OriginalGriffO Offline
                                    OriginalGriffO Offline
                                    OriginalGriff
                                    wrote on last edited by
                                    #17

                                    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...

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                    R P 2 Replies 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

                                      R Offline
                                      R Offline
                                      Rob Philpott
                                      wrote on last edited by
                                      #18

                                      Because LINQ is evil.

                                      Regards, Rob Philpott.

                                      H 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
                                        Power Puff Boy
                                        wrote on last edited by
                                        #19

                                        I could have told you 20 years ago that that's a battle you're certain to lose. But I guess you know that by now. ;P

                                        Kitty at my foot and I waAAAant to touch it...

                                        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
                                          Duncan Edwards Jones
                                          wrote on last edited by
                                          #20

                                          Lambdas are exactly the same - thousands of inline functions all called f()... (Some people still think that Maths is the only metaphor in IT[^] )

                                          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