What is the proper way to format code?

前端 未结 22 2204
没有蜡笔的小新
没有蜡笔的小新 2021-02-18 18:33

When I write code, I try to group lines of similar code together, then leave a blank line and write another block.

I believe this contributes to the neatness and readabi

相关标签:
22条回答
  • 2021-02-18 19:09

    I use line spacing very similar to you. The code I've found in the real world often tend to be laid out similarly, or at least not so bunched together that it can't be read.

    0 讨论(0)
  • 2021-02-18 19:11

    In my opinion, adding a blank space to your code is a hint that you should split your function into smaller parts. Cleaning up your code by adding spaces is preferable to a mile long list of unseparated lines. Cleaning up your code by separating out smaller functions is better.

    0 讨论(0)
  • 2021-02-18 19:12

    Since we don't have an example of your personal idea of "logical" line spacing, we can't really say whether or not you deserved to be downgraded.

    I tend to group similar statements or steps in a sequence together before spacing (such as variable declarations, looping, etc.)

    0 讨论(0)
  • 2021-02-18 19:13

    My rule of thumb is:

    Put a single blank line between blocks of code that can be described with one comment.

    But generally I agree that many large functions with lots of white space should be broken into smaller functions.

    0 讨论(0)
  • 2021-02-18 19:13

    1) I agree with you about line spacing

    2) In my office there's a lot of lack of line spacing because "you get to see more of the code on one page that way." They also put multiple statements on one line, and over (IMHO) use ?:... I hate it.

    3) I do disagree with the phrase "That's why he is a teacher". as an (ex) teacher, I have to say that I would downgrade people for NOT putting space between sections before I took off points for putting space. I don't think I did either. My point is that him being an ass is orthogonal to his being a teacher. (EDIT: that section got edited from the original, but I'm leaving it here to maintain the rule of 3...)

    Don't malign teachers!

    0 讨论(0)
  • 2021-02-18 19:18

    Your teacher was probably half right in that in the real World you won't have the line spacing. Certainly on the Big Ball of Mud code bases I come across you're lucky if you get a line space let alone comment of explanation.

    As an aside the 73 year old programmer that wrote most of this Big Ball of Mud is still working there and his explanation is that the binaries need to be kept as small as possible, I didn't bother to check whether the compilers of 20 to 30 years ago were that inefficient they couldn't strip whitespace but I'm somewhat skeptical.

    I use single blank lines to break up logical sections in my own code as I find it greatly enhance readability.

    As always the best test with these type of readability concerns is to grab some tricky piece of code you wrote and haven't looked at for over a year and see if you can get a grasp on it quickly. If you can then your code will be better than most of what you'll see in the real World!

    0 讨论(0)
提交回复
热议问题