| --- |
| layout: docpage |
| |
| title: "Documentation" |
| |
| is_homepage: false |
| is_sphinx_doc: true |
| |
| doc-parent: "Cassandra Development" |
| |
| doc-title: "Code Style" |
| doc-header-links: ' |
| <link rel="top" title="Apache Cassandra Documentation v3.11.8" href="../index.html"/> |
| <link rel="up" title="Cassandra Development" href="index.html"/> |
| <link rel="next" title="Review Checklist" href="how_to_review.html"/> |
| <link rel="prev" title="Contributing Code Changes" href="patches.html"/> |
| ' |
| doc-search-path: "../search.html" |
| |
| extra-footer: ' |
| <script type="text/javascript"> |
| var DOCUMENTATION_OPTIONS = { |
| URL_ROOT: "", |
| VERSION: "", |
| COLLAPSE_INDEX: false, |
| FILE_SUFFIX: ".html", |
| HAS_SOURCE: false, |
| SOURCELINK_SUFFIX: ".txt" |
| }; |
| </script> |
| ' |
| |
| --- |
| <div class="container-fluid"> |
| <div class="row"> |
| <div class="col-md-3"> |
| <div class="doc-navigation"> |
| <div class="doc-menu" role="navigation"> |
| <div class="navbar-header"> |
| <button type="button" class="pull-left navbar-toggle" data-toggle="collapse" data-target=".sidebar-navbar-collapse"> |
| <span class="sr-only">Toggle navigation</span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| </div> |
| <div class="navbar-collapse collapse sidebar-navbar-collapse"> |
| <form id="doc-search-form" class="navbar-form" action="../search.html" method="get" role="search"> |
| <div class="form-group"> |
| <input type="text" size="30" class="form-control input-sm" name="q" placeholder="Search docs"> |
| <input type="hidden" name="check_keywords" value="yes" /> |
| <input type="hidden" name="area" value="default" /> |
| </div> |
| </form> |
| |
| |
| |
| <ul class="current"> |
| <li class="toctree-l1"><a class="reference internal" href="../getting_started/index.html">Getting Started</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../architecture/index.html">Architecture</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../data_modeling/index.html">Data Modeling</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../cql/index.html">The Cassandra Query Language (CQL)</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../configuration/index.html">Configuring Cassandra</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../operating/index.html">Operating Cassandra</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Cassandra Tools</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../troubleshooting/index.html">Troubleshooting</a></li> |
| <li class="toctree-l1 current"><a class="reference internal" href="index.html">Cassandra Development</a><ul class="current"> |
| <li class="toctree-l2"><a class="reference internal" href="ide.html">Building and IDE Integration</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="testing.html">Testing</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="patches.html">Contributing Code Changes</a></li> |
| <li class="toctree-l2 current"><a class="current reference internal" href="#">Code Style</a><ul> |
| <li class="toctree-l3"><a class="reference internal" href="#general-code-conventions">General Code Conventions</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#exception-handling">Exception handling</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#boilerplate">Boilerplate</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#multiline-statements">Multiline statements</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#whitespace">Whitespace</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#imports">Imports</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#format-files-for-ides">Format files for IDEs</a></li> |
| </ul> |
| </li> |
| <li class="toctree-l2"><a class="reference internal" href="how_to_review.html">Review Checklist</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="how_to_commit.html">How-to Commit</a></li> |
| </ul> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../faq/index.html">Frequently Asked Questions</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../bugs.html">Reporting Bugs and Contributing</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../contactus.html">Contact us</a></li> |
| </ul> |
| |
| |
| |
| </div><!--/.nav-collapse --> |
| </div> |
| </div> |
| </div> |
| <div class="col-md-8"> |
| <div class="content doc-content"> |
| <div class="content-container"> |
| |
| <div class="section" id="code-style"> |
| <h1>Code Style<a class="headerlink" href="#code-style" title="Permalink to this headline">¶</a></h1> |
| <div class="section" id="general-code-conventions"> |
| <h2>General Code Conventions<a class="headerlink" href="#general-code-conventions" title="Permalink to this headline">¶</a></h2> |
| <blockquote> |
| <div><ul class="simple"> |
| <li>The Cassandra project follows <a class="reference external" href="http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html">Sun’s Java coding conventions</a> with an important exception: <code class="docutils literal notranslate"><span class="pre">{</span></code> and <code class="docutils literal notranslate"><span class="pre">}</span></code> are always placed on a new line</li> |
| </ul> |
| </div></blockquote> |
| </div> |
| <div class="section" id="exception-handling"> |
| <h2>Exception handling<a class="headerlink" href="#exception-handling" title="Permalink to this headline">¶</a></h2> |
| <blockquote> |
| <div><ul class="simple"> |
| <li>Never ever write <code class="docutils literal notranslate"><span class="pre">catch</span> <span class="pre">(...)</span> <span class="pre">{}</span></code> or <code class="docutils literal notranslate"><span class="pre">catch</span> <span class="pre">(...)</span> <span class="pre">{</span> <span class="pre">logger.error()</span> <span class="pre">}</span></code> merely to satisfy Java’s compile-time exception checking. Always propagate the exception up or throw <code class="docutils literal notranslate"><span class="pre">RuntimeException</span></code> (or, if it “can’t happen,” <code class="docutils literal notranslate"><span class="pre">AssertionError</span></code>). This makes the exceptions visible to automated tests.</li> |
| <li>Avoid propagating up checked exceptions that no caller handles. Rethrow as <code class="docutils literal notranslate"><span class="pre">RuntimeException</span></code> (or <code class="docutils literal notranslate"><span class="pre">IOError</span></code>, if that is more applicable).</li> |
| <li>Similarly, logger.warn() is often a cop-out: is this an error or not? If it is don’t hide it behind a warn; if it isn’t, no need for the warning.</li> |
| <li>If you genuinely know an exception indicates an expected condition, it’s okay to ignore it BUT this must be explicitly explained in a comment.</li> |
| </ul> |
| </div></blockquote> |
| </div> |
| <div class="section" id="boilerplate"> |
| <h2>Boilerplate<a class="headerlink" href="#boilerplate" title="Permalink to this headline">¶</a></h2> |
| <blockquote> |
| <div><ul class="simple"> |
| <li>Avoid redundant <code class="docutils literal notranslate"><span class="pre">@Override</span></code> annotations when implementing abstract or interface methods.</li> |
| <li>Do not implement equals or hashcode methods unless they are actually needed.</li> |
| <li>Prefer public final fields to private fields with getters. (But prefer encapsulating behavior in “real” methods to either.)</li> |
| <li>Prefer requiring initialization in the constructor to setters.</li> |
| <li>Avoid redundant <code class="docutils literal notranslate"><span class="pre">this</span></code> references to member fields or methods.</li> |
| <li>Do not extract interfaces (or abstract classes) unless you actually need multiple implementations of it.</li> |
| <li>Always include braces for nested levels of conditionals and loops. Only avoid braces for single level.</li> |
| </ul> |
| </div></blockquote> |
| </div> |
| <div class="section" id="multiline-statements"> |
| <h2>Multiline statements<a class="headerlink" href="#multiline-statements" title="Permalink to this headline">¶</a></h2> |
| <blockquote> |
| <div><ul class="simple"> |
| <li>Try to keep lines under 120 characters, but use good judgement – it’s better to exceed 120 by a little, than split a line that has no natural splitting points.</li> |
| <li>When splitting inside a method call, use one line per parameter and align them, like this:</li> |
| </ul> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>SSTableWriter writer = new SSTableWriter(cfs.getTempSSTablePath(), |
| columnFamilies.size(), |
| StorageService.getPartitioner()); |
| </pre></div> |
| </div> |
| <ul class="simple"> |
| <li>When splitting a ternary, use one line per clause, carry the operator, and align like this:</li> |
| </ul> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>var = bar == null |
| ? doFoo() |
| : doBar(); |
| </pre></div> |
| </div> |
| </div></blockquote> |
| </div> |
| <div class="section" id="whitespace"> |
| <h2>Whitespace<a class="headerlink" href="#whitespace" title="Permalink to this headline">¶</a></h2> |
| <blockquote> |
| <div><ul class="simple"> |
| <li>Please make sure to use 4 spaces instead of the tab character for all your indentation.</li> |
| <li>Many lines in many files have a bunch of trailing whitespace… Please either clean these up in a separate patch, or leave them alone, so that reviewers now and anyone reading code history later doesn’t have to pay attention to whitespace diffs.</li> |
| </ul> |
| </div></blockquote> |
| </div> |
| <div class="section" id="imports"> |
| <h2>Imports<a class="headerlink" href="#imports" title="Permalink to this headline">¶</a></h2> |
| <p>Please observe the following order for your imports:</p> |
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>java |
| [blank line] |
| com.google.common |
| org.apache.commons |
| org.junit |
| org.slf4j |
| [blank line] |
| everything else alphabetically |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="format-files-for-ides"> |
| <h2>Format files for IDEs<a class="headerlink" href="#format-files-for-ides" title="Permalink to this headline">¶</a></h2> |
| <blockquote> |
| <div><ul class="simple"> |
| <li>IntelliJ: <a class="reference external" href="https://wiki.apache.org/cassandra/CodeStyle?action=AttachFile&do=view&target=intellij-codestyle.jar">intellij-codestyle.jar</a></li> |
| <li>IntelliJ 13: <a class="reference external" href="https://gist.github.com/jdsumsion/9ab750a05c2a567c6afc">gist for IntelliJ 13</a> (this is a work in progress, still working on javadoc, ternary style, line continuations, etc)</li> |
| <li>Eclipse (<a class="reference external" href="https://github.com/tjake/cassandra-style-eclipse">https://github.com/tjake/cassandra-style-eclipse</a>)</li> |
| </ul> |
| </div></blockquote> |
| </div> |
| </div> |
| |
| |
| |
| |
| <div class="doc-prev-next-links" role="navigation" aria-label="footer navigation"> |
| |
| <a href="how_to_review.html" class="btn btn-default pull-right " role="button" title="Review Checklist" accesskey="n">Next <span class="glyphicon glyphicon-circle-arrow-right" aria-hidden="true"></span></a> |
| |
| |
| <a href="patches.html" class="btn btn-default" role="button" title="Contributing Code Changes" accesskey="p"><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span> Previous</a> |
| |
| </div> |
| |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |