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. General Programming
  3. C#
  4. Do Lambda expressions smell?

Do Lambda expressions smell?

Scheduled Pinned Locked Moved C#
linqcsharpdatabasefunctionalquestion
9 Posts 8 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.
  • U Offline
    U Offline
    User 4483848
    wrote on last edited by
    #1

    I saw some nasty code recently. A function took a parameter of a type that was something like Func. You get no clues about the parameters. When (if ever) should we use Lambdas? They seem like a shortcut which will save some time during the initial development, but lose more time later. I think the old fashioned way will almost always be more readable. One place where lambdas tend to be useful is when using LINQ (to SQL or Entity Framework). Is it really that good though? SQL is designed for the job, and performs MUCH better, so why use LINQ to SQL? If we have automated tests then we can fully test that the code is correctly wired up to the SQL.

    I E P J B 5 Replies Last reply
    0
    • U User 4483848

      I saw some nasty code recently. A function took a parameter of a type that was something like Func. You get no clues about the parameters. When (if ever) should we use Lambdas? They seem like a shortcut which will save some time during the initial development, but lose more time later. I think the old fashioned way will almost always be more readable. One place where lambdas tend to be useful is when using LINQ (to SQL or Entity Framework). Is it really that good though? SQL is designed for the job, and performs MUCH better, so why use LINQ to SQL? If we have automated tests then we can fully test that the code is correctly wired up to the SQL.

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      They're useful shortcuts. Func is just a shortcut for when you don't want to define a delegate yourself. These are all pretty much equivalent:

      Func<string, bool>
      Predicate<string>
      delegate bool MyStringTest(string s)

      Lambda expressions themselves, I find extremely useful... I love being able to type:

      People.Where(a=>a.FirstName.StartsWith("B"));

      This use of lambdas has nothing to do with SQL, aside from a syntax resemblance between the long-form LINQ queries and SQL queries (I prefer the shorthand notation). Remember that LINQ extensions aren't only made for database access... They're great for working with collections.

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      M 1 Reply Last reply
      0
      • U User 4483848

        I saw some nasty code recently. A function took a parameter of a type that was something like Func. You get no clues about the parameters. When (if ever) should we use Lambdas? They seem like a shortcut which will save some time during the initial development, but lose more time later. I think the old fashioned way will almost always be more readable. One place where lambdas tend to be useful is when using LINQ (to SQL or Entity Framework). Is it really that good though? SQL is designed for the job, and performs MUCH better, so why use LINQ to SQL? If we have automated tests then we can fully test that the code is correctly wired up to the SQL.

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        A lambda expression makes a good short-cut for many well known items, every wanted a one off-anon event, lambda, want to sort a list and not implement IComparable, lambda. However, I often refactor many of my lambda expressions into real code.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

        L 1 Reply Last reply
        0
        • U User 4483848

          I saw some nasty code recently. A function took a parameter of a type that was something like Func. You get no clues about the parameters. When (if ever) should we use Lambdas? They seem like a shortcut which will save some time during the initial development, but lose more time later. I think the old fashioned way will almost always be more readable. One place where lambdas tend to be useful is when using LINQ (to SQL or Entity Framework). Is it really that good though? SQL is designed for the job, and performs MUCH better, so why use LINQ to SQL? If we have automated tests then we can fully test that the code is correctly wired up to the SQL.

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

          Member 4487083 wrote:

          the old fashioned way will almost always be more readable

          I'm with you, mainly because I'm old-school :cool:. But I suppose it's similar to using a Regular Expression rather than writing a custom parser. Or even like including SQL in your code rather than calling an API of some sort.

          J 1 Reply Last reply
          0
          • P PIEBALDconsult

            Member 4487083 wrote:

            the old fashioned way will almost always be more readable

            I'm with you, mainly because I'm old-school :cool:. But I suppose it's similar to using a Regular Expression rather than writing a custom parser. Or even like including SQL in your code rather than calling an API of some sort.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            PIEBALDconsult wrote:

            But I suppose it's similar to using a Regular Expression rather than writing a custom parser. Or even like including SQL in your code rather than calling an API of some sort.

            Pretty good analogy actually. Mainly because people misuse those all the time too. And as for "custom parser" there are probably quite a few cases where people attempt to use a regex when they problem can be solved with nothing more than a simple index or split.

            1 Reply Last reply
            0
            • U User 4483848

              I saw some nasty code recently. A function took a parameter of a type that was something like Func. You get no clues about the parameters. When (if ever) should we use Lambdas? They seem like a shortcut which will save some time during the initial development, but lose more time later. I think the old fashioned way will almost always be more readable. One place where lambdas tend to be useful is when using LINQ (to SQL or Entity Framework). Is it really that good though? SQL is designed for the job, and performs MUCH better, so why use LINQ to SQL? If we have automated tests then we can fully test that the code is correctly wired up to the SQL.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Member 4487083 wrote:

              When (if ever) should we use Lambdas?

              Almost never.

              Member 4487083 wrote:

              and performs MUCH better, so why use LINQ to SQL?

              Perhaps to demonstrate that one doesn't in fact know exactly what you said?

              1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                A lambda expression makes a good short-cut for many well known items, every wanted a one off-anon event, lambda, want to sort a list and not implement IComparable, lambda. However, I often refactor many of my lambda expressions into real code.

                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #7

                Ennis Ray Lynch, Jr. wrote:

                I often refactor many of my lambda expressions into real code

                True. Same with the Nullable<T> and Tuple<x,y,z>.

                Greetings - Jacek

                1 Reply Last reply
                0
                • I Ian Shlasko

                  They're useful shortcuts. Func is just a shortcut for when you don't want to define a delegate yourself. These are all pretty much equivalent:

                  Func<string, bool>
                  Predicate<string>
                  delegate bool MyStringTest(string s)

                  Lambda expressions themselves, I find extremely useful... I love being able to type:

                  People.Where(a=>a.FirstName.StartsWith("B"));

                  This use of lambdas has nothing to do with SQL, aside from a syntax resemblance between the long-form LINQ queries and SQL queries (I prefer the shorthand notation). Remember that LINQ extensions aren't only made for database access... They're great for working with collections.

                  Proud to have finally moved to the A-Ark. Which one are you in?
                  Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                  M Offline
                  M Offline
                  Member 4448460
                  wrote on last edited by
                  #8

                  Exactly, it was made for querying an array of objects. If you are working with db connection you would more use T-SQL. With using lamdba's, yeah, they are handy but if the code is long, or if the method is virtual/abstract I would prefer not to and more than not, I mainly only use them in the LINQ querying on array types unless it is complex where I would return Predicate/Func/Action from a methods also where I could add some more parameters to help my need in filtering.

                  1 Reply Last reply
                  0
                  • U User 4483848

                    I saw some nasty code recently. A function took a parameter of a type that was something like Func. You get no clues about the parameters. When (if ever) should we use Lambdas? They seem like a shortcut which will save some time during the initial development, but lose more time later. I think the old fashioned way will almost always be more readable. One place where lambdas tend to be useful is when using LINQ (to SQL or Entity Framework). Is it really that good though? SQL is designed for the job, and performs MUCH better, so why use LINQ to SQL? If we have automated tests then we can fully test that the code is correctly wired up to the SQL.

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #9

                    Func is a generic type, you'd usually have information about the parameters. Lambdas are really nice for simple functions (in the mathematical or FP sense), where the boilerplate text for defining a method would be a significant portion of the code, and where the calculation type is simple enough that everything can be inferred (by the reader; the computer has no problem anyway!). I've also seen them used for event handlers, where the parameter types are well known. LINQ to SQL, if used correctly, creates queries very similar to what you'd make yourself, and manages the result in a much more friendly fashion. It doesn't have that much to do with lambdas though apart from the query related functions that take a lambda parameter.

                    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