-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Newline before else #43
Comments
I don't believe that a newline between the closing brace and the I also don't think that newline would help with grouping, for the same reason that the concept of |
I don't find that a newline here helps scanability - I'm looking at the shape of the code then and the blocks/indenting make the shape obvious, you don't need any further help finding the I agree with Josh about the diff stuff. I don't think this significantly affects rightward drift, usually when we care about rwd, we are talking about half a line, or most of a line in edge cases, this change would only save two characters. Furthermore, since we never have anything after the Finally, as well as adding an extra line, I think this makes the code noisier by making two short lines instead of one short line. Overall, I'm not in favour. |
I personally do prefer the newline. The first reason would be consistency between collapsed and expanded block versions:
versus
I also like that everything stays consistent even if I end up mixing expanded/oneline versions. Usually when every branch returns a simple value, and the else constructs an error. The second reason immediately coming to mind looking at my code is that I'd certainly have the newline when I add a comment:
A version to do that without a newline is to put it in the previous block:
but everytime I see this with longer comments it looks like there's a closing brace missing before the comment. If you use the no-newline version by default and add a newline for the comment, you end up with a mixed result like this:
which I find gives an odd grouping effect that is unintended. |
@phaylon The Rust style doesn't support "collapsed" conditionals like that either. If the entire conditional (if and else) doesn't fit on one line, the style guide recommends splitting out each block. For comments, I tend to either put them inside the block, or above the if, or (rarely) on the same line as the else and braces. |
@joshtriplett @phaylon I personally prefer "on the same line": if { // foo
} else { // bar
} |
I personally prefer else to be on the same line, but I think that there should be an option to customize this. |
FCP, propose to close |
Hi @est31, Why should such thing be customizable? |
@KalitaAlexey why not? Its popular in C where many people are coming from. |
@est31, pointers are popular in C, but they shouldn't use them right? |
@KalitaAlexey your comment better fits to this pr: #33 |
@est31, yes thanks. |
@est31 I've seen two popular styles in C: if (condition) {
body;
} else {
body;
} and if (condition)
{
body;
}
else
{
body;
} (I've also seen some software that indented the braces to match the body.) I've seen perhaps a couple of codebases, ever, that put a newline before the |
@KalitaAlexey otoh, I use the same style literally everywhere (as close as possible; i.e., in python I don't deal with braces, in C++ I don't have trailing commas). I can empathize with people who want to continue using their preferred style in Rust. |
@ubsan Just to clarify, you prefer the style without a newline before the else, correct? My reason for preferring style 1, below, is because it reduces the amount of vertical space used for "nothing". There is no reason or need to give the braces an entire line (or 4, in the completely expanded case). However, I do see that a style like style 2 could be preferred by some since it keeps all conditions aligned. I don't think this alignment is worth it in Rust, especially since the blocks in between can be large enough that the alignment is "worthless", i.e. it doesn't have any effect since both cannot be seen simultaneously or scanned between visually. Style 1:
Style 2:
|
@Mark-Simulacrum I don't put the whitespace, but I can empathize with people who do. I don't know if it's 'worthless' or w/e, I think it's fine. I probably did that at some point, knowing how many different styles I've used over the years, before finally settling on this one. I do very, very much dislike style 2, and I don't think I've ever seen it used; much more common is the |
Each choice has its benefits, of course. What bothers me the most is the wall of code in the first example I presented. I find that a newline between branches seems to help open up the text sometimes. For comparison, here's what the
instead of the more commonly used style:
|
I think this has been in FCP long enough with no new discussion. Closing. We may well allow an option to do this, but that should be discussed on an |
Newline before else / else if
Should there be a newline before
else
?Snippet from the list.rs example file
vs:
Some specifics from the Guiding principles
I found some guidelines that might be relevant:
Would a newline be desirable with regards to version control logs when adding / removing
else
blocks? If so, does that outweigh the benefit of minimising vertical space -- a point of lower priority according to the list of guiding principles?Does a newline here, in any way, influence readability or scan-ability? For example, does the vertical space help perception in grouping lines of code that belong together (i.e. the condition and the subsequent block contents)?
The text was updated successfully, but these errors were encountered: