코드 스타일

경고

This documentation refers to an outdated SpongeAPI version and is no longer actively maintained. While the code examples still work for that API version, the policies, guidelines, and some links may have changed. Please refer to the latest version of the documentation for those.

우리는 이 문서에 설명되어 있는 `구글 자바 스타일 가이드라인<https://google.github.io/styleguide/javaguide.html>`을 개조한 스타일을 따릅니다.

You can use our code styles for Eclipse or IntelliJ IDEA to let your IDE format the code correctly for you. See 개발 준비하기 for more information.

  • 줄 끝

    • 유닉스 개행 문자를 사용하세요 (\n)

      • 윈도우 Git 사용자라면 git config --global core.autocrlf true 로 Git 을 통해 자동 변환할 수 있습니다.

  • Column width

    • 80 for Javadocs

    • 150 for code

    • Feel free to wrap when it will help with readability

  • 들여쓰기

    • 공백 4개를 사용해주세요, 2개는 안됩니다.

  • Vertical whitespace

    • Place a blank line before the first member of a class, interface, enum, etc. (i.e. after class Example {) as well as after the last member

  • 파일 헤더

    • File headers must contain the license headers for the project. Use the licenseFormat Gradle task to add them automatically.

  • Imports

    • Imports must be grouped in the following order, where each group is separated by an empty line

      • Static imports

      • All other imports

      • java imports

      • javax imports

    • This differs from Google’s style in that imports are not grouped by top-level package, they are all grouped as one.

  • 예외

    • For exceptions that are to be ignored, name the exception variable ignored

  • Field accesses

    • Qualify all field accesses with this

  • Javadocs

    • Do not use @author

    • Wrap additional paragraphs in <p> and </p>

    • Capitalize the first letter in the descriptions within each “at clause”, i.e. @param name Player to affect, no periods

Code Conventions

  • Use Optional instead of returning null in the API

  • Method parameters accepting null must be annotated with @Nullable (from javax.*), all methods and parameters are @Nonnull by default.

  • Use Google Preconditions for null- and argument checking.

The Gist

While we urge that you read Google’s Java conventions particularly, the two are fairly long documents. To get you started quickly, here is an example of properly formatted code:

/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered.org <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.example.java;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.Optional;

public class Example {

    private static final Logger log = LoggerFactory.getLogger(Example.class);
    private static final Random random = new Random();
    private final String id = "test";

    /**
     * Returns an identifier approximately half of the time.
     *
     * <p>A static instance of {@link Random} is used to calculate the
     * outcome with a 50% chance.</p>
     *
     * @return The ID, if available
     */
    public Optional<String> resolveId() {
        log.info("ID requested");

        if (random.nextBoolean()) {
            return Optional.of(this.id);
        } else {
            return Optional.empty();
        }
    }

    /**
     * Returns an identifier approximately half of the time.
     *
     * <p>A static instance of {@link Random} is used to calculate the
     * outcome with a 50% chance. If the outcome is to not return the ID,
     * the given fallback ID is returned.</p>
     *
     * @param fallback A fallback name to return
     * @return The ID half of the time, the given fallback the other half
     */
    public String resolveId(String fallback) {
        return resolveId().orElse(fallback);
    }

}