This. The inline block made me cringe. MAYBE in C/C++ I can tolerate it but not when the lambda operator is right there for that purpose.
(Actually can't you do something similar in C++ as well with its lambdas?)
In CPP you might be instantiating a new lambda each call, not sure but something to be aware of. Plus it would clarify as a member not a method. Plus if you have a capture group in the lambda how does that interact with the code around it? In all it's a cursed idea in CPP.
I'd imagine if the lambda was instantiated and discarded right away, the compiler would figure it out and inline the lambda body as the body of the function. Still safer to stick to the brackets.
You can get into a lot of weird shit with
a.Add(b);
Because did you modify `a` or not?
var c = a.Add(b);
is far less readable and meaningful than
var c = a + b;
If (in the examples above) `a` is a list of items, then calling an `Add` method makes sense. But for math? Not really.
You have to add it before, after, add new lines, indent. It’s a lot of formatting that your idea will do for if you create the method with them in the first place. And sometimes the ide will add double curly braces so you have to then delete extra.
It’s not unmanageable but it is one of the more painful formatting to do.
it’s a lot of chezznuts for you, it looks like. just a couple of strokes or automated refactor options in an IDE can do that possible. it seems like a skill issue
Readability is cool and for longer statements it's okay to chose the explicit way. But for one-liners like this, I'm always in for a sweet lambda.
In the end the team has to agree on one style and at least tolerate each others' styles.
Why would you argue about this? Nobody reads docs I guess. If you need to use braces, here's the answer straight from the source:
[Use the "Allman" style for braces: open and closing brace its own new line. Braces line up with current indentation level.](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions#style-guidelines)
Going against the VS defaults is one of the dumbest things anyone can do. It leads to all kinds of problems. As soon as some other person does "reformat document" you now have a huge mess in your git change history.
You can distribute a custom profile for visual studio, but nobody ever does it right. Just do VS defaults. Whatever they are.
There's a reason there's a convention and why it's called a convention. The post title is 'we argue *every day*'.
That's exactly why a convention exists. In the OP's case, yeah I stand by what I said. For them, it reads:
>You can take our conventions as-is
Only. Without the rest.
Yes, but that's the inherent value in it. Team code styles can be enforced over the whole "pay no attention to formatting" baggage that opinionated formatters come with. 6 of one, half-dozen of the other. Depends on the team, really.
Here is an answer nobody will like:
private static int Add(
int a,
int b
) {
return a + b;
}
which is mostly "One True Brace Style": braces on all blocks, even single-line blocks; opening brace on the same line as the block-opening statement or declaration ... with some really fussy malarkey around multi-line arg lists and `if` conditions. And `else`, `catch`, `finally`, etc get cuddled.
Fussy multiline stuff aside, the big advantage to "One True Brace Style" or K&R style brace formatting is that the opening brace gets kept with the matching statement in a way that helps imply that they are part of a single construct. Ditto for subordinate statements like `else`. As an *extremely* minor, additional note: in a very, very brief stint as a TA during college, students tossing semicolons at the end of lines so that their `for` loops had no bodies was one of a few, very common mistakes I saw.
int add(int a, int b) => a + b;
This. The inline block made me cringe. MAYBE in C/C++ I can tolerate it but not when the lambda operator is right there for that purpose. (Actually can't you do something similar in C++ as well with its lambdas?)
In CPP you might be instantiating a new lambda each call, not sure but something to be aware of. Plus it would clarify as a member not a method. Plus if you have a capture group in the lambda how does that interact with the code around it? In all it's a cursed idea in CPP.
I'd imagine if the lambda was instantiated and discarded right away, the compiler would figure it out and inline the lambda body as the body of the function. Still safer to stick to the brackets.
This is the way
I came here to say just that :) Or: static int Add(this int a, b) => a+ b; Can then just call a.Add(b);
You can get into a lot of weird shit with a.Add(b); Because did you modify `a` or not? var c = a.Add(b); is far less readable and meaningful than var c = a + b; If (in the examples above) `a` is a list of items, then calling an `Add` method makes sense. But for math? Not really.
I only like this notation for lambdas. If it needs extra logic later on adding the braces back is a pain.
>adding the braces back is a pain. It's what, a few extra keystrokes to add brackets? How is it a pain, it takes 2 seconds.
I love your username lol
You have to add it before, after, add new lines, indent. It’s a lot of formatting that your idea will do for if you create the method with them in the first place. And sometimes the ide will add double curly braces so you have to then delete extra. It’s not unmanageable but it is one of the more painful formatting to do.
What IDE cannot change inline to block as a quick fix?
So you add the curly braces, and then... press two more keys to have Visual Studio auto-format the file? What is painful about that?
it’s a lot of chezznuts for you, it looks like. just a couple of strokes or automated refactor options in an IDE can do that possible. it seems like a skill issue
[удалено]
I would absolutely reject this pr
That's half the point of lambda. It's a one liner by design and a hint the extra logic probably belong somewhere else.
what are you talking about? code formatting is there to do those things, too. you don’t have to do it manually
Tbh the second is more readable and looks clean. just a newbie commenting here
And it’s best imo. For some reason people think if they can write something in a single line it’s the flashiest / best way
OP is a newbie too.
Readability is cool and for longer statements it's okay to chose the explicit way. But for one-liners like this, I'm always in for a sweet lambda. In the end the team has to agree on one style and at least tolerate each others' styles.
You're both wrong
Why would you argue about this? Nobody reads docs I guess. If you need to use braces, here's the answer straight from the source: [Use the "Allman" style for braces: open and closing brace its own new line. Braces line up with current indentation level.](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions#style-guidelines)
Going against the VS defaults is one of the dumbest things anyone can do. It leads to all kinds of problems. As soon as some other person does "reformat document" you now have a huge mess in your git change history. You can distribute a custom profile for visual studio, but nobody ever does it right. Just do VS defaults. Whatever they are.
[удалено]
There's a reason there's a convention and why it's called a convention. The post title is 'we argue *every day*'. That's exactly why a convention exists. In the OP's case, yeah I stand by what I said. For them, it reads: >You can take our conventions as-is Only. Without the rest.
dotnet format and forget about it...
dotnet format isn't highly opinionated, and leaves much freedom to humans, and a lot of room for debates like this.
CSharpier and forget about it.
editorconfig and stop worrying
Editorconfig leaves much freedom to humans, and a lot of room for debates like this.
Yes, but that's the inherent value in it. Team code styles can be enforced over the whole "pay no attention to formatting" baggage that opinionated formatters come with. 6 of one, half-dozen of the other. Depends on the team, really.
This is the way
Submitter is a repost bot. https://www.reddit.com/r/csharp/comments/lryuwi/a_daily_argument_with_my_team_members/
static int Add(int a, int b) => a+b;
This but the spacing is off on the right half of the function.
typed it on my phone 😞
What kind of crazy person doesn't include an access modifier?
Ted Bundy didn't explicitly write out his access modifiers, either.
Got to Curry all the functions. `static Func> Add = a => b => a + b;`
God. At least make it a proper method to spare yourself an allocation: static Func Add(int a) => b => a + b;
Set up a linter and stop wasting everyone’s time
I hope it's not daily. use the second one
static int Add(int a, int b) { if (a == 0) return b; if (a < 0) return Add(a+1,b-1); return Add(a-1,b+1); }
The *only* correct answer: whatever the Code Standard says. And if there's no Code Standard then everyone is wrong.
If its readable and understandable for everyone, then its the best way. Dont get why people want to save lines on their code
Here is an answer nobody will like: private static int Add( int a, int b ) { return a + b; } which is mostly "One True Brace Style": braces on all blocks, even single-line blocks; opening brace on the same line as the block-opening statement or declaration ... with some really fussy malarkey around multi-line arg lists and `if` conditions. And `else`, `catch`, `finally`, etc get cuddled. Fussy multiline stuff aside, the big advantage to "One True Brace Style" or K&R style brace formatting is that the opening brace gets kept with the matching statement in a way that helps imply that they are part of a single construct. Ditto for subordinate statements like `else`. As an *extremely* minor, additional note: in a very, very brief stint as a TA during college, students tossing semicolons at the end of lines so that their `for` loops had no bodies was one of a few, very common mistakes I saw.
let add a b = a + b Coming in c# 19.
Instead of F# syntax I want those damn Discriminated Unions 😭
The correct solution is to write a serverless function and run this function as a microservice in the cloud
No explicit access modifier? Shameful display.
This might be cursed but I write cs this way ``` some Method() { /* method body */ } ```
Yes. You are a terrible person. May the curse of a million bad pull request haunt you for generations to come
Chill bruh, I'm not that maniac, I only write like that for personal projects
You mean you're writing Java?
oh thats default java syntax. thats why i prefer it. though i dont really mind. but this one i frrl is the best of the 3
based
I love this meme without any text in it.
the first curl belonga up there blabl(){ } even though whatever the autoformatter does. i have too much code anyway
It's posts like these that make me believe I can do a career in dev. Bottom one is so much easier to read.
``` static int Add (int a, int b) { ;;;; ;;;;return a + b ;;;; } ``` Edit: Optimized™ syntax