开发者

How to format complex mathematical expressions? [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 11 years ago.

Improve this question

This might not be the place for this, and if so I do apologize and ask that you point me in the direction where it may be appropriate for me to get an answer. This is a problem I run into when I code alot.

new Vector2(parent.Coordinates.X + (Position + parent.Prompt.Length - parent.visibilityIndex) * parent.font.Width, parent.Coordinates.Y);

That's quite a line! So I'll break it up into two:

new Vector2(parent.Coordinates.X + (Position + parent.Prompt.Length - p开发者_运维问答arent.visibilityIndex) * parent.font.Width, 
            parent.Coordinates.Y);

That's a little better, but still way too long. Anywhere else I try to break up the line seems arbitrary and to serve to obfuscate the code further. Am I wrong? What do you do? Again, I apologize if this is the wrong place for this as I am not sure.


This is subjective, but in this case I would split it out into separate variables.

var fontWidth = parent.font.Width;
var index = parent.visibilityIndex;
var offset = (Position + parent.Prompt.Length - index) * fontWidth;
return new Vector2(parent.Coordinates.X + offset, parent.Coordinates.Y);

Split it up in whichever way makes the most sense to you.


@Ed S. uses quite a few variables, you could accomplish the same with less.

double x = (Position + parent.Prompt.Length - parent.visibilityIndex);
x *= parent.font.Width;
x += parent.Coordinates.X;
new Vector2(x, parent.Coordinates.Y);


I typically just set a column guide at column 80 and break on the first operator I find before the guide:

new Vector2(parent.Coordinates.X + (Position + parent.Prompt.Length -          |
    parent.visibilityIndex) * parent.font.Width, parent.Coordinates.Y);        |

It's possibly not the most readable approach, and sometimes needs some tweaking (particularly when having to deal with ridiculouslyLongVariableOrMethodNames), but it does the job for me. YMMV.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