JiwonDev

# 2. Java Naming Conventions

by JiwonDev

# ์ฝ”๋”ฉ ์ˆœ์„œ

package -> import -> code ์ˆœ์œผ๋กœ ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค.

๋ณ€์ˆ˜๋Š” public -> protected -> package -> private ์ˆœ์œผ๋กœ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ ์ž๋ฐ”์—์„œ๋Š” ์•„๋ž˜ ๋‘ ์ฝ”๋“œ๊ฐ€ ๊ฐ™์€ ๋™์ž‘์„ ํ•ฉ๋‹ˆ๋‹ค.

int[] a = new int[20]; // ๋ณดํ†ต์˜ ๋ฐฉ์‹
int a[] = new int[20]; // ๊ถŒ์žฅํžˆ์ง€ ์•Š์Œ, ์˜›๋‚ ์˜ C์–ธ์–ด ๋ฐฉ์‹

 


# Comment

์ฐธ๊ณ ๋กœ /** ~ */ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JavaDoc ์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

    /**
        * ๋ฉ”์„œ๋“œ ์„ค๋ช…
        *
        * @param name ์‚ญ์ œํ•  ์œ ์ € ์ด๋ฆ„
        * @return ์œ ์ €๊ฐ€ ์กด์žฌํ•ด์„œ ์‚ญ์ œ๊ฐ€ ๋˜๋ฉด true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , ์œ ์ €๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
        * @throws Exception ๊ทธ๋ƒฅ ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ํ–ˆ์Œ.
   */
   public boolean removeUserName(String name){

   }
    /**
    * class ์„ค๋ช…
    *
    * @author ๊น€์ง€์›
    * @since 2021.04.03
    * @version 1.0
    * <pre>
    * 2021.04.03 : ์ตœ์ดˆ ์ž‘์„ฑ
    * 2021.04.04 : ์ปฌ๋ ‰์…˜ ์ถ”๊ฐ€
    * </pre>
    */
    public class DocExample {
    
    }

์ฝ”๋“œ ์œ„์— ์ฃผ์„์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฉ”์„œ๋“œ๋ช…, ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๊ธฐ๋Šฅ์„ ํ™•์‹คํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ฃผ์„์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

 package example;

    import java.util.ArrayList;
    import java.util.Collection;

    /**
    * JavaDoc ํ…Œ์ŠคํŠธ์šฉ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
    *
    * @author ๊น€์ง€์›
    * @since 2021.04.03
    * @version 1.0
    * <pre>
    * 2021.04.03 : ์ตœ์ดˆ ์ž‘์„ฑ
    * 2021.04.04 : ์ปฌ๋ ‰์…˜ ์ถ”๊ฐ€
    * </pre>
    */
    public class DocExample {

        private Collection<String> userNames;

        public DocExample(String name) {
            userNames = new ArrayList<String>();
        }

        /**
        * ์œ ์ € ์ด๋ฆ„์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ
        *
        * @param name ์‚ญ์ œํ•  ์œ ์ € ์ด๋ฆ„
        * @return ์œ ์ €๊ฐ€ ์กด์žฌํ•ด์„œ ์‚ญ์ œ๊ฐ€ ๋˜๋ฉด true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , ์œ ์ €๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
        * @throws Exception ๊ทธ๋ƒฅ ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ํ–ˆ์Œ.
        */
        public boolean removeUserName(String name)
        throws Exception {
            return userNames.remove(name);
        }

        /**
        * ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ ์“ธ ๋•Œ๋ฅผ ์œ„ํ•œ ์˜ˆ์ œ ๋ฉ”์„œ๋“œ
        *
        * @return ๊ทธ๋ƒฅ ํ…์ŠคํŠธ๊ฐ€ ์ถœ๋ ฅ๋จ
        */
        @Override
        public String toString() {
            return "ํ…Œ์ŠคํŠธ์šฉ์ž…๋‹ˆ๋‹ค.";
        }
    }

 


# General Naming Conventions

1. <ํŒจํ‚ค์ง€> ์†Œ๋ฌธ์ž๋กœ๋งŒ ์‚ฌ์šฉํ•˜์„ธ์š”.

mypackage, com.company.application.ui

 

2. <๋ณ€์ˆ˜> ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ , camelCase ๋ช…์‚ฌ๋กœ ์ง€์–ด์ฃผ์„ธ์š”.

