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.
  • 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
    • R Rowdy Raider

      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 Offline
      C Offline
      codejager
      wrote on last edited by
      #42

      No, it is not. I really want committed and released code to break on that line on a future change of the GetOrder return type. While developing 'var' is my friend and when done the "use explicit type" refactor option is. :-)

      1 Reply Last reply
      0
      • C Camilo Reyes

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

        C Offline
        C Offline
        codejager
        wrote on last edited by
        #43

        Exactly.

        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
          codejager
          wrote on last edited by
          #44

          Yes, you are but so am I. I guess that linq is all about lazyness, don't you think? ;-) Instead of using single char variable names I mostly use a short descriptive name for the type like 'car' or 'person'. In this case I'd use 'ivpair' instead of sortItem, since the OrderBy is clear enough for me. Also 'item' adds no more clarity than 'i' or 't' because the item type is irrelevant for sorting. The lazyness is IMHO encouraged by the numerous examples available and course exercises; they tend to save on keystrokes and sacrifice maintainance. Can't change the world though...

          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
            Steven Melendez
            wrote on last edited by
            #45

            I have to agree to an extent. Consider the following:

            var user = new User();

            This is fine imho because it explicitly states the type on the right. I don't mind seeing types on the right with a var on the declaration of type because it is still right there.

            User user = new User();

            This just feels unnecessary to me because you are stating it in two places;

            var user = GetUser(userId);

            I don't like this one either as the type of user can be assumed, but isn't immediately obvious in more complicated examples. In this example, sure, you can assume user, but what if you were actually returning:

            IQueryable user = GetUser(userId);

            because for some odd business reason, a user consisted of multiple users. It is still a single base user in this example that logged in, but he has multiple user objects. Returning the IQueryable allows us to further filter afterwards, but it isn't obvious. So, my own idea of a go to is if it is explicitly stated on the right, var on the left is fine. If not, then no. Even if it appears to be an obvious return value, you don't truly know unless you have intimate knowledge of the code.

            Steve

            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

              A Offline
              A Offline
              Al Chak
              wrote on last edited by
              #46

              I program 30+ years. 30+ years before I'd start programming, i,j,k,n were well known cycle counter variables for one page code.

              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