When a long expression is written in multi-line, should the character be placed at the end of the line or in the first line of the next line?

When the expression is too long and cannot fit into the display area or for the accuracy of the source code, we will write a long expression in multi-line. I tried to check the related programming rules, but I can not find some related to the character , which should be placed at the end of the line or first of the next line.

For example, an expression of a long IF in C # code,

if (!double.TryParse(text4Hours.Text, out outputValue)||
        !double.TryParse(text4Hours.Text, out outputValue)||
        !double.TryParse(text10Hours.Text, out outputValue)){ }

OR

if (!double.TryParse(text4Hours.Text, out outputValue)
        ||!double.TryParse(text4Hours.Text, out outputValue)
        ||!double.TryParse(text10Hours.Text, out outputValue)){ }

Parallel symbol || can be put at the end of the line or in the first line of the next line, any rules declare that we should follow the first or second? What are the benefits for this?

Another example is

        answer = amount4Hours * double.Parse(text4Hours.Text) +
                amount8Hours * double.Parse(text8Hours.Text) - 
                    break45 * double.Parse(text8Hours.Text) +
                amount10Hours * double.Parse(text10Hours.Text) -
                    break45 * double.Parse(text10Hours.Text);

OR

            answer = amount4Hours * double.Parse(text4Hours.Text) 
                    +amount8Hours * double.Parse(text8Hours.Text)  
                        -break45 * double.Parse(text8Hours.Text) 
                    +amount10Hours * double.Parse(text10Hours.Text) 
                        -break45 * double.Parse(text10Hours.Text);

I know that we should try to avoid a long expression, but if you consider that the display area of ​​a text editor is small or other problems, then this question will pop up anyway. Thank.

+3
source share
2 answers

I believe that any answers to this question will be based on opinions, but I also believe that most people would prefer the first approach. Imagine something like:

if (expression1 &&
        expression243787 &&
        expression3 &&
        expression938533485934 &&
        expression7247)

Now imagine that you catch an error in your code and understand that there is a logical error in this expression, where all && operators must be || instead. It might just be an opinion, but I think it would be much easier to fix this if the code were like this:

if (expression1
        && expression243787
        && expression3
        && expression938533485934
        && expression7247)

, , , , , .

+2

. .

0

All Articles