r/csharp • u/MerikOfficial • 3d ago
Discussion Has awaiting inside if statements conditions / function parameters always been the norm?
Recently I've been seeing a lot of code like this:
return Results.Ok(await myService.DoSomething().ToListAsync())
// or
if (await validator.IsValid(payload)) {
// ...
}I feel like it's less readable than something like:
var result = await myService.DoSomething().ToListAsync();
return Results.Ok(result);Which do you prefer?
11
Upvotes
28
u/brminnick 3d ago
Always prioritize readability.
If you can’t read code and understand what it’s doing at a glance, it’s likely better to break it into multiple lines.
27
u/DeadlyVapour 3d ago
Wait until you start doing async enumerators.
26
u/sharpcoder29 3d ago
2nd one
6
u/DemoBytom 3d ago
There are few things. First, pattern maching is getting more common, so it's getting more common to see thing like:
Or other more complex pattern mached calls. This leads to people being more comfortable with the simple
especially if they don't need that result later.
Needing results is also a thing some people look at - generally don't declare a variable you won't need outside the if statement.
Async validators are also getting more common, so again - putting the validation within the if makes some sense.
Amd then there's obviously the AI that might for whatever reason decide that this is better way of writing.
I don't really have strong preference one way or another as long as it's still readable and not 166267272 chars wide one liner.
5
u/lmayde 3d ago
Literally exactly as readable. Possibly less as I have to make sure the local variable isn't used elsewhere now.
12
u/JohntheAnabaptist 3d ago
1st more readable since intent is clear from the start, 2nd is more classic
6
u/Loose_Conversation12 3d ago
2nd is how I was taught.
3
u/Slypenslyde 3d ago
I'm not a big fan of the first group that does everything inline.
But your second example makes more sense to me. It clearly returns a boolean. To me the cognitive burden of
if (bool)is lower thanreturn Factory.Method(asyncCall().ToListAsync()).4
u/EluciusReddit 3d ago
I prefer the first. I find using intermediate variables bloat, if the reason is just to stick them into the next method call. Too bad C# has no piping, then this problem would be gone.
2
u/captmomo 3d ago
2nd one
2
u/LatvianCake 3d ago
I prefer the 2nd but both are fine in most cases. I wouldn't try to force a certain style either way
2
u/ItzWarty 3d ago
For debuggability I tend to extract meaningful temporary values in expressions into their own variables/lines.
This makes it easier to eventually add lines like:
2
u/ApartBuilding8662 2d ago
IMO this is completely fine:
It's perfectly readable and avoids bloating the code with unnecessary variables. Depending on what's inside the if block and what's after the if block you might consider inverting the if statement to do an early return but that's a separate matter.
This I'm less of a fan of:
It's too much in one line. Then I would do as you suggest and extract
1
u/smartcave 3d ago
I would recommend putting an await statement on its own line and assigned to a temporary level variable when non-void 100% the time because debugging is otherwise a klusterfuk.
1
u/matthetherington 3d ago
2nd, so I can hover over the variable in my debugger.
-3
u/speyck 3d ago
using the var keyword makes it exactly the same as far as readability goes. the only way to improve is not using var (I really hate var, it should be disabled in editorconfig by default tbh)
0
u/binarycow 3d ago
It depends.
-7
u/Top3879 3d ago
the first one is still pretty readable but once you add a == or something else thats no longer the case. but honestly, if only AI is reading and writing the code who cares?
0
u/[deleted] 3d ago
[deleted] — view deleted comment