Skip to content

Code Style

Vladislav Gasanov edited this page Mar 21, 2018 · 20 revisions

Codestyle and Copyright IDE Settings

License

Each source file has to specify the license at the very top of the file:

/*
 * Copyright (c) 2017 stnetix.com. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

License header file: doc/codestyle/header.txt

Add Copyright to IntelliJ

First step add new scope "stnetix.com".

  • Open File -> Settings.
  • Type "Scopes" into search string and select "Scopes".
  • Press "+" icon for add new scope and type name "stnetix.com" and select "Share scope".
  • Type in Pattern field: src:com.stnetix..*||test:com.stnetix..*.
  • Press "Apply".
  • Press "Ok" (fix for init scope because IDEA 2016.1.1 don't apply setting without press "Ok").

So you can read the official instruction from JetBrains.

Second step add copyright profiles.

  • Open File -> Settings.
  • Type "Copyright" into search string and select "Copyright profiles".
  • Press "+" icon for add new profiles and type name "stnetix.com".
  • Copy Velocity template from file doc/codestyle/header.idea.txt.
  • Press "Apply".
  • Select into settings tree view "Copyright"
  • Press "+" icon for add new references scope to copyright.
  • Set "stnetix.com" as scope and copyright.
  • Press "Apply".
  • Press "Ok".

Checkstyle

Checkstyle runs as part of maven validate lifecycle.

You can call it manually like ./mvnw validate or ./mvnw checkstyle:checkstyle.

checkstyle file: doc/codestyle/checkstyle.xml

Add CheckStyle plugin to IntelliJ

IntelliJ with plugin checkstyle can online show chechstyle issue in the editor area. For install this plugin did:

  • Open "File" -> "Settings" -> "Plugins" and press button "Browse repositories...".
  • Type "checkstyle" in the search string.
  • Select and install "CheckStyle-IDEA" plugin.
  • Restart IntelliJ
  • After restart open "File" -> "Settings"
  • Type "checkstyle" in the search string and select "Checkstyle" from tree view
  • In "Configuration File" table press "+"
  • In the opened dialog set "Description" as "stnetix.com"
  • "Use a local Checkstyle file", browse path to file doc/codestyle/checkstyle.xml and set checkbox "Store relative to project location".
  • Press "Next" button.
  • In the table add value absolute path to doc/codestyle/header.regexp file and press "Next".
  • Press "Finish"
  • Set "Active" checkbox.
  • Set "Apply" and "Ok" button.

After this steps on "Toolbar Buttons" you can see button "CheckStyle". If you press this button you can see CheckStyle tab. On the this tab you can chek some files or packages on codestyle issue. For it set "Rules" as "stnetix.com" and press on of a button "Check Current File", "Check Module", "Check Project", "Check All Modified Files" or "Check Files in the Current Change List". After it in right area you can see list of issue.

Codestyle format

For both Eclipse and IntellJ, import doc/codestyle/eclipse-java-style.xml

Import format settings to IntelliJ IDEA

IntelliJ can import eclipse formatter file.

  • Open File -> Settings.
  • Type "Code" into search string and select "Code Style".
  • Press button "Manage" and "Import..." in the next open dialog.
  • Select "Eclipse XML Profile" and press "Ok"
  • In the next dialog selected select file doc/code-style/eclipse-java-style.xml
  • On request "Scheme 'Stnetix CodeProfile' was copied to be used as the project scheme. Switch to this created scheme?" select "Yes".
  • Once "Stnetix CodeProfile" is imported, select it and click "Copy to Project"
  • Select in tree view "Editor" -> "CodeStyle" -> "Java" and select "Scheme" as "Stnetix CodeProfile".
  • Press "Apply".
  • Press "Ok".

Import checkstyle rule to IntelliJ IDEA

Because some setting we can't import from Eclipse, we should did import other setting from doc/codestyle/checkstyle.xml

  • Open File -> Settings.
  • Type "Code" into search string and select "Code Style".
  • Press button "Manage".
  • Select "Stnetix CodeProfile" and "Import..." in the next open dialog.
  • Select "CheckStyle Configuration" and press "Ok"
  • In the next dialog selected select file doc/codestyle/checkstyle.xml.
  • Press "Ok" and you see note about configuration was updated.
  • Press button "Close".
  • Press "Apply".
  • Press "Ok".

The next codestyle setting will be set when your import configuration files.

Set other settings

Check that line length set to 100

  • Open File -> Settings.
  • Type "Code" into search string and select "Code Style".
  • In the field "Hard wrap at" set 100 columns.
  • Press "Apply".
  • Press "Ok".

Import statements

The "Class count to use import with ''" and "Names count to use static import with''" is 999.

The order of import statements is:

  • import static all other imports
  • blank line
  • import static com.stnetix.*
  • blank line
  • import java.*
  • blank line
  • import javax.*
  • blank line
  • import javafx.*
  • blank line
  • import all other imports
  • blank line
  • import com.stnetix.*

Step for setting import order:

  • Open File -> Settings.
  • Type "Code" into search string and open group "Code Style".
  • Select "Java" and tab "Imports"
  • Set "Class count to use import with ''" and "Names count to use static import with''" is 999.
  • Add package "com.stnetix.*" on the "Import Layout" area. For it press "+" and type "com.stnetix".
  • In the "Import Layout" set order for imports.
  1. import static all other imports
  2. blank line
  3. import static com.stnetix.*
  4. blank line
  5. import java.*
  6. blank line
  7. import javax.*
  8. blank line
  9. import javafx.*
  10. blank line
  11. import all other imports
  12. blank line
  13. import com.stnetix.*
  • Set "Layout static imports separately"
  • Press "Apply".
  • Press "Ok".

Other recomendation

Comments

Comments are always placed at independent line.
Do not append the comment at the end of the line.

Wrong

  KeyStorePassword password = transformer.keyStorePasswordDTOToEntity(passwordDTO);  // get Password Entity from DTO 

Correct

  // get Password Entity from DTO 
  KeyStorePassword password = transformer.keyStorePasswordDTOToEntity(passwordDTO);

Commit message

Follow the widely used format:

Sample:

Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary.  Wrap it to
about 72 characters or so.  In some contexts, the first
line is treated as the subject of an email and the rest of
the text as the body.  The blank line separating the
summary from the body is critical (unless you omit the body
entirely); tools like rebase can get confused if you run
the two together.

Further paragraphs come after blank lines.

  - Bullet points are okay, too

  - Typically a hyphen or asterisk is used for the bullet,
    preceded by a single space, with blank lines in
    between, but conventions vary here
  • 50 char in title
  • Wrap the body at 72 char or less

Clone this wiki locally