λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

πŸ“šμ½μ€ μ±… 정리/μžλ°” μ½”λ”©μ˜ 기술

[μžλ°” μ½”λ”©μ˜ 기술] 3μž₯ : 슬기둭게 주석 μ‚¬μš©ν•˜κΈ°

3.1 μ§€λ‚˜μΉ˜κ²Œ λ§Žμ€ 주석 μ—†μ• κΈ°

μ•„λ¬΄λŸ° μ˜λ―Έμ—†λŠ” 주석은 μ—†μ•€λ‹€.

λŒ€μ‹  μ½”λ“œλ§Œ λ³΄μ•„μ„œλŠ” λ“œλŸ¬λ‚˜μ§€ μ•ŠλŠ” 정보λ₯Ό "주석"으둜 ν‘œμ‹œν•˜μž.

 

3.2 주석 처리된 μ½”λ“œ 제거

주석 처리된 μ½”λ“œλŠ” 일반적인 주석과 달리 λͺ…ν™•νžˆ μ„€λͺ…νžˆ μ—†λ‹€.

μ΄λŠ” μ½”λ“œμ— ν˜Όλž€λ§Œ κ°€μ€‘μ‹œν‚€λŠ” μ“°λ ˆκΈ°λ₯Ό λΌμ–ΉλŠ” 것과 κ°™λ‹€.

 

μ–΄μ°¨ν”Ό 주석 처리된 μ½”λ“œλ₯Ό λ‹€μ‹œ 찾을 일은 μ—†λ‹€. μ œκ±°ν•˜μž.

 

3.3 주석을 μƒμˆ˜λ‘œ λŒ€μ²΄

2μž₯μ—μ„œ λ³΄μ•˜λ˜ "λ§€μ§λ„˜λ²„" μ‚¬μš© μ‹œ 각 λ„˜λ²„μ— μ˜λ―Έμ— λŒ€ν•œ 주석을 λ‹¬μ•„λ†“λŠ” 것은 λ‚˜μ˜μ§€ μ•Šμ€ 방법이닀.

 

ν•˜μ§€λ§Œ μš°λ¦¬λŠ” "μƒμˆ˜"λ₯Ό μ‚¬μš©ν•¨μœΌλ‘œμ¨ 주석없이 μ½”λ“œλ§ŒμœΌλ‘œ 주석을 λŒ€μ²΄ν•  수 μžˆλ‹€.

 

주석을 "μƒμˆ˜, λ³€μˆ˜, ν•„λ“œ, λ©”μ„œλ“œ 이름"으둜 λŒ€μ²΄ν•  수 μžˆλ‹€λ©΄ λ°”λ‘œ μ‹œλ„ν•˜μž!

3.4 주석을 μœ ν‹Έλ¦¬ν‹° λ©”μ„œλ“œλ‘œ λŒ€μ²΄

// round to integer percent
return Math.toIntExact(Math.round(averageFuel * 100));

μœ„μ˜ μ½”λ“œλ₯Ό μ΄ν•΄ν•˜λŠ” 것은 κ°„λ‹¨ν•˜μ§€ μ•Šλ‹€. 그렇기에 주석을 톡해 의미λ₯Ό μ„€λͺ…ν–ˆλ‹€.

 

 

주석을 μ•ˆμ“°κ³  μ½”λ“œλ§ŒμœΌλ‘œ 의미λ₯Ό λ‚΄ν¬μ‹œν‚¬ 순 μ—†μ„κΉŒ?

int roundedToPercent = Math.toIntExact(Math.round(averageFuel * 100));

μš°λ¦¬λŠ” λ³€μˆ˜λͺ…에 의미λ₯Ό λΆ€μ—¬ν•˜κ³  주석을 μ‚­μ œν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ ν•„μš”μ—†λŠ” λ³€μˆ˜κ°€ μ‚¬μš©λœλ‹€.

 

μœ ν‹Έλ¦¬ν‹° λ©”μ„œλ“œ

    int getAverageTankFillingPercent() {
        double sum = 0;
        for (double tankFilling : tanks) {
            sum += tankFilling;
        }
        double averageFuel = sum / tanks.size();
        return roundToIntegerPercent(averageFuel);
    }

    static int roundToIntegerPercent(double value) {
        return Math.toIntExact(Math.round(value * 100));
    }

