<p>I am writing a coding standards document for a team of about 15 developers with a project load of between 10 and 15 projects a year. Amongst other sections (which I may post here as I get to them) I am writing a section on code formatting. So to start with, I think it is wise that, for whatever reason, we establish some basic, consistent code formatting/naming standards.</p>

<p>I've looked at roughly 10 projects written over the last 3 years from this team and I'm,  obviously, finding a pretty wide range of styles. Contractors come in and out and at times, and sometimes even double the team size.</p>

<p>I am looking for a few suggestions for code formatting and naming standards that have really paid off ... but that can also really be justified. I think consistency and shared-patterns go a long way to making the code more maintainable ... but, are there other things I ought to consider when defining said standards?</p>

<ul>
<li><p>How do you lineup parenthesis? Do you follow the same parenthesis guidelines when dealing with classes, methods, try catch blocks, switch statements, if else blocks, etc.</p></li>
<li><p>Do you line up fields on a column? Do you notate/prefix private variables with an underscore? Do you follow any naming conventions to make it easier to find particulars in a file? How do you order the members of your class?</p></li>
</ul>

<p>What about suggestions for namespaces, packaging or source code folder/organization standards? I tend to start with something like:</p>

<pre><code>&lt;com|org|...&gt;.&lt;company&gt;.&lt;app&gt;.&lt;layer&gt;.&lt;function&gt;.ClassName
</code></pre>

<p>I'm curious to see if there are other, more accepted, practices than what I am accustomed to -- before I venture off dictating these standards. Links to standards already published online would be great too -- even though I've done a bit of that already.</p>

[{'answer_id': 47664, 'author': 'stimms', 'author_id': 361, 'author_profile': 'https://Stackoverflow.com/users/361', 'pm_score': 1, 'selected': False, 'text': '<p>It obviously varies depending on languages and technologies.  By the look of your example name space I am going to guess java, in which case <a href="http://java.sun.com/docs/codeconv/" rel="nofollow noreferrer">http://java.sun.com/docs/codeconv/</a> is a really good place to start.  You might also want to look at something like maven\'s standard directory structure which will make all your projects look similar.  </p>\n'}, {'answer_id': 48107, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 3, 'selected': True, 'text': "<p>First find a automated code-formatter that works with your language.  Reason: Whatever the document says, people will inevitably break the rules.  It's much easier to run code through a formatter than to nit-pick in a code review.</p>\n\n<p>If you're using a language with an existing standard (e.g. Java, C#), it's easiest to use it, or at least start with it as a first draft.  Sun put a lot of thought into their formatting rules; you might as well take advantage of it.</p>\n\n<p>In any case, remember that much research has shown that varying things like brace position and whitespace use has no measurable effect on productivity or understandability or prevalence of bugs.  Just having <i>any</i> standard is the key.</p>\n"}, {'answer_id': 48119, 'author': 'Sean', 'author_id': 4919, 'author_profile': 'https://Stackoverflow.com/users/4919', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m going to second Jason\'s suggestion.</p>\n\n<p>I just completed a standards document for a team of 10-12 that work mostly in perl. The document says to use "perltidy-like indentation for complex data structures." We also provided everyone with example perltidy settings that would clean up their code to meet this standard. It was very clear and very much industry-standard for the language so we had great buyoff on it by the team.</p>\n\n<p>When setting out to write this document, I asked around for some examples of great code in our repository and googled a bit to find other standards documents that smarter architects than I to construct a template. It was tough being concise and pragmatic without crossing into micro-manager territory but very much worth it; having <em>any</em> standard is indeed key.</p>\n\n<p>Hope it works out!</p>\n'}, {'answer_id': 48121, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 2, 'selected': False, 'text': "<p>Coming from the automotive industry, here's a few style standards used for concrete reasons:</p>\n\n<p>Always used braces in control structures, and place them on separate lines.  This eliminates problems with people adding code and including it or not including it mistakenly inside a control structure.</p>\n\n<pre><code>if(...)\n{\n\n}\n</code></pre>\n\n<p>All switches/selects have a default case.  The default case logs an error if it's not a valid path.</p>\n\n<p>For the same reason as above, any if...elseif... control structures MUST end with a default else that also logs an error if it's not a valid path.  A single if statement does not require this.</p>\n\n<p>In the occasional case where a loop or control structure is intentionally empty, a semicolon is always placed within to indicate that this is intentional.</p>\n\n<pre><code>while(stillwaiting())\n{\n   ;\n}\n</code></pre>\n\n<p>Naming standards have very different styles for typedefs, defined constants, module global variables, etc.  Variable names include type. You can look at the name and have a good idea of what module it pertains to, its scope, and type.  This makes it easy to detect errors related to types, etc.</p>\n\n<p>There are others, but these are the top off my head.</p>\n\n<p>-Adam</p>\n"}]
['https://Stackoverflow.com/questions/47658', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4910/']