What is the proper way to format code?

前端 未结 22 2202
没有蜡笔的小新
没有蜡笔的小新 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:03

    I've just been working on some code that goes in the opposite direction; each statement is separated from the next by a blank line. The authors also liked to use four lines of comment right aligned at column 60 instead of a one-line comment indented at code level. It is hard to read, and tedious to fix. Another feature of the code (C code), the break from a previous case is 'attached' to the case of the next, but there's a blank line after the case, separating it from its code. Ick!

    Blank lines around blocks of code are good. Not having too many blocks of code in a single function is good. Blank lines around every line of code is unpleasant. Too much, or too little, of a good thing is a bad thing.

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

    Basically the same thing as everyone else is saying. COBOL had very distinct rules about what a sentence and a paragraph was. I guess, in the back of my mind, I follow those. If you have a large IF statement, you didn't put a period until the very end of the nest. Similarly, I put a blank line after my last } // end if

    And yes, I put the // end if, // end for, // end method stuff in there. Some of the more vocal people I know don't like it, but I like it. They say you shouldn't make your if-statements huge and that you're probably coding wrong if you NEED the // end if stuff, and I don't NEED it, but I do find it makes it easier to read. Call me old fashioned, OCD, whatever.

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

    I've started following the Microsoft Design Guidelines found here:

    Design Guidelines for Class Library Developers

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

    One of the teachers I had, downgraded one of my assignments because I had spaced my code logically. He said, 'When you have to read code all day in the real world, you won't have this line spacing and you'll be thanking me."

    Unless you're seperating blocks with 5 or 10 lines of whitespace (which would drive anyone nuts), you're instructor is just being an ass.

    Coding standards are not etched in stone, and they are certainly not the same for all software shops. All companies have different coding standards. For what its worth, some of the coding standards at my company state explicitly "visually seperate logically related blocks of code using a single blank line".

    Although we should strive not to write 200-line long methods, its still very common for all of our short methods to contain more than one control flow element, and we should understand that vertical whitespace is just as important as horizontal whitespace in readability. You can satisfy the "single method, single purpose" principle even if you put a blank line between a for-loop and an if-statement in the same method.


    [Edit to add] And just a few more comments:

    1) Its very presumptuous that several people in this thread are assuming that the OP is writing 200-line methods, or that there is a necessary correlation between adding blank lines and writing sloppy methods.

    2) For what its worth, while the OP's instructor is utterly wrong in assuming that his coding standards are the the same everywhere. However, you should treat the programming course as its own little software shop with its own standards, so your code should be written in a way which follows those standards.

    If your instructor is grading you based on how well your code conforms to coding standards, then insist on getting a list of standards. I know if my grade was docked because it didn't conform to standards that the instructor had never given to me (or if his standards say "prefix variables with their datatype"), heads would be rolling.

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

    I know this is an old question, but I tripped over it doing a search for something else, and had an observation: look at the existing answers. They're generally short, yet they contain blank lines.

    That's what paragraphs are. As in writing, lines are a quick, visual way to separate concepts. It doesn't mean each paragraph above should be broken into a separate answer; it doesn't mean each code "paragraph" should be broken into a separate method.

    It's a silly complaint. If it were still possible to talk to that professor, I'd explain what "wall of text" means.

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

    I consider code as an article. Have you ever tried to read 2 pages of article which has no paragraph or line spacing in it?

    I agree with you, not having line spaces between logical groups is just insane.

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