Skip to content

2. Android Coding Convention

mdb1217 edited this page Jul 4, 2022 · 4 revisions

Android Coding Convention

RevisedBy

λͺ©μ°¨


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 μ΄λ¦„μ˜ Pascal Caseλ₯Ό μΆ•μ•½ν•˜μ—¬ Snake Case둜 λ³€ν™˜ν•œ 것을 Prefix둜 μ‚¬μš©ν•œλ‹€.

2.1.1. Prefix

<WHAT>_<DESCRIPTION>

View Prefix
TextView tv_
ImageView iv_
EditText et
Button, ImageButton btn_
Toolbar tb_
ConstraintLayout cl_
LinearLayout ll_
BottomNavigationView bnv_
.. ..

2.1.2. μ˜ˆμ‹œ

@+id/tv_login
@+id/et_password
@+id/btn_login

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λ₯Ό μ§κ΄€μ μœΌλ‘œ 연상할 수 μžˆλŠ” 이름을 μ‚¬μš©ν•œλ‹€.

2.5.1. μ˜ˆμ‹œ

<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="cornflowerBlue">#6195ED</color>

2.6. String

String μž‘μ„± μ‹œ 주석을 톡해 String이 μ‚¬μš©λ˜λŠ” 곳을 λͺ…μ‹œν•œλ‹€.

2.6.1. Prefix

<WHERE/WHAT>_<DESCRIPTION>

2.6.2. μ˜ˆμ‹œ

<!--Main Menu-->
<string name="menu_daily">ν•˜λ£¨μ˜ 기둝</string>
<string name="menu_remind">평가 및 회고</string>
<string name="menu_my">My</string>

<!--Toolbar Title-->
<string name="title_search">검색</string>
<string name="title_settings">ν™˜κ²½μ„€μ •</string>

<!--Dialog Message-->
<string name="msg_login">λ‘œκ·ΈμΈν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?</string>
<string name="msg_login_failed">λ‘œκ·ΈμΈμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</string>
<string name="msg_password_error">λΉ„λ°€λ²ˆν˜Έκ°€ μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.</string>

2.7. Style

View의 Pascal Case와 Style에 λŒ€ν•œ μ„€λͺ…을 μ‘°ν•©ν•˜μ—¬ Style λͺ…을 μ§€μ •ν•œλ‹€.

2.7.1. Prefix

<WHAT><Description>Style

2.7.2. μ˜ˆμ‹œ

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

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