| <!DOCTYPE html> |
| <html> |
| |
| |
| |
| |
| <head> |
| <meta charset="utf-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <meta name="description" content="The Apache Cassandra database is the right choice when you need scalability and high availability without compromising performance. Linear scalability and proven fault-tolerance on commodity hardware or cloud infrastructure make it the perfect platform for mission-critical data. Cassandra's support for replicating across multiple datacenters is best-in-class, providing lower latency for your users and the peace of mind of knowing that you can survive regional outages. |
| "> |
| <meta name="keywords" content="cassandra, apache, apache cassandra, distributed storage, key value store, scalability, bigtable, dynamo" /> |
| <meta name="robots" content="index,follow" /> |
| <meta name="language" content="en" /> |
| |
| <title>Documentation</title> |
| |
| <link rel="canonical" href="http://cassandra.apache.org/doc/4.0/development/code_style.html"> |
| |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> |
| <link rel="stylesheet" href="./../../../css/style.css"> |
| |
| <link rel="stylesheet" href="./../../../css/sphinx.css"> |
| |
| |
| <link rel="top" title="Apache Cassandra Documentation v4.0" href="../index.html"/> <link rel="up" title="Contributing to Cassandra" href="index.html"/> <link rel="next" title="Review Checklist" href="how_to_review.html"/> <link rel="prev" title="Contributing Code Changes" href="patches.html"/> |
| <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"> |
| |
| <link type="application/atom+xml" rel="alternate" href="http://cassandra.apache.org/feed.xml" title="Apache Cassandra Website" /> |
| </head> |
| |
| <body> |
| <!-- breadcrumbs --> |
| <div class="topnav"> |
| <div class="container breadcrumb-container"> |
| <ul class="breadcrumb"> |
| <li> |
| <div class="dropdown"> |
| <img class="asf-logo" src="./../../../img/asf_feather.png" /> |
| <a data-toggle="dropdown" href="#">Apache Software Foundation <span class="caret"></span></a> |
| <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> |
| <li><a href="http://www.apache.org">Apache Homepage</a></li> |
| <li><a href="http://www.apache.org/licenses/">License</a></li> |
| <li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| <li><a href="http://www.apache.org/security/">Security</a></li> |
| </ul> |
| </div> |
| </li> |
| |
| |
| <li><a href="./../../../">Apache Cassandra</a></li> |
| |
| |
| |
| |
| <li><a href="./../../../doc">Documentation</a></li> |
| |
| |
| |
| |
| <li><a href="./">Contributing to Cassandra</a></li> |
| |
| |
| |
| <li>Code Style</li> |
| |
| </ul> |
| </div> |
| |
| <!-- navbar --> |
| <nav class="navbar navbar-default navbar-static-top" role="navigation"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#cassandra-menu" aria-expanded="false"> |
| <span class="sr-only">Toggle navigation</span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| <a class="navbar-brand" href="./../../../"><img src="./../../../img/cassandra_logo.png" alt="Apache Cassandra logo" /></a> |
| </div><!-- /.navbar-header --> |
| |
| <div id="cassandra-menu" class="collapse navbar-collapse"> |
| <ul class="nav navbar-nav navbar-right"> |
| <li><a href="./../../../">Home</a></li> |
| <li><a href="./../../../download/">Download</a></li> |
| <li><a href="./../../../doc/">Documentation</a></li> |
| <li><a href="./../../../community/">Community</a></li> |
| <li> |
| <a href="./../../../blog">Blog</a> |
| </li> |
| </ul> |
| </div><!-- /#cassandra-menu --> |
| |
| |
| </div> |
| </nav><!-- /.navbar --> |
| </div><!-- /.topnav --> |
| |
| <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">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="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><!--/.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> |
| |
| <hr /> |
| |
| <footer> |
| <div class="container"> |
| <div class="col-md-4 social-blk"> |
| <span class="social"> |
| <a href="https://twitter.com/cassandra" |
| class="twitter-follow-button" |
| data-show-count="false" data-size="large">Follow @cassandra</a> |
| <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| <a href="https://twitter.com/intent/tweet?button_hashtag=cassandra" |
| class="twitter-hashtag-button" |
| data-size="large" |
| data-related="ApacheCassandra">Tweet #cassandra</a> |
| <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| |
| </span> |
| <a class="subscribe-rss icon-link" href="/feed.xml" title="Subscribe to Blog via RSS"> |
| <span><i class="fa fa-rss"></i></span> |
| </a> |
| </div> |
| |
| <div class="col-md-8 trademark"> |
| <p>© 2016 <a href="http://apache.org">The Apache Software Foundation</a>. |
| Apache, the Apache feather logo, and Apache Cassandra are trademarks of The Apache Software Foundation. |
| <p> |
| </div> |
| </div><!-- /.container --> |
| </footer> |
| |
| <!-- Javascript. Placed here so pages load faster --> |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> |
| <script src="./../../../js/underscore-min.js"></script> |
| <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> |
| |
| |
| <script src="./../../../js/doctools.js"></script> |
| <script src="./../../../js/searchtools.js"></script> |
| |
| <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: "", VERSION: "", COLLAPSE_INDEX: false, FILE_SUFFIX: ".html", HAS_SOURCE: false, SOURCELINK_SUFFIX: ".txt" }; </script> |
| |
| |
| |
| <script type="text/javascript"> |
| var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| |
| try { |
| var pageTracker = _gat._getTracker("UA-11583863-1"); |
| pageTracker._trackPageview(); |
| } catch(err) {} |
| </script> |
| |
| |
| </body> |
| </html> |