-
Notifications
You must be signed in to change notification settings - Fork 0
Description
https://github.com/banksalad/styleguide/tree/master/android
Android Style Guide
Style Guide for Android Developers
Getting Started
- Download CodeStyle.xml
- AndroidStudio > Preferences > Editor > CodeStyle > Scheme > ... > Import Scheme
- Select downloaded
CodeStyle.xml - OK to apply
Kotlin
Kotlin Coding Convention 설정을 기본으로 합니다.
Line Spacing
한 줄은 140자를 초과하지 않습니다.
- Argument가 많다면, 한줄에 하나의 Argument만 적어주세요 (Chop down if long).
...
import com.rainist.banksalad2.R
data class Rainist(
careerPageUrl: String,
banksaladPlayStoreUrl: String
)
val rainist = Rainist(
"https://career.banksalad.com/",
"https://play.google.com/store/apps/details?id=com.rainist.banksalad2"
)Function / Property
Argument 없는 function이 다음 조건을 모두 만족한다면, function 대신 property를 사용합니다.
If a function is/does...
- does not throw Exception
- is O(1) complexity
- is cheap or its computation result is being cached
- is idempotent operation
// Use Property
fun getMaxHeight(): Int = toolbar.height + someTextView.height // (X)
val maxHeight: Int = toolbar.height + someTextView.height // (O)
// Use Function
@Throws(TextViewNotFoundException::class)
fun filterTextViews(views: List<View>): List<View> {
val textViews = views.filterIsInstance<TextView>()
if (textViews.isEmpty()) {
throw TextViewNotFoundException()
}
return textViews
}Network Request / Response
:data 레이어의 Network Response 객체는 다음 구조로 작성되어야 합니다.
data class SampleResponse(
@SerializedName("id")
val id: String,
@SerializedName("name")
val name: String
): Response {
companion object {
val toEntity: Sample = Sample(id)
}
}enum class
Response에 enum이 포함되어 있다면, value property를 만들고, Response 값으로 초기화 합니다.
Raw String을 Mapping해주는 Extension Function을 :data 레이어에 추가합니다.
// Entity Layer
enum class DeletedStatus(val value: String) {
NORMAL("normal"),
DELETED("deleted"),
;
companion object {
fun from(rawValue: String): DeletedStatus =
values().find { it.value == rawValue }!!
}
}
// Data Layer
fun DeletedStatus.from(rawValue: String): DeletedStatus =
values().find { it.value == rawValue }apply / run / with / let / also
특정 객체가 3번 이상 사용되는 상황에서만 사용합니다.
-
객체 자신을 반환할 때는
applyalso를 사용합니다. -
반환 값이 없는 경우에는
with를 사용합니다. -
applyrunwith내부에서는let을 사용합니다.letargument는 named argument로 만듭니다.letargument는 shadow 되지 않도록 합니다.it은 가장 안쪽 블록에서만 사용합니다.
if / else
-
ifelse표현식 각각이 한 줄로 처리될 수 없다면 괄호로 감싸 블록을 만들어 줍니다. -
if ~ else표현식을 통틀어 한 줄로 처리할 수 있는 경우에는 inline 으로 작성합니다. -
if표현식이 블록으로 처리되었더라도,else표현식을 한 줄로 처리할 수 있는 경우에는 inline 으로 작성합니다.
// Good
if (condition) ifExpression else elseExpression
// Good
if (condition) {
ifBlockExpression
} else {
elseBlockExpression
}
// Good
if (condition) {
ifBlockExpression1
ifBlockExpression2
} else {
elseBlockExpression1
elseBlockExpression2
}
// OK
if (condition) {
ifBlockExpression1
ifBlockExpression2
} else elseExpression
// Not Good
if (condition) ifExpression
else {
elseBlockExpression1
elseBlockExpression2
}when
한 줄일 경우에는 inline 하고, 여러 줄인 경우에는 {} Block을 이용합니다.
when (condition) {
caseExpression1 -> inlineExpression1
someLongCaseExpression2 -> inlineExpression2
caseExpression3 -> {
multiLineExpression3_1
multiLineExpression3_2
multiLineExpression3_3
}
}Intent
Intent를 생성하는 함수명은 getIntent()로 생성합니다.
Activity 등 화면 전환 동작을 하는 함수명에는 start 접두사를 붙입니다.
XML
XML Style은 AndroidStudio 기본 XML 스타일을 기본으로 합니다.
Arrangement
XML 태그 내의 각 속성들은 자동 정렬 적용 시 다음과 같은 순서로 배치됩니다:
SORT_BY_NAME 이 적혀진 항목은 해당 조건 내에서 Alphabetical Sort 처리됩니다.
xmlns:androidxmlns:appSORT_BY_NAMExmlns:.*namestylelayoutandroid:idandroid:nameandroid:layout_widthandroid:layout_heightandroid:layout_marginTop.*android:layout_marginStart.*android:layout_marginLeft.*android:layout_marginEnd.*android:layout_marginRight.*android:layout_marginBottom.*android:layout_.*android:paddingTop.*android:paddingStart.*android:paddingLeft.*android:paddingEnd.*android:paddingRight.*android:paddingBottom.*android:widthandroid:heightandroid:viewportWidthandroid:viewportHeightSORT_BY_NAMEandroid:.*app:layout_constraintTop.*app:layout_constraintStart.*app:layout_constraintLeft.*app:layout_constraintEnd.*app:layout_constraintRight.*app:layout_constraintBottom.*SORT_BY_NAMEapp:layout_constraint.*app:layout_constrainedWidthapp:layout_constrainedHeightSORT_BY_NAMEapp:.*SORT_BY_NAME.*
Snapshot
View ID
android:id suffix는 해당 View Type을 사용합니다.
<TextView
android:id="@+id/welcomeTextView"
.../>Java
We don't. Use Default StyleGuide shipped with AndroidStudio






