Live data from Hacker News

LINQ and Learning to Be Declarative

nickstambaugh.dev

1–10 of 60 posts

Re: LINQ and Learning to Be Declarative

#2
> Functional programming isn’t an afterthought in C#, it’s effective and you should learn it if you haven’t already.

I think C# is the best functional programming language because you always have access to a procedural code safety valve if the situation calls for it.

100% purity down the entire vertical is a very strong anti-pattern. You want to focus on putting the functional code where it is most likely to be wrong or cause trouble (your business logic). Worrying about making the underlying infrastructure functional is where I start to zone out.

Does F# care if a DLL it references was coded in a functional style?

Re: LINQ and Learning to Be Declarative

#3
If you're going to write code snippets, please make sure they compile!

The final example has signature List but tries to return IOrderedEnumerable.

I recognise that this is very much a taster rather than even a full introduction, so the author didn't want to explain IOrderedEnumerable, but please when writing blogs, run through your code examples and make sure they compile.

This means that your audience can follow along.

( It just needs a .ToList() on the end. )

Re: LINQ and Learning to Be Declarative

#4
post #3

If you're going to write code snippets, please make sure they compile! The final example has signature List but tries to return IOrderedEnumerable . I recognise that this is very much a taster rather than even a full introduction, so the author didn't want to explain IOrderedEnumerable , but please when writing blogs, run through your code examples and make sure they compile. This means that your audience can follow…

Thanks for the heads up, must've slipped my mind. Edit: fixed

Re: LINQ and Learning to Be Declarative

#6

Fwiw in the final iteration I would still place each clause on a seperate line aligned by their leading '.' because I find it much easier to scan.

Agreed. That's how I would write it in a professional codebase. I added it to show the transformation from separate functions -> LINQ one-liner. Cheers!

Re: LINQ and Learning to Be Declarative

#8
The second SQL-like version is far more readable. There is just less "syntax noise" and it's far more "declarative" by definition.

The author stating that the lambda is "better" because it's less lines is also silly. It's been a while since I've written C#, but pretty sure the SQL-like version can be formatted to a single line as well:

List GetExclusiveProducts(List source) => (from p in source where p.ProductTitle == "iPhone" orderby p.TypeOfPhone select p).ToList();

Re: LINQ and Learning to Be Declarative

#9
post #6

Fwiw in the final iteration I would still place each clause on a seperate line aligned by their leading '.' because I find it much easier to scan.

Agreed. That's how I would write it in a professional codebase. I added it to show the transformation from separate functions -> LINQ one-liner. Cheers!

Having things on one line is not an upside. Just think of all the Bash one-liners that are write once edit never. Doubly so in what's essentially a tutorial.

Re: LINQ and Learning to Be Declarative

#10
post #4
post #3

If you're going to write code snippets, please make sure they compile! The final example has signature List but tries to return IOrderedEnumerable . I recognise that this is very much a taster rather than even a full introduction, so the author didn't want to explain IOrderedEnumerable , but please when writing blogs, run through your code examples and make sure they compile. This means that your audience can follow…

Thanks for the heads up, must've slipped my mind. Edit: fixed

Isn’t that also true for the second to last example with the query syntax code?
Post reply on HN