csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Appearance of formatted code which use chaining of expressions including ?.

Open fgimian opened this issue 2 years ago • 4 comments

Hey there, I just wanted to throw out an idea for consideration. Please forgive me if this has already been discussed in the past and agreed on.

Here's an example piece of code formatted with CSharpier:

-                bool? response = OrielState.Writer?.Prompt(
-                    $"Add a trusted publisher using the certificate at {path}?"
-                );
+                bool? response = OrielState
+                    .Writer
+                    ?.Prompt($"Add a trusted publisher using the certificate at {path}?");

I wonder if the following would be better:

...
            if (OrielState.Mode == OrielMode.Prompt)
            {
                bool? response = OrielState
                    .Writer?
                    .Prompt($"Add a trusted publisher using the certificate at {path}?");
                proceed = response is true;
            }
...

I think this looks a bit cleaner as all periods / dots are aligned together.

What do you think? Fotis

fgimian avatar Nov 29 '23 07:11 fgimian

Just my personal opinion: I do think your proposed behavior looks cleaner, but to me the current behavior is more readable at a glance. That is, I notice that Writer may be null much more quickly with the ? at the beginning of a line rather than at the end.

ShawnTheBeachy avatar Dec 27 '23 16:12 ShawnTheBeachy

Just my personal opinion: I do think your proposed behavior looks cleaner, but to me the current behavior is more readable at a glance. That is, I notice that Writer may be null much more quickly with the ? at the beginning of a line rather than at the end.

I completely see your point, it is something I thought about too. I do believe tools such as rust fmt in Rust do place the ? (used in Rust for automatically returning possible Err values from functions) similar to my proposal, but I do see how both approaches have merit.

Cheers Fotis

fgimian avatar Dec 28 '23 08:12 fgimian

If I recall correctly, putting ? at the beginning of the line came from how prettier formats javascript. But it appears that javascript requires it there which may be how they ended up using that formatting.

This is invalid js

OrielState()
    .Writer()?
    .Prompt("Add a trusted publisher using the certificate at {path}?")
    .CallMethod()
    .DoThing();

And this is invalid ts, which leads to these two being inconsistent.

OrielState()
    .Writer()
    !.Prompt("Add a trusted publisher using the certificate at {path}?")
    .CallMethod()
    .DoThing();

It may be worth getting the two consistent in CSharpier.

I don't know if I have a strong preference. I do like the look of all the . lining up, but also agree that with ? at the beginning of a line it is easy to know the previous call may have been null. But is it imporatant to know that the call may have been null?

belav avatar Dec 28 '23 20:12 belav

You're probably right that the readability might not really matter.

ShawnTheBeachy avatar Dec 28 '23 20:12 ShawnTheBeachy