Skip to content

Code Convention

k_dragonm edited this page Jul 8, 2022 · 5 revisions

Android Coding Convention

๋ชฉ์ฐจ


1. ๊ธฐ๋ณธ

๊ธฐ๋ณธ์ ์œผ๋กœ ๊ณต์‹๋ฌธ์„œ์ธ Kotlin Coding Conventions๊ณผ Kotlin style guide๋ฅผ ์ค€์ˆ˜ํ•œ๋‹ค.

1.1. ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค์˜ Optimize imports ๊ธฐ๋Šฅ๊ณผ Reformat Code ๊ธฐ๋Šฅ์„ ์ด์šฉํ•œ๋‹ค.

  • Optimize imports (Ctrl+Alt+O) : ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” Class๋ฅผ importํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ ์ œ๊ฑฐํ•ด์ค€๋‹ค.
  • Reformat Code (Ctrl+Alt+L) : ์ฝ”๋“œ์˜ Kotlin Style Guide๋ฅผ ์ ์šฉํ•˜์—ฌ Code๋ฅผ Reformatํ•œ๋‹ค.

2. Resource Naming Convention

2.1. ID

View ์ด๋ฆ„์˜ ์ถ•์•ฝํ•˜์—ฌ Camel Case๋กœ ๋ณ€ํ™˜ํ•œ ๊ฒƒ์„ Prefix๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

2.1.1. Prefix

<WHAT>_<DESCRIPTION>

View Prefix
TextView text
ImageView image
EditText edit
Button, ImageButton button
Toolbar toolBar
ConstraintLayout layout
LinearLayout layout
BottomNavigationView bottomNavi
.. ..

2.1.2. ์˜ˆ์‹œ

@+id/textLogin
@+id/editPassword
@+id/buttonLogin

2.2. Layout

Layout์˜ xml ํŒŒ์ผ์˜ ์ด๋ฆ„์€ .kt .java์˜ Pascal Case๋ฅผ Snake Case๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

2.2.1. Prefix

<WHAT>_<WHERE>

View Prefix
Activity activity_
Fragment fragment_
Dialog dialog_
CustomView view_
Item item_

2.2.2. ์˜ˆ์‹œ

SignInActivity.kt -> activity_sign_in.xml
SignUpFragment.kt -> fragment_sign_up.xml
CustomCalendarView.kt -> view_custom_calendar.xml
item_user.xml

2.3. Drawable

2.3.1. Prefix

<WHAT_DESCRIPTION>

Drawable Prefix
Icon ic_
Image img_
Background bg_
Shape <shape>_<color>_<radius>_<value>

2.3.2. ์˜ˆ์‹œ

ic_error.xml
img_default_user.xml
bg_main.xml
rectangle_yellow_radius_20.xml

2.4. Menu

2.4.1. Prefix

menu_


2.5. Color

Color์˜ ์ด๋ฆ„์€ Camel Case๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•˜๋˜, ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด Color๋ฅผ ์ง๊ด€์ ์œผ๋กœ ์—ฐ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฆ„ + RGB ์ด๋ฆ„์„ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋•Œ ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์•ž์— ๋‘๊ธ€์ž๋งŒ ์ ๋Š”๋‹ค.

2.5.1. ์˜ˆ์‹œ

<color name="blackFF000000">#FF000000</color>
<color name="whiteFF">#FFFFFFFF</color>
<color name="cornflowerBlue6195ED">#6195ED</color>

2.6. Style

View์˜ Pascal Case์™€ Style์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์กฐํ•ฉํ•˜์—ฌ Style ๋ช…์„ ์ง€์ •ํ•œ๋‹ค.

2.6.1. Prefix

<WHAT><Description>Style

2.6.2. ์˜ˆ์‹œ

<style name="LoginEditTextStyle"/>
<style name="MainDialogStyle"/>

2.7. String

ํ˜„์žฌ View์ด๋ฆ„๊ณผ ํ•ด๋‹นํ•˜๋Š” String ์„ค๋ช…์„ ์กฐํ•ฉํ•˜์—ฌ string๋ช…์„ ์ง€์ •ํ•œ๋‹ค. ์ด๋•Œ lowerCamelCase๋กœ ์ด๋ฆ„์„ ์ž‘์„ฑํ•œ๋‹ค.

2.7.1. ์˜ˆ์‹œ

<string name="homeCancel">
<string name="myPagePlus">

## 3. Kotlin Naming Convention

3.1. Class

ํด๋ž˜์Šค ํŒŒ์ผ ์ด๋ฆ„์€ UpperCamelCase(ํŒŒ์Šค์นผ ์ผ€์ด์Šค(PascalCase))๋กœ ์ž‘์„ฑํ•œ๋‹ค.

3.1.1. Prefix

<DESCRIPTION><WHAT>

Prefix๋Š” ํ•ด๋‹น ํด๋ž˜์Šค์™€ ๊ด€๋ จ์„ฑ์ด ๋†’์€ ๊ฒƒ์œผ๋กœ ์ž„์˜๋กœ ์ž‘์„ฑํ•œ๋‹ค.

3.1.2. ์˜ˆ์‹œ

MainActivity
UserViewModel
WriteFragment

3.2. Method

๋ฉ”์†Œ๋“œ ์ด๋ฆ„์€ lowerCamelCase๋กœ ์ž‘์„ฑํ•œ๋‹ค.

3.2.1. Prefix

"๋™์‚ฌ"๋กœ ์‹œ์ž‘ํ•˜๋Š” "๋™์‚ฌ๊ตฌ" ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋˜, ๋™์‚ฌ ์›ํ˜•๋งŒ์„ ์‚ฌ์šฉํ•œ๋‹ค.
์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋™์‚ฌ๋Š” ์šฉ๋ฒ•์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค.

Word Description
show Invisibleํ•œ ๊ฒƒ์„ Visibleํ•˜๊ฒŒ ๋ฐ”๊พธ๋Š” ๋™์ž‘
check ์–ด๋–ค ๊ฒƒ์„ ํ™•์ธํ•œ ํ›„ boolean ๋˜๋Š” ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋™์ž‘
is ์–ด๋–ค ๊ฒƒ์ธ์ง€ ํ™•์ธํ•œ ํ›„ boolean์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋™์ž‘
has ์–ด๋–ค๊ฒƒ์„๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ™•์ธ ํ›„ boolean์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋™์ž‘

3.2.2. ์˜ˆ์‹œ

showList
updateContacts

3.3. Variable

๋ณ€์ˆ˜ ์ด๋ฆ„ ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ lowerCamelCase๋กœ ์ž‘์„ฑํ•œ๋‹ค.

3.3.1. ์˜ˆ์‹œ

isEnd
viewPagerAdapter