What is the proper way to format code?

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

    Unless you go nuts with the vertical spacing, I see no problem with separating out your code. I know people like to toss around the idea of using little methods, but something even if your method does one single thing, that single thing could take a lot of code to accomplish. Not everything can be done in a screenful of code.

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

    Use little methods, in order to have a low cyclomatic complexity. High cyclomatic complexity in methods generally means that your method must be divided into several others sub methods, which will be more easier to read, and more easier to test!

    I think that your code is facing this kind of problem.

    Generally, a method with 100+ lines is too big and too complex, and then must be refactored.

    To summarize, instead of breaking code with line spaces, break it into separate methods...

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

    Spaces help to make your code more readable and I think the consensus here is that they are not for the most part a bad idea. But nobody seems to have pointed out that along with the blank line a comment may not be amiss.

    /* This section preps the widgets for display */
    block 
    of some 
    code
    
    /* This section passes the widgets to the display handler */
    and 
    so 
    on
    

    Remember most code will be read many times over it’s life so anything you can do to make life easier for future maintainers is a great plus.

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

    I'm not going to begin to suggest any specifics. There are plenty of good suggestions here and elsewhere on the interweb.

    But I would say be consistent. At least within an application or module - since I know I refine my approach from time to time, but I try to keep all code in the same places looking the same.

    If you are consistent, it's easy enough for any other developer to pick your tempo and style.

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

    Vertical space is usually at a premium relative to horizontal space, so excessive spacing usually isn't a good idea. I do think it's a good idea to logically separate blocks of code with a single blank line.

    Sounds like your teacher is mostly a jerk. As the saying goes, "those who can, do; those who can't are on StackOverflow teach." ;)

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

    I follow Microsoft's Guidelines for C#.

    Edit: The standard for C# is Don't fight the IDE. If you hit CTRL K+D, the IDE will automatically put blank lines in between code sections.

    To follow that up, if you look at C# sample code on MSDN or anywhere else, there's normally a blank line in between each logically placed group. So There will be a blank line after all your member variables, a blank line after each method, etc.

    In response to the comments expressing shock and horror that I use an IDE for C# programming:


    REAL PROGRAMMERS

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