blob: 41dd86f06ea2d001d04bd361949baf39335de728 [file] [log] [blame]
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Style &mdash; Apache Cassandra Documentation v4.0-rc2</title>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/language_data.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/extra.css" type="text/css" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="License Compliance" href="license_compliance.html" />
<link rel="prev" title="Contributing Code Changes" href="patches.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Apache Cassandra
</a>
<div class="version">
4.0-rc2
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<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="../new/index.html">New Features in Apache Cassandra 4.0</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="../cql/index.html">The Cassandra Query Language (CQL)</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="../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">Contributing to Cassandra</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="gettingstarted.html">Getting Started</a></li>
<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="license_compliance.html">License Compliance</a></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>
<li class="toctree-l2"><a class="reference internal" href="documentation.html">Working on Documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="ci.html">Jenkins CI Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="dependencies.html">Dependency Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="release_process.html">Release Process</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="../plugins/index.html">Third-Party Plugins</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bugs.html">Reporting Bugs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contactus.html">Contact us</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Apache Cassandra</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> &raquo;</li>
<li><a href="index.html">Contributing to Cassandra</a> &raquo;</li>
<li>Code Style</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/development/code_style.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<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><p>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</p></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><p>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.</p></li>
<li><p>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).</p></li>
<li><p>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.</p></li>
<li><p>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.</p></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><p>Do not implement equals or hashcode methods unless they are actually needed.</p></li>
<li><p>Prefer public final fields to private fields with getters. (But prefer encapsulating behavior in “real” methods to either.)</p></li>
<li><p>Prefer requiring initialization in the constructor to setters.</p></li>
<li><p>Avoid redundant <code class="docutils literal notranslate"><span class="pre">this</span></code> references to member fields or methods.</p></li>
<li><p>Do not extract interfaces (or abstract classes) unless you actually need multiple implementations of it.</p></li>
<li><p>Always include braces for nested levels of conditionals and loops. Only avoid braces for single level.</p></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><p>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.</p></li>
<li><p>When splitting inside a method call, use one line per parameter and align them, like this:</p></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><p>When splitting a ternary, use one line per clause, carry the operator, and align like this:</p></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><p>Please make sure to use 4 spaces instead of the tab character for all your indentation.</p></li>
<li><p>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.</p></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><p>IntelliJ: <a class="reference external" href="https://wiki.apache.org/cassandra/CodeStyle?action=AttachFile&amp;do=view&amp;target=intellij-codestyle.jar">intellij-codestyle.jar</a></p></li>
<li><p>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)</p></li>
<li><p>Eclipse (<a class="reference external" href="https://github.com/tjake/cassandra-style-eclipse">https://github.com/tjake/cassandra-style-eclipse</a>)</p></li>
</ul>
</div></blockquote>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="license_compliance.html" class="btn btn-neutral float-right" title="License Compliance" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="patches.html" class="btn btn-neutral float-left" title="Contributing Code Changes" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2020, The Apache Cassandra team
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>