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. Using IEnumerable nonsense for everything

Using IEnumerable nonsense for everything

Scheduled Pinned Locked Moved The Lounge
questioncsharp
124 Posts 41 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.
  • L Lost User

    You've probably seen this style if you're done anything with C# after 2007 or so. someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e)); Instead of, you know, a plain old `for` loop with an `if` in it and so on. Or maybe `foreach` if you want to be fancy. So, now we have nearly a decade of experience with this, can we finally settle this question: Is this style cancer? I still think it is, and the retort "you just have to get used to it" isn't going to work any more. I file this firmly under "stupid one-liner 'clever' code with no benefits to compensate". Yes, I've argued in the past that "clever code" isn't necessarily bad, and I'll keep saying that - there's a time and a place for it. But not if you're just trying to be cute. "Oh look at me, I put everything on one line, +1 nerd points for me" And this is even worse. It's not just cute with no benefits to compensate, it's cute and harder to read. Side question, why is this style popular?

    M Offline
    M Offline
    Mark_Wallace
    wrote on last edited by
    #42

    harold aptroot wrote:

    Is this style cancer?

    Absolutely! A page full of IF...GOTO statements looks far more organised! You don't even need ELSEs, or any of that indentation that makes the page a mess!

    I wanna be a eunuchs developer! Pass me a bread knife!

    1 Reply Last reply
    0
    • M Marc Clifton

      harold aptroot wrote:

      t's still "codegolfing but with longer method names" to me.

      Well, if readability, simplicity, maintainability, and a more functional programming syntax style don't sell you, then I don't know what will. :) And an FP style is often times better because those "long method names" are descriptive of what is happening, rather than having to look at code to figure out what is happened. Marc

      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

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

      Marc Clifton wrote:

      readability, simplicity, maintainability,

      These might have sold it to me if they were true. They're true for you, but not for me. The functional syntax I see as detrimental.

      J 1 Reply Last reply
      0
      • L Lost User

        You've probably seen this style if you're done anything with C# after 2007 or so. someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e)); Instead of, you know, a plain old `for` loop with an `if` in it and so on. Or maybe `foreach` if you want to be fancy. So, now we have nearly a decade of experience with this, can we finally settle this question: Is this style cancer? I still think it is, and the retort "you just have to get used to it" isn't going to work any more. I file this firmly under "stupid one-liner 'clever' code with no benefits to compensate". Yes, I've argued in the past that "clever code" isn't necessarily bad, and I'll keep saying that - there's a time and a place for it. But not if you're just trying to be cute. "Oh look at me, I put everything on one line, +1 nerd points for me" And this is even worse. It's not just cute with no benefits to compensate, it's cute and harder to read. Side question, why is this style popular?

        Y Offline
        Y Offline
        Yet Another XCoder
        wrote on last edited by
        #44

        When I started programming, "some" years ago, people where complaining about the performance of Object Oriented Programming (I won't speak of assembly vs. "high-level" language). A "few" years later, when .NET arrived, the same was said regarding the use of the Framework compared to native code. Nothing changes…

        L 1 Reply Last reply
        0
        • L Lost User

          You've probably seen this style if you're done anything with C# after 2007 or so. someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e)); Instead of, you know, a plain old `for` loop with an `if` in it and so on. Or maybe `foreach` if you want to be fancy. So, now we have nearly a decade of experience with this, can we finally settle this question: Is this style cancer? I still think it is, and the retort "you just have to get used to it" isn't going to work any more. I file this firmly under "stupid one-liner 'clever' code with no benefits to compensate". Yes, I've argued in the past that "clever code" isn't necessarily bad, and I'll keep saying that - there's a time and a place for it. But not if you're just trying to be cute. "Oh look at me, I put everything on one line, +1 nerd points for me" And this is even worse. It's not just cute with no benefits to compensate, it's cute and harder to read. Side question, why is this style popular?

          I Offline
          I Offline
          irneb
          wrote on last edited by
          #45

          The "idea" isn't bad per say ... just that it tends to be taken too far. Personally I try to keep such Linq chains down ... at most two dots in such a call (at least that being a quick-n-dirty rule-of-thumb). Especially as a normal for/foreach tends to be more efficient too, your sample is quite litterally performing 3 loops where one for loop would have sufficed. The only time I feel such long chain of Linq extension methods make sense is if using the Linq SQL syntax instead. Though it's still not very efficient, actually less so than the pseudo FP style.

          S 1 Reply Last reply
          0
          • L Lost User

            You've probably seen this style if you're done anything with C# after 2007 or so. someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e)); Instead of, you know, a plain old `for` loop with an `if` in it and so on. Or maybe `foreach` if you want to be fancy. So, now we have nearly a decade of experience with this, can we finally settle this question: Is this style cancer? I still think it is, and the retort "you just have to get used to it" isn't going to work any more. I file this firmly under "stupid one-liner 'clever' code with no benefits to compensate". Yes, I've argued in the past that "clever code" isn't necessarily bad, and I'll keep saying that - there's a time and a place for it. But not if you're just trying to be cute. "Oh look at me, I put everything on one line, +1 nerd points for me" And this is even worse. It's not just cute with no benefits to compensate, it's cute and harder to read. Side question, why is this style popular?

            H Offline
            H Offline
            HerrGilbert
            wrote on last edited by
            #46

            harold aptroot wrote:

            Is this style cancer?

            No. Linq has both it's Pro's and Con's. However, there is a clear benefit: Easy to write and easy to read (keep in mind that, if using Visual Studio, you have full intellisense support. And you can use your favourite code template, too.)

            someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e));

            looks better to me than

            List tmpStuffList = new List();
            for (int i = 0; i < someStuff.Count - 1; i++)
            {
            if (someStuff[i] != What)
            {
            tmpStuffList.Add(someStuff[i]);
            }
            }

            for (int n = 0; n < tmpStuffList.Count - 1; n++)
            {
            Hell(tmpStuffList[n]);
            }

            I'll stick to the LinQ way of Querying. ;)

            L 1 Reply Last reply
            0
            • L Lost User

              Sander Rossel wrote:

              I have no idea why you'd find it unreadable, it reads almost like natural language..

              I think the problem is it's the wrong language for me. I don't think in terms of filters and transformations but this style forces me to.

              Sander RosselS Offline
              Sander RosselS Offline
              Sander Rossel
              wrote on last edited by
              #47

              I do think that way, but your style forces me not to. From now on I'll consider for loops a cancer :)

              Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

              Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

              Regards, Sander

              1 Reply Last reply
              0
              • Sander RosselS Sander Rossel

                Nope, I love that style of programming. It's SO much more readable than a foreach/for/while loop. It becomes immediately clear what the code does. There's some collection than we need to filter, transform and process whereas a loop is just a loop and might do all those things, but you won't know until you read through the loop, probably with a lot more code to keep the new lists and counters. I've found a lot more unreadable loops than LINQ queries. I have no idea why you'd find it unreadable, it reads almost like natural language... :~ Anyway, that style is necessary for LINQ to SQL/Entities (because loops can't build expression trees, convert that to SQL and be lazy evaluated). And if I had to choose between LINQ or plain old SQL I'd choose LINQ wherever possible. Only the .ForEach() is an odd one. It's defined on List and not as a LINQ extension method because ForEach, by definition, has side-effects and LINQ was designed keeping the functional paradigm in mind. I never use it.

                Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                Regards, Sander

                G Offline
                G Offline
                Gaston Verelst
                wrote on last edited by
                #48

                Sander Rossel wrote:

                It becomes immediately clear what the code does.

                I think this is the important part. You focus on what the code does, without caring how this is done. As long as it does what it promises (which is the case with LINQ - usually) you're fine. So you're abstracting away how you would (for example) filter the collection.

                Check out my blog at http://msdev.pro/

                Sander RosselS 1 Reply Last reply
                0
                • G Gaston Verelst

                  Sander Rossel wrote:

                  It becomes immediately clear what the code does.

                  I think this is the important part. You focus on what the code does, without caring how this is done. As long as it does what it promises (which is the case with LINQ - usually) you're fine. So you're abstracting away how you would (for example) filter the collection.

                  Check out my blog at http://msdev.pro/

                  Sander RosselS Offline
                  Sander RosselS Offline
                  Sander Rossel
                  wrote on last edited by
                  #49

                  Yeah, and the how becomes so much more easier to read when you know what it is supposed to be doing in the first place :)

                  Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                  Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                  Regards, Sander

                  1 Reply Last reply
                  0
                  • L Lost User

                    You've probably seen this style if you're done anything with C# after 2007 or so. someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e)); Instead of, you know, a plain old `for` loop with an `if` in it and so on. Or maybe `foreach` if you want to be fancy. So, now we have nearly a decade of experience with this, can we finally settle this question: Is this style cancer? I still think it is, and the retort "you just have to get used to it" isn't going to work any more. I file this firmly under "stupid one-liner 'clever' code with no benefits to compensate". Yes, I've argued in the past that "clever code" isn't necessarily bad, and I'll keep saying that - there's a time and a place for it. But not if you're just trying to be cute. "Oh look at me, I put everything on one line, +1 nerd points for me" And this is even worse. It's not just cute with no benefits to compensate, it's cute and harder to read. Side question, why is this style popular?

                    A Offline
                    A Offline
                    Aunebakk
                    wrote on last edited by
                    #50

                    Sorting arrays in asp pages is ok I guess. For business logic I would recommend an traditional approach. so, yea, cancer for sure! //ra

                    1 Reply Last reply
                    0
                    • L Lost User

                      OriginalGriff wrote:

                      Would you like to write and debug "Except" each time you need it?

                      Actually yes, so I wouldn't end up in that situation of horribly nested function calls in your second code block.

                      S Offline
                      S Offline
                      Sentenryu
                      wrote on last edited by
                      #51

                      Instead having loads of repeated code everywhere for no real benefit? are you trolling?

                      L 1 Reply Last reply
                      0
                      • I irneb

                        The "idea" isn't bad per say ... just that it tends to be taken too far. Personally I try to keep such Linq chains down ... at most two dots in such a call (at least that being a quick-n-dirty rule-of-thumb). Especially as a normal for/foreach tends to be more efficient too, your sample is quite litterally performing 3 loops where one for loop would have sufficed. The only time I feel such long chain of Linq extension methods make sense is if using the Linq SQL syntax instead. Though it's still not very efficient, actually less so than the pseudo FP style.

                        S Offline
                        S Offline
                        Sentenryu
                        wrote on last edited by
                        #52

                        irneb wrote:

                        your sample is quite litterally performing 3 loops where one for loop would have sufficed.

                        you should really take a look on what the compiler does when it enconters the yield keyword, you might be surprised to find out it's not as inefficient as you think.

                        I 2 Replies Last reply
                        0
                        • Y Yet Another XCoder

                          When I started programming, "some" years ago, people where complaining about the performance of Object Oriented Programming (I won't speak of assembly vs. "high-level" language). A "few" years later, when .NET arrived, the same was said regarding the use of the Framework compared to native code. Nothing changes…

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

                          I didn't even mention performance, but sure, it aint great.

                          1 Reply Last reply
                          0
                          • H HerrGilbert

                            harold aptroot wrote:

                            Is this style cancer?

                            No. Linq has both it's Pro's and Con's. However, there is a clear benefit: Easy to write and easy to read (keep in mind that, if using Visual Studio, you have full intellisense support. And you can use your favourite code template, too.)

                            someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e));

                            looks better to me than

                            List tmpStuffList = new List();
                            for (int i = 0; i < someStuff.Count - 1; i++)
                            {
                            if (someStuff[i] != What)
                            {
                            tmpStuffList.Add(someStuff[i]);
                            }
                            }

                            for (int n = 0; n < tmpStuffList.Count - 1; n++)
                            {
                            Hell(tmpStuffList[n]);
                            }

                            I'll stick to the LinQ way of Querying. ;)

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

                            Well, you did use some unnecessary stuff there.. temporary list isn't necessary, and those -1's should obviously not be there.

                            1 Reply Last reply
                            0
                            • S Sentenryu

                              Instead having loads of repeated code everywhere for no real benefit? are you trolling?

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

                              It's literally a single "if" that's added into a loop. That's not enough to refactor out of it, anything it can be replaced by is at least as complicated. Writing an "except" method is certainly more complicated, and calling it isn't any simpler than what it replaces.

                              S M 2 Replies Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                I use it - though not the .ForEach on the end - because there are times when it provides a reliable, succinct, and clear way to process list (or other collection) based data.

                                var vidsWithOutPics = vidList.Except(vidsWithPics).Where(v => !v.IsAlternateTitle);

                                Or

                                var inDuration = DiskFile.GetAll().Where(df => !df.HasDuration).Select(df => df.Video).Distinct();

                                Or

                                var noSizeList = videoFiles.Where(file => file.Bytes < 0 && files.Contains(file.Location));

                                All I'm doing is "hiding" the loop so I don't have to write it! Yes, I could write each of those as loops - they aren't at all complex - but they would be longer; they would need debugging each time I wrote them. The other alternative would be to use Linq syntax, and that's pretty horrible!

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

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

                                Linq => Backward SQL

                                We're philosophical about power outages here. A.C. come, A.C. go.

                                R 1 Reply Last reply
                                0
                                • L Lost User

                                  It's literally a single "if" that's added into a loop. That's not enough to refactor out of it, anything it can be replaced by is at least as complicated. Writing an "except" method is certainly more complicated, and calling it isn't any simpler than what it replaces.

                                  S Offline
                                  S Offline
                                  Sentenryu
                                  wrote on last edited by
                                  #57

                                  this thread only shows that you're hellbent on your "One true way" of coding, so there's not much to discuss here. but do keep in mind that calling a style "cancer" just because you don't want to learn how to read and use it is exactly what causes so many flamewars on the IT world (tabs vs spaces anyone?). Sure, you tell me it's just an if but you know what? I much prefer to read

                                  someList.Where(condition).Select(fields).Distinct().OrderBy(field)

                                  than the alternative

                                  HashSet distinctSet = new HashSet();

                                  foreach(var item in someList){
                                  if(condition){
                                  distinctSet.Add(item);
                                  }
                                  }

                                  specially if you want to roll your own sorting method at the end. As a last note, i sometimes work with code where the order of operations (where, distinct, etc) sometimes yields different results and is important (due to crazy business rules, what can you do), so it's way easier to get the intent from the link way, but I recognize that you mileage may vary on that last one.

                                  L M 2 Replies Last reply
                                  0
                                  • S Sentenryu

                                    this thread only shows that you're hellbent on your "One true way" of coding, so there's not much to discuss here. but do keep in mind that calling a style "cancer" just because you don't want to learn how to read and use it is exactly what causes so many flamewars on the IT world (tabs vs spaces anyone?). Sure, you tell me it's just an if but you know what? I much prefer to read

                                    someList.Where(condition).Select(fields).Distinct().OrderBy(field)

                                    than the alternative

                                    HashSet distinctSet = new HashSet();

                                    foreach(var item in someList){
                                    if(condition){
                                    distinctSet.Add(item);
                                    }
                                    }

                                    specially if you want to roll your own sorting method at the end. As a last note, i sometimes work with code where the order of operations (where, distinct, etc) sometimes yields different results and is important (due to crazy business rules, what can you do), so it's way easier to get the intent from the link way, but I recognize that you mileage may vary on that last one.

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

                                    I promise you I'm not just flaming, I just really find those chained LINQy things hard to read. It's not like I haven't tried, I've been reading them for nearly a decade, I'm not getting used to them. It takes an extra step in my mind somehow, normal code I read and build up a picture of it in my mind, LINQy stuff I read, tear down, then build a picture. Clearly that is not the case for everyone here

                                    S J C 3 Replies Last reply
                                    0
                                    • L Lost User

                                      You've probably seen this style if you're done anything with C# after 2007 or so. someStuff.Where(c => c != What).Select(d => d + The).Foreach(e => Hell(e)); Instead of, you know, a plain old `for` loop with an `if` in it and so on. Or maybe `foreach` if you want to be fancy. So, now we have nearly a decade of experience with this, can we finally settle this question: Is this style cancer? I still think it is, and the retort "you just have to get used to it" isn't going to work any more. I file this firmly under "stupid one-liner 'clever' code with no benefits to compensate". Yes, I've argued in the past that "clever code" isn't necessarily bad, and I'll keep saying that - there's a time and a place for it. But not if you're just trying to be cute. "Oh look at me, I put everything on one line, +1 nerd points for me" And this is even worse. It's not just cute with no benefits to compensate, it's cute and harder to read. Side question, why is this style popular?

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

                                      boss: "we need an else statement adding for when 'c' == Who and to run "Heaven(e)" programmer: "right, that will be 2 hours."

                                      foreach(var stuff in someStuff) {
                                      if(stuff == What) {
                                      Hell(stuff + The)
                                      }
                                      else if(stuff == Who) {
                                      Heaven(stuff)
                                      }
                                      }

                                      Boss: We all so need to call Devil() for when stuff it is "What".

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        I promise you I'm not just flaming, I just really find those chained LINQy things hard to read. It's not like I haven't tried, I've been reading them for nearly a decade, I'm not getting used to them. It takes an extra step in my mind somehow, normal code I read and build up a picture of it in my mind, LINQy stuff I read, tear down, then build a picture. Clearly that is not the case for everyone here

                                        S Offline
                                        S Offline
                                        Sentenryu
                                        wrote on last edited by
                                        #60

                                        It was hard for me too, but once you understand how the context shifts it becomes way easier. LINQ queries have a different flow from the rest of the imperative code and are best left a little separated from stuff like for loops and complex conditionals, they require you to use the same way of thinking you use when writing SQL. It's not that you are telling the machine what to do, you're telling it what you want done, so you read this:

                                        someList.Where(condition).Select(field)

                                        As "Give me field for all objects where condition is true" instead of "For each object, if condition is true, stuff field on another list". Truth is, those kind of LINQ queries are just convenience. The real power of LINQ comes when you start using joins and groups, like this example from LINQPad (still on the simple side):

                                        var query =
                                        from o in Orders
                                        group o by o.ShipCountry into countryGroups
                                        select new
                                        {
                                        Country = countryGroups.Key,
                                        Cities =
                                        from cg in countryGroups
                                        group cg.ShipPostalCode by cg.ShipCity into cityGroups
                                        select new
                                        {
                                        City = cityGroups.Key,
                                        PostCodes = cityGroups.Distinct()
                                        }
                                        };

                                        that produces this output:

                                        // Brazil
                                        // Campinas
                                        // 04876-786
                                        // Resende
                                        // 08737-363
                                        // Rio de Janeiro
                                        // 02389-673
                                        // 02389-890
                                        // 05454-876

                                        As much as I can write that using for loops and conditionas, i don't want to write it that way. Also, join's and group's are the only thing I'll ever use query syntax for, they are somewhat easier to understand that way.

                                        1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          harold aptroot wrote:

                                          Is this style cancer?

                                          Yes. Many fans of that style don't realize how many times the data gets copied and iterated when they do nonsense like that. What really irks me is the near-constant use of ToList or ToArray; those are definitely cries for help. Even a simple foreach should generally be avoided in situations where a for will perform at least as well.

                                          R Offline
                                          R Offline
                                          Richard Deeming
                                          wrote on last edited by
                                          #61

                                          PIEBALDconsult wrote:

                                          how many times the data gets copied and iterated

                                          Iterated: yes. Copied: only if you use ToList / ToArray / ToDictionary / etc. That's the big benefit of lazy evaluation - nothing gets iterated or copied until you're ready to use it. Of course, if you don't understand what you're doing, that can also the big drawback. :)


                                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                          P 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