Do you have any good advice/links to a set of coding standards or best practices to follow?

前端 未结 20 2361
再見小時候
再見小時候 2021-02-01 10:40

For those of us that have programmed enough I’m sure we have come across many different flavours of coding standards that you can use when it comes to programming.

e.g.

相关标签:
20条回答
  • 2021-02-01 10:56

    Google has a posted style guide for C++ here which I consult sometimes. Just reading through the explanations and reasoning, despite whether you end up agreeing with some of the styles or not, may teach you some things you might not have thought about.

    0 讨论(0)
  • 2021-02-01 11:00

    Some comment to the post suggesting looking at the Google C++ guidelines. Detailed discussion about some aspects of these guidelines are posted at comp.lang.c++.moderated.

    Some weird or controversial points include:

    We don't believe that the available alternatives to exceptions, such as error codes and assertions, introduce a significant burden.

    As if assertions were a viable alternative... Assertions are usually for programming errors and situations that should never happen, while exceptions can happen (are somewhat anticipated) in the execution flow.

    Reference Arguments: All parameters passed by reference must be labeled const. ... In fact it is a very strong convention that input arguments are values or const references while output arguments are pointers.

    No comment, about weasel phrase a very strong convention.

    Doing Work in Constructors: Do only trivial initialization in a constructor. If at all possible, use an Init() method for non-trivial initialization.  ... If your object requires non-trivial initialization, consider having an explicit Init() method and/or adding a member flag that indicates whether the object was successfully initialized.

    Yes... 2-phase init to make things simpler... What if I have const fields? This rule is probably the effect of attitude towards exceptions.

    Use streams only for logging

    Which streams? IOStreams, standard C streams, other?

    On one hand they advise to use macros only in exceptional situations, while they recommend using DISALLOW_COPY_AND_ASSIGN to prohibit copy/assign. They could have advised the approach with special class (like in Boost)

    Do not overload operators except in rare, special circumstances.

    What about assignment, or arithmetic operators for numeric calculations, etc?

    Default parameters are more difficult to maintain because copy-and- paste from previous code may not reveal all the parameters. Copy-and- pasting of code segments can cause major problems when the default arguments are not appropriate for the new code.

    The what? Copy/paste from previous code?

    Remember that reading any of the guidelines can introduce a bias to your way of thinking. And sometimes it won't be beneficial for you or your code. I agree with some other posts advising reading good books by good authors beforehand. When you have sufficient amount of knowledge, then you are able to look at the guidelines and find good and weak points easily, without creating a mess in your brain ;)

    0 讨论(0)
  • 2021-02-01 11:02

    Having asked this question. I found that the accepted answer proved to be sufficient for my needs.

    However, I realise that this is not a 'one-size-fits-all' scenario, so there is a large quantity of information within the thread that you may find more or less useful. Weel worth a read!

    0 讨论(0)
  • 2021-02-01 11:03

    i think Code Craft - The Practice of Writing Excellent Code pretty much sums it all up

    0 讨论(0)
  • 2021-02-01 11:05

    If you are maintaining code that continue to use the same standard as the original code was developed in (there is nothing worse then trying to debug a problem when the code looks all higgildy piggeldy)

    0 讨论(0)
  • 2021-02-01 11:06

    Coding standards are good, but coding standards written from scratch in which the company reinvents the wheel, or coding standards imposed by a single "prophet", can be worse than having no coding standards at all.

    This means:

    • Coding standards should be discussed and agreed upon.
    • The coding standards document should include the reasons behind each rule.
    • Coding standards should be at least partially based on reliable sources.

    The sources I know of for the languages in your tags are:

    • For C++: The book C++ Coding Standards by Sutter/Alexandrescu.
    • For C#: 4 or 5 PDF's I found googling for C# Coding Standards :)
    0 讨论(0)
提交回复
热议问题