Skip to content

Java Suite Style Guide

Julian Kast edited this page May 26, 2020 · 4 revisions

When using Android Studio or IntelliJ, its best to reformat your code using their default "Reformat Code" option.

Copyright should be at the top of all files in a comment block before all code.

/*
 * Copyright (c) 2020 Livio, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Livio Inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

Javadocs

All Classes and methods should have Javadocs add to them. For example,

/**
 * Describe the class
 */
public class JavaDocsExample {
    /**
     * Describe what the function does
     * @param testVar - Give info on parameter passed
     * @return - Give information on what is returned
     */
    public String testing(String testVar) {
	return testVar;
    }

    /**
     * Describe what the function does
     */
    public void testing() {

    }
}

Naming

All naming should follow Google's Java naming conventions.

Package names

Package names are all lowercase with consecutive words simply concatenated together. There are no spaces/special characters/underscores. For example,

Correct

com.sdl.newpackage

Wrong

com.sdl.new_package
com.sdl.newPackage

Class names

Class names should be written in UpperCamelCase. For Example,

Correct

SdlManager

Wrong

sdlManager , sdlmanager, Sdlmanager

Methods and variables

Methods and local variables should be camel-case starting with a lower-case letter. For Example,

String myVariable;
public void myFunctionShouldBeNamedLikeThis(Object myVariable) {
}

Constants and Enums

Constants and Enums should be all uppercase and words separated by underscores. For Example:

public enum HMILevel {

    HMI_FULL("FULL"),

    HMI_LIMITED("LIMITED");

    private final String VALUE;
}

Spacing

You should have proper spacing between arguments and keywords. For Example,

Correct

if (x == y) {
    z = 1;
} else {
    z = 0;
}

Wrong

if(x==y)
{
    z=1;
}
else{
    z=0;
}

Best Practices

Saving instances of services and activities

Don't save instances of services or activities to pass them around to other classes. It's better to use built-in methods (intents, binding, etc) when available. Otherwise, use custom interfaces to pass around.

Intent proxyIntent = new Intent(this, SdlService.class);
startService(proxyIntent);

Static Instances

Don't use them. Ok, you can use them, but only when you absolutely need to. How that is defined is if there is really only either one place of input or output.