For manually wrapping long lines, what is your personal heuristic for choosing places to break a line?
Assuming this line is too long, where might you break it and it what order of precedence?
double var = GetContext()->CalculateValue(element, 10.0);
Most people agree about separating parameters per line:
double var = GetContext()->CalculateValue(element,
10.0);
Does anyone break at an opening paren?
double var = GetContext()->CalculateValue(
element, 10.0);
But how bout with a dereferencing operator (or .
):
double var = GetContext()
->CalculateValue(element, 10.0);
or would you:
double var = GetContext()->
CalculateValue(element, 10.0);
Any different for the assignment operator?
double var =
GetContext()->CalculateValue(element, 10.0);
or
double var
= GetContext()->CalculateValue(element, 10.0);
Any others?
If your system is procedural, you could answer like this:
->
or .
operatorOr just post some example code!
Bonus points if you can academically justify your breaking decision.
This is useful for code readability. To break an expression into multiple lines, wrap the expression around a set of parenthesis and break it down as you want. If the expression is already in a set of parenthesis, square brackets, or curly braces, you can split it to multiple lines.
If you have a very long line of code in Python and you'd like to break it up over over multiple lines, if you're inside parentheses, square brackets, or curly braces you can put line breaks wherever you'd like because Python allows for implicit line continuation.
In addition to negatively affecting readability, having very long lines can destroy the positive impact of code indentation, which makes the code even harder to understand and maintain (because of chaotic line returns).
The \n Character The other way to break a line in C++ is to use the newline character — that ' \n ' mentioned earlier.
I like to make the splits in strength of binding order, closest to the end of the line first. So in your examples I would split at the = sign. If this still spilled over the margin I would split at the ->
The idea of splitting a line is solely for the benefit of readers (since the compile could care less). I find that mentally it is easier to mentally chunk pieces of code that are broken into logical groups.
In your particular example I'd personally go with the fourth one:
double var = GetContext()->
CalculateValue(element, 10.0);
I've never been a fan of strictly defined code formatting standards, aside from the basic rule to always format one's code so that it makes sense (particularly to somebody who didn't write it) and is easy to read. Any overly-broad rule like "always put a line break at this or that spot in a line" or even "always name variables with this or that naming scheme" just doesn't sit right with me. I find that it does more harm than good in the long run.
It doesn't account for edge cases very well, it places the focus on the wrong thing (the points of syntax rather than the actual readable meaning and flow of the code) and it furthers the goal of commoditizing the developer to make the position as plug-and-play as possible without account for the developer's own best judgement.
(Ok, that may have turned into a bit of a rant. My apologies. But the point remains.)
Basically, without any concrete rules, the code should be made to best suit what it's trying to say in a readable manner on a case-by-case basis. Developers should use their best judgement accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With