How to replace for-loops with a functional statement in C#?

前端 未结 18 1725
不思量自难忘°
不思量自难忘° 2021-01-31 08:59

A colleague once said that God is killing a kitten every time I write a for-loop.

When asked how to avoid for-loops, his answer was to use a functional language. However

相关标签:
18条回答
  • 2021-01-31 09:12

    You can refactor your code well enough so that you won't see them often. A good function name is definitely more readable that a for loop.

    Taking the example from AndyC :

    Loop

    // mystrings is a string array
    List<string> myList = new List<string>();
    foreach(string s in mystrings)
    {
        if(s.Length > 5)
        {
            myList.add(s);
        }
    }
    

    Linq

    // mystrings is a string array
    List<string> myList = mystrings.Where<string>(t => t.Length > 5)
                                   .ToList<string();
    

    Wheter you use the first or the second version inside your function, It's easier to read

    var filteredList = myList.GetStringLongerThan(5);
    

    Now that's an overly simple example, but you get my point.

    0 讨论(0)
  • 2021-01-31 09:18

    Functional constructs often express your intent more clearly than for-loops in cases where you operate on some data set and want to transform, filter or aggregate the elements.

    Loops are very appropriate when you want to repeatedly execute some action.


    For example

    int x = array.Sum();
    

    much more clearly expresses your intent than

    int x = 0;
    for (int i = 0; i < array.Length; i++)
    {
        x += array[i];
    }
    
    0 讨论(0)
  • 2021-01-31 09:19

    For loop is, let's say, "bad" as it implies branch prediction in CPU, and possibly performance decrease when branch prediction miss.

    But CPU (having a branch prediction accuracy of 97%) and compiler with tecniques like loop unrolling, make loop performance reduction negligible.

    0 讨论(0)
  • 2021-01-31 09:23

    Your colleague is not right. For loops are not bad per se. They are clean, readable and not particularly error prone.

    0 讨论(0)
  • 2021-01-31 09:23

    A simple (and pointless really) example:

    Loop

    // mystrings is a string array
    List<string> myList = new List<string>();
    foreach(string s in mystrings)
    {
        if(s.Length > 5)
        {
            myList.add(s);
        }
    }
    

    Linq

    // mystrings is a string array
    List<string> myList = mystrings.Where<string>(t => t.Length > 5).ToList<string>();
    

    In my book, the second one looks a lot tidier and simpler, though there's nothing wrong with the first one.

    0 讨论(0)
  • 2021-01-31 09:23

    Sometimes a for-loop is bad if there exists a more efficient alternative. Such as searching, where it might be more efficient to sort a list and then use quicksort or binary sort. Or when you are iterating over items in a database. It is usually much more efficient to use set-based operations in a database instead of iterating over the items.

    Otherwise if the for-loop, especially a for-each makes the most sense and is readable, then I would go with that rather than rafactor it into something that isn't as intuitive. I personally don't believe in these religious sounding "always do it this way, because that is the only way". Rather it is better to have guidelines, and understand in what scenarios it is appropriate to apply those guidelines. It is good that you ask the Why's!

    0 讨论(0)
提交回复
热议问题