Does functional programming mandate new naming conventions?

后端 未结 10 1090
借酒劲吻你
借酒劲吻你 2021-01-31 08:51

I recently started studying functional programming using Haskell and came upon this article on the official Haskell wiki: How to read Haskell.

The article claims that sh

10条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 09:18

    I think if the semantics of the arguments are clear within the context of the code then you can get away with short variable names. I often use these in C# lambdas for the same reason. However if it is ambiguous, you should be more explicit with naming.

    map                     :: (a->b) -> [a] -> [b]
    map f  []               =  []
    map f (x:xs)            =  f x : map f xs
    

    To someone who hasn't had any exposure to Haskell, that might seem like ugly, unmaintainable code. But most Haskell programmers will understand this right away. So it gets the job done.

    var list = new int[] { 1, 2, 3, 4, 5 };
    int countEven = list.Count(n => n % 2 == 0)
    

    In that case, short variable name seems appropriate.

    list.Aggregate(0, (total, value) => total += value);
    

    But in this case it seems more appropriate to name the variables, because it isn't immediately apparent what the Aggregate is doing.

    Basically, I believe not to worry too much about convention unless it's absolutely necessary to keep people from screwing up. If you have any choice in the matter, use what makes sense in the context (language, team, block of code) you are working, and will be understandable by someone else reading it hours, weeks or years later. Anything else is just time-wasting OCD.

提交回复
热议问题