์˜์–ด ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ , ๋‹จ์–ด๊ฐ€ ๋ฐ”๋€Œ๋Š” ๋ถ€๋ถ„์€ camelCase๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

line, myValue, audioSystem, isDone

 

3. <์ƒ์ˆ˜> (final) ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์“ฐ์„ธ์š”. ์—ฌ๋Ÿฌ ๋‹จ์–ด๋Š” ์–ธ๋”๋ฐ”(_)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋‹จ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ์ˆ˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

MAX_VALUE,  COLOR_RED

int getMaxValue() { // NOT : MAX_VALUE = 25
    return 25;
}

์•„๋ž˜ ๊ฐ™์€ ๊ฒฝ์šฐ ์ˆซ์ž๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹จ, ์ƒ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์˜๋ฏธ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

private static final int TEAM_SIZE = 11;
Player[] players = new Player[TEAM_SIZE]; // NOT: Player[] players = new Player[11];

 

4. <๋ฉ”์„œ๋“œ> ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ , ๋™์‚ฌ๋กœ ์ง€์–ด์ฃผ์„ธ์š”.

getName(); computeTotalWidth();

 

5. <์•ฝ์–ด>๋„ ๋˜๋„๋ก์ด๋ฉด camelCase๋ฅผ ์ง€์ผœ์ฃผ์„ธ์š”.

๋ณดํ†ต์˜ ๊ฒฝ์šฐ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค.

exportHtmlSource(); // NOT: exportHTMLSource();
openDvdPlayer(); // NOT: openDVDPlayer();

 

6. ์ผ๋ฐ˜์ ์ธ ๋ณ€์ˆ˜๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด ํƒ€์ž…๊ณผ ๋ณ€์ˆ˜๋ช…์„ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“œ์„ธ์š”.

void setTopic(Topic topic) // NOT: void setTopic(Topic value)
// NOT: void setTopic(Topic aTopic)
// NOT: void setTopic(Topic t)
void connect(Database database) // NOT: void connect(Database db)
// NOT: void connect(Database oracleDB)

 

 

7. ์‚ฌ์šฉ๋ฒ”์œ„๊ฐ€ ๋„“์€ ๋ณ€์ˆ˜๋Š” ์ด๋ฆ„์„ ๊ธธ๊ฒŒ, ์ž ๊น ์‚ฌ์šฉํ•˜๋ฉด ์งง๊ฒŒ ์ง€์œผ์„ธ์š”.

๋‹จ์ˆœ ๋ฃจํ”„๋ฌธ์— ์‚ฌ์šฉ๋  ๋ณ€์ˆ˜๋ฅผ loopIndexNumber ์™€ ๊ฐ™์ด ์ง€์„ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. i, j, k ๋กœ๋„ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค๋งŒ ์—ฌ๋Ÿฌ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜๋Š” ์กฐ๊ธˆ ๊ธธ์–ด๋„ ๊ดœ์ฐฎ์œผ๋‹ˆ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ์˜๋ฏธ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์–ด์ฃผ์„ธ์š”. 

 

8. object์˜ ์ด๋ฆ„์—์„œ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์€ ์ƒ๋žตํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

line.getLength(); // NOT: line.getLineLength();
person.getName(); // NOT: person.getPersonName();

 


# Specific Naming Conventions

1. get, set์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” getter์™€ setter์—๋งŒ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.

employee.getName();
employee.setName(name);
matrix.getElement(2, 4);
matrix.setElement(2, 4, value);

 

2. is๋กœ ์‹œ์ž‘ํ•˜๋ฉด boolean ํƒ€์ž…์„ ๋ฆฌํ„ดํ•ด์ฃผ์„ธ์š”.

๋ฌผ๋ก  ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์—ญํ• ์— ๋”ฐ๋ผ has, can, should ๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

isSet, isVisible, isFinished, isFound, isOpen

// boolen ํŒŒ๋ผ๋ฉ”ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ
void setFound(boolean isFound);

boolean hasLicense();
boolean canEvaluate();
boolean shouldAbort = false;

 

3. ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ compute ๋ฅผ ๋ถ™์—ฌ์ฃผ์„ธ์š”.

๊ณ„์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๊ทธ ๊ฐ’์„ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ช…์„ result ๋กœ ์ง“๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

valueSet.computeAverage();
matrix.computeInverse();

 

4. ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒฝ์šฐ find ๋ฅผ ๋ถ™์—ฌ์ฃผ์„ธ์š”.