μœ„ μ½”λ“œμ²˜λŸΌ μœ ν‹Έλ¦¬ν‹° λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λͺ‡κ°€μ§€ μž₯점을 얻을 수 μžˆλ‹€.

  1. μ½”λ“œλͺ…μœΌλ‘œ λ™μž‘μ— λŒ€ν•œ μ„€λͺ…이 λ˜λ―€λ‘œ 주석 μ‚­μ œ κ°€λŠ₯
  2. λ‹€λ₯Έ λ©”μ„œλ“œ μ—μ„œ λ§Œλ“  λ©”μ„œλ“œλ₯Ό μž¬μ‚¬μš©ν•  수 μžˆλ‹€.
  3. λ©”μ„œλ“œμ— 계측 ꡬ쑰가 μƒκΉ€μœΌλ‘œμ¨ μƒμœ„ 계측 λ©”μ„œλ“œμ— λŒ€ν•œ 이해도가 κ°œμ„ λ¨

결둠적으둜 "μœ ν‹Έλ¦¬ν‹° λ©”μ„œλ“œ"λ₯Ό μ‚¬μš©ν•˜λ©΄ μ£Όμ„λ§Œ μ œκ±°λ˜λŠ” 것이 μ•„λ‹ˆλΌ μ½”λ“œκ°€ 더 λͺ¨λ“ˆν™”λ˜κ³  좔상화 μˆ˜μ€€λ„ κ· ν˜•μ„ 이루게 λœλ‹€.

 

3.5 κ΅¬ν˜„ κ²°μ • μ„€λͺ…ν•˜κΈ°

λ©”μ„œλ“œ κ΅¬ν˜„ μ‹œ μ‚¬μš©ν•œ "자료ꡬ쑰"와 "μ•Œκ³ λ¦¬μ¦˜"을 μ™œ μ„ νƒν–ˆλŠ”μ§€μ— λŒ€ν•΄ μžμ„Έν•˜κ²Œ μ„€λͺ…ν•˜κΈ°

 

κ΅¬ν˜„ κ²°μ • μ„€λͺ… ν…œν”Œλ¦Ώ

λ©”μ„œλ“œμ˜ [μ‚¬μš©μ‚¬λ‘€, μš°λ €μ‚¬ν•­, 해법, νŠΈλ ˆμ΄λ“œμ˜€ν”„, λΉ„μš©]κΉŒμ§€ λͺ¨λ‘ μ„€λͺ…ν•œλ‹€.

/*  
In the context μ‚¬μš©μ‚¬λ‘€
facing μš°λ €μ‚¬ν•­
we decided 해법
to achieve ν’ˆμ§ˆ
accepting 단점
*/  

μ΄λŸ¬ν•œ ν…œν”Œλ¦Ώμ„ μ‚¬μš©ν•˜λ©΄ μ½”λ“œλ₯Ό μ΄ν•΄ν•˜κΈ° μ‰¬μ›Œμ§€κ³  미리 μ •μ˜λˆ ꡬ쑰λ₯Ό λ”°λ₯΄λ―€λ‘œ λ™λ£Œ κ°œλ°œμžκ°€ 주석을 μ΄ν•΄ν•˜κΈ° 쉽닀.

 

λͺ¨λ“  μ½”λ“œμ— 이처럼 μ‚¬μš©ν•˜λŠ” 것은 λΉ„νš¨μœ¨μ μ΄λ‹€.

κ·ΈλŸ¬λ―€λ‘œ μ€‘μš”ν•œ κ²°μ •μ΄λ‚˜ κΉŒλ‹€λ‘œμš΄ μ½”λ“œλ₯Ό μ„€λͺ…ν•  λ•Œ μœ„ ν…œν”Œλ¦Ώμ„ μ‚¬μš©ν•˜μž.

 

3.6 예제둜 μ„€λͺ…ν•˜κΈ°

class Supply {

    /**
     * The code universally identifies a supply.
     *
     * It follows a strict format, beginning with an S (for supply), followed
     * by a five digit inventory number. Next comes a backslash that
     * separates the country code from the preceding inventory number. This
     * country code must be exactly two capital letters standing for one of
     * the participating nations (US, EU, RU, CN). After that follows a dot
     * and the actual name of the supply in lowercase letters.
     */
    static final Pattern CODE =
            Pattern.compile("^S\\d{5}\\\\(US|EU|RU|CN)\\.[a-z]+$");
}

μœ„ μ½”λ“œμ—μ„  μ‚¬μš©ν•˜λŠ” μ •κ·œμ‹μ˜ λ™μž‘μ— λŒ€ν•΄ μžμ„Έν•˜κ²Œ μ„€λͺ…ν–ˆμ§€λ§Œ μ–΄λ”˜κ°€ 아쉽닀. (μ΄ν•΄ν•˜κΈ° μ–΄λ ΅λ‹€)

 

class Supply {

    /**
     * μ•„λž˜ ν‘œν˜„μ‹μ€ μ–΄λ””μ„œλ“  재고 μ½”λ“œλ₯Ό μ‹λ³„ν•œλ‹€.
     *
     * Format: "S<inventory-number>\<COUNTRY-CODE>.<name>"
     *
     * Valid examples: "S12345\US.pasta", "S08342\CN.wrench",
     * "S88888\EU.laptop", "S12233\RU.brush"
     *
     * Invalid examples:
     * "R12345\RU.fuel"      (Resource, not supply)
     * "S1234\US.light"      (Need five digits)
     * "S01234\AI.coconut"   (Wrong country code. Use US, EU, RU, or CN)
     * " S88888\EU.laptop "  (Trailing whitespaces)
    */
    static final Pattern SUPPLY_CODE =
            Pattern.compile("^S\\d{5}\\\\(US|EU|RU|CN)\\.[a-z]+$");
}

μœ„ 치럼 λ™μž‘μ— λŒ€ν•΄μ„  ν•œ μ€„λ‘œ κ°„λ‹¨νžˆ ν‘œν˜„ν•˜κ³  μœ νš¨ν•œ μ˜ˆμ œμ™€ μœ νš¨ν•˜μ§€ μ•Šμ€ 예제λ₯Ό 주석을 μ„€λͺ…ν•˜λ©΄ μ½”λ“œλ₯Ό μ΄ν•΄ν•˜κΈ° 훨씬 μ‰¬μ›Œμ§„λ‹€.

 

3.7 νŒ¨ν‚€μ§€λ₯Ό JavaDoc으둜 κ΅¬μ‘°ν™”ν•˜κΈ°

JavaDocμ΄λž€ μžλ°” APIκ°€ μ œκ³΅ν•˜λŠ” λ¬Έμ„œν™” κΈ°λŠ₯이닀.

 

μš”μ•½λ¬Έ, μ „μ²΄ν΄λž˜μŠ€ λͺ©λ‘ λ“± ν•„μš”μ—†λŠ” ν‘œκΈ° μ œμ™Έν•˜κΈ°

 

JavaDoc이 포함해야 ν•˜λŠ” λ‚΄μš©

  1. μ†Œκ°œλ¬Έ : νŒ¨ν‚€μ§€ λ‚΄ 클래슀둜 무엇을 ν•  수 μžˆλŠ”μ§€ μš”μ•½ μ„€λͺ…
  2. νŒ¨ν‚€μ§€ λ‚΄ μ£Όμš” 클래슀둜 무엇을 ν•  수 μžˆλŠ”μ§€ μ„€λͺ…ν•˜κ³  @linkλ₯Ό 톡해 클래슀둜 이동 κ°€λŠ₯ν•˜κ²Œ λ§Œλ“¬
  3. μ£Όμš” μ‚¬μš© 사둀λ₯Ό μ–΄λ–»κ²Œ κ΅¬ν˜„ν•˜λŠ” 지 보여 쀄 것

3.8 ν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό JavaDoc으둜 ꡬ쑰화

JavaDoc μž‘μ„± 방법

  1. μš”μ•½λ¬Έκ³Ό 본문을 λΆ„λ¦¬ν•΄μ„œ μš”μ•½λ¬Έμ΄ 잘 보이게 ν•œλ‹€.
  2. μΈν„°νŽ˜μ΄μŠ€κ°€ 보μž₯ν•˜λŠ” λΆˆλ³€κ³Ό 수직으둜 λΆ„λ¦¬ν•˜λΌ

3.9 λ©”μ„œλ“œλ₯Ό JavaDoc으둜 κ΅¬μ‘°ν™”ν•˜κΈ°

    /**
     * Loads supplies onto the cargo ship.
     *
     * <p>
     * Only lets you load as many supplies as there is remaining capacity.
     *
     * Example:
     * <pre>
     * int capacity = cargoShip.getRemainingCapacity(); // 1
     * Queue&lt;Supply> supplies = Arrays.asList(new Supply("Apple"));
     * Queue&lt;Supply> spareSupplies = cargoShip.load(supplies);
     * spareSupplies.isEmpty(); // true;
     * cargoShip.getRemainingCapacity() == 0; // true
     * </pre>
     *
     * @param supplies to be loaded; must not be null
     * @return supplies that could not be loaded because of too little
     *          capacity; is empty if everything has been loaded
     * @throws NullPointerException if supplies is null
     * @see CargoShip#getRemainingCapacity() check capacity
     * @see CargoShip#unload() unload the supplies
     */
    Queue<Supply> load(Queue<Supply> supplies);

    int getRemainingCapacity();

ν•΄λ‹Ή λ©”μ„œλ“œμ˜ μž…λ ₯κ³Ό λ‚΄λΆ€ μƒνƒœκ°€ νŠΉμ • 좜λ ₯κ³Ό μƒνƒœ 변경을 μ–΄λ–»κ²Œ 보μž₯ν•˜λŠ” 지 λͺ…μ‹œν•œλ‹€.

 

"μ„€λͺ…"κ³Ό "μ½”λ“œ μ˜ˆμ‹œ"κ°€ 적절히 λ“€μ–΄κ°”μœΌλ©° @Param, @throws μ˜ˆμ™Έμ²˜λ¦¬κΉŒμ§€ λͺ…μ‹œν•œλ‹€.

 

@seeλ₯Ό 톡해 λ‹€λ₯Έ λ©”μ„œλ“œλ„ μ°Έμ‘°ν•  수 μžˆλ‹€.

3.10 μƒμ„±μžλ₯Ό JavaDoc으둜 κ΅¬μ‘°ν™”ν•˜κΈ°

μƒμ„±μžλŠ” 의미있고 μ•Œλ§žμ€ 이름을 μƒμ„±μžλͺ…에 ν• λ‹Ήν•  수 μ—†λ‹€.

 

κ·Έλž˜μ„œ JavaDoc 주석은 μƒμ„±μžμ— λŒ€ν•΄ 의미 정보λ₯Ό 적절히 μ„€λͺ…ν•΄μ•Ό ν•œλ‹€.

class Inventory {

    List<Supply> supplies;

    /**
     * Creates an empty inventory.
     *
     * @see Inventory#Inventory(Collection) instantiate with initial supplies //초기 μ œν’ˆμ„ μ΄ˆκΈ°ν™” ν•˜λŠ” ν•¨μˆ˜ 
     */ 
    Inventory() {
        this(new ArrayList<>());
    }

    /**
     * Creates an inventory with an initial shipment of supplies.
     *
     * @param initialSupplies Initial supplies. 
     *                        Must not be null, can be empty. //μƒμ„±μž μ’…λ£Œν›„μ— 객체 μƒνƒœ 정보λ₯Ό μ•Œλ €μ€Œ (사후 정보) , 빈 μƒνƒœκ°€ λ˜κ±°λ‚˜ μ΄ˆκΈ°μ œν’ˆμœΌλ‘œ μ±„μ›Œμ§
     * @throws NullPointerException if initialSupplies is null  //null μž…λ ₯μ‹œμ— NullPointerException λ°˜ν™˜ 
     * @see Inventory#Inventory() instantiate with no supplies //μ œν’ˆμ—†μ΄ μ΄ˆκΈ°ν™” ν•˜λŠ” ν•¨μˆ˜ //두 μƒμ„±μžμ˜ 관계λ₯Ό μ„€λͺ…ν•œλ‹€. 
     */
    Inventory(Collection<Supply> initialSupplies) {
        this.supplies = new ArrayList<>(initialSupplies);
    }
}

@ParamλŠ” λ§€κ°œλ³€μˆ˜λ₯Ό μ„€λͺ…ν•œλ‹€.

@throws -> λ°œμƒν•  수 μžˆλŠ” μ˜ˆμ™Έμ— λŒ€ν•΄ μ„€λͺ…ν•œλ‹€.

@see -> μ§€κΈˆ μ‚¬μš©ν•˜λŠ” 것 μ΄μ™Έμ˜ λ‹€λ₯Έ λŒ€μ•ˆμ„ μ œμ•ˆν•œλ‹€.