Hey 🤩
Umm, I was coding and then lambda expressions comes up in my mind.
Which syntax is more clearer for you (in this situation)?
(with lambda expression):
private static MyClass MyMethod (object obj) => obj;
__________
(without lambda expression):
private static MyClass MyMethod(object obj)
{
return obj;
}
Normally I just use lambda expressions for delegates.
First one.
Though there are only a few situations where this would even come up (like overriding/implementing interface) because a property is usually more suited (assuming there are no parameters).
And then, i would still use lambda
Code public static MyClass MyProperty => [value];
Lotta blood to let, peace to make, give a fuk about a check
Dec 6, 2017 03:02 PM
(This post was last modified: Dec 6, 2017 03:03 PM by 0xDEAD10CC.)
It depends on the size, captures you might want, and re-use of the code that your lambda encapsulates... The context here is irrelevant because it looks dumbed down for the purpose of your explanation, so nobody here is going to be able to provide you with a good answer.
(Dec 6, 2017 03:02 PM)0xDEAD10CC Wrote: It depends on the size, captures you might want, and re-use of the code that your lambda encapsulates... The context here is irrelevant because it looks dumbed down for the purpose of your explanation, so nobody here is going to be able to provide you with a good answer.
I assume the context is a function with one instruction/line?
Lotta blood to let, peace to make, give a fuk about a check
DO NOT DEAL WITH THIS MEMBER
Dec 7, 2017 11:26 AM
(This post was last modified: Dec 7, 2017 11:26 AM by Ninja.)
(Dec 7, 2017 05:51 AM)Owl Wrote: First is of course 'cleaner' but if you ever have to revise it to >1 line you must revert to the original syntax. I tend to use this for short overrides (operators, ToString, etc.), delegates and LINQ.
Why is everyone saying that? I find the second one cleaner. Then again, I hardly ever use the first.
If you find yourself shortening code down to LINQ just so that you can use the first, then you're probably just not thinking about readability. Depending too, properties might be a better choice. The example above doesn't really show much.
I use the second one since it is cleaner and more understandable to use.
Dec 10, 2017 04:02 PM
(This post was last modified: Dec 10, 2017 04:04 PM by Crake.)
The former definitely is cleaner.
EDIT: Removed me being an idiot.
DO NOT DEAL WITH THIS MEMBER
(Dec 7, 2017 11:45 AM)0xDEAD10CC Wrote: If you find yourself shortening code down to LINQ just so that you can use the first, then you're probably just not thinking about readability. Depending too, properties might be a better choice. The example above doesn't really show much.
I agree, it's a broad example.
|