# 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