vertex.findNearestVertex();
matrix.findSmallestElement();
node.findShortestPath(Node destinationNode);

 

5. ์–ด๋–ค ์ž‘์—…์˜ ์ดˆ๊ธฐํ™”๋Š” initalize๋ฅผ ๋ถ™์—ฌ์ฃผ์„ธ์š”.

init์ด๋ผ๊ณ  ์ค„์—ฌ์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

printer.initializeFontSet();

 

6. GUI ์ปดํฌ๋„ŒํŠธ๋“ค์€ ์ด๋ฆ„ ๋’ค์— component ํƒ€์ž…์„ ๋ถ™์—ฌ์ฃผ์„ธ์š”.

widthScale, nameTextField, leftScrollbar, mainPanel, fileToggle, minLabel

 

7. collection ์ด๋‚˜ ๋ฐฐ์—ด ํƒ€์ž…์€ ๋ณต์ˆ˜ํ˜•(-s)์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

Collection<Point> points;
int[] values;

 

8. ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜์˜ ๊ฒฝ์šฐ ์•ž์— n์„ ๋ถ™์—ฌ์ฃผ์„ธ์š”.

num์ด๋ผ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ€๋…์„ฑ์ด ๋ณ„๋กœ๋ผ๋ฉด ์ฐจ๋ผ๋ฆฌ numberOf ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

nPoints, nLines

 

9. ๊ฐ์ฒด๋‚˜ ์—”ํ‹ฐํ‹ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ๋‚˜ํƒ€๋‚ผ ๊ฒฝ์šฐ No๋ฅผ ๋’ค์— ๋ถ™์—ฌ์ฃผ์„ธ์š”.

๋˜๋Š” iterableํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•ž์— i (iTable, iEmployee)๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

tableNo, employeeNo

 

10. ํ•œ ํด๋ž˜์Šค ๋‚ด์—์„œ ๋Œ€์‘๋˜๋Š” ๋‹จ์–ด set์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

get/set, add/remove, create/destroy, start/stop, insert/delete,
increment/decrement, old/new, begin/end, first/last, up/down, min/max,
next/previous, old/new, open/close, show/hide, suspend/resume, etc.

 

11. ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ๋Š” ์•ฝ์ž๋‚˜ ์ถ•์•ฝํ˜•์„ ์“ฐ์ง€๋งˆ์„ธ์š”.

์ž๋ฐ”์—์„œ๋Š” ๋˜๋„๋ก์ด๋ฉด ์ถ•์•ฝํ˜•์„ ์“ฐ์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

computeAverage(); // NOT: compAvg();
ActionEvent event; // NOT: ActionEvent e;
catch (Exception exception) { // NOT: catch (Exception e) {

๋ฌผ๋ก  ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ํ—ท๊ฐˆ๋ฆด ์ผ์ด ์—†๋Š” ๋‹จ์–ด๋“ค์€ ์ถ•์•ฝํ˜•์œผ๋กœ ์“ฐ์…”๋„ ๋ฉ๋‹ˆ๋‹ค.

HypertextMarkupLanguage =>  html
CentralProcessingUnit => cpu
PriceEarningRatio => pe

 

12. boolean ํƒ€์ž…์„ ๋ถ€์ •์ ์ธ ์˜๋ฏธ๋กœ ์“ฐ์ง€๋งˆ์„ธ์š”.

No, Not์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์„ true๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.

bool isError; // NOT: isNoError
bool isFound; // NOT: isNotFound

 

13. Singleton ํŒจํ„ด์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋Š” getInstance๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.

class UnitManager {
    private final static UnitManager instance_ = new UnitManager();
    private UnitManager() {
        ...
    }
    public static UnitManager getInstance() { // NOT: get() or instance() or unitManager() etc.
        return instance_;
    }
}

 

14. Factory ํŒจํ„ด์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋Š”  new ๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.

class PointFactory {
    public Point newPoint(...) {
        ...
    }
}

 

15. ๋ฆฌํ„ด ํƒ€์ž…์ด ์žˆ๋‹ค๋ฉด ์ฒ˜๋ฆฌ ํ›„ ๋ฌด์—‡์„ ๋ฆฌํ„ดํ•˜๋Š”์ง€ ์ ์–ด์ฃผ์„ธ์š”.

๋ฐ˜๋Œ€๋กœ ๋ฆฌํ„ด ํƒ€์ž…์ด ์—†๋‹ค๋ฉด(void) ๋ฌด์—‡์„ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ์ ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