Skip to content

Commit

Permalink
Adjust codestyle (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu authored Oct 2, 2023
1 parent 27615ca commit 247afd2
Show file tree
Hide file tree
Showing 10 changed files with 736 additions and 104 deletions.
9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.2.1/userguide/building_java_projects.html in the Gradle documentation.
*/

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id("org.jetbrains.kotlin.jvm") version "1.8.20"
Expand Down Expand Up @@ -66,4 +69,10 @@ java {
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
// Log useful information when running tests.
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = setOf(TestLogEvent.SKIPPED, TestLogEvent.PASSED, TestLogEvent.FAILED)
showStandardStreams = true
}
}
231 changes: 231 additions & 0 deletions codestyle/codestyle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# Code style

Before writing your first line of code, please import `codestyles.xml` into your IDE settings from `docs/codestyles/codestyles.xml`

---

## Add trailing comma when possible and makes sense

**Note:** That makes Git history easier to traverse, adding comma to line before is not overriding this line Git history

```kotlin
val someMap = mapOf(
"a" to "b",
"c" to "d",
)

class GreatClass(
val fieldA: String,
val fieldB: String,
)
```

---

## Don't explicitly define a type when it's not required

```kotlin
// 🔴 don't
val someVariable: String = "asd"

// 🟢 do
val someVariable = "asd"
```

**Note:** If you are still missing explicit type definition, consider enabling `Type Hints` in Android Studio, IDE will add them automatically.

---

## Empty statements or lambdas

We want to make sure that empty or blank statements are not left there by accident, that's why you need to use `doNothing()` function from `com.dazn.extensions` for marking it as intentionally empty.

```kotlin
when (x) {
VALUE_1 -> println("1")
VALUE_2 -> println("2")
else -> doNothing()
}
```

```kotlin
executeMethodWithCallbacks(
success = { view.showSomething() },
error = { doNothing() }
)
```

---

## Functions and its parameters

### When function only returns something, remove it's body

```kotlin
// 🔴 don't
fun returnNumber(): Int {
return 2137
}

// 🟢 do
fun returnNumber() = 2137
```

### When function only returns something and it has a long call inside, break it with new lines

```kotlin
// 🔴 don't
fun returnSomething() = thisIsVeryImportantVariableNameBecauseItsNameIsLong.andThisIsVeryImportantMethodName(thisLooksLikeAnImportantParameter)

// 🟢 do
fun returnSomething() =
thisIsVeryImportantVariableNameBecauseItsNameIsLong.andThisIsVeryImportantMethodName(thisLooksLikeAnImportantParameter)

// 🟢 do
fun returnSomething() =
thisIsVeryImportantVariableNameBecauseItsNameIsLong.andThisIsVeryImportantMethodName(
thisLooksLikeAnImportantParameter,
ohNoThisMethodHasAnotherParameter,
)
```

### Underscore unused parameters

```kotlin
// 🔴 don't
val function: (Int, Long, Double) -> Unit = { someInt, someLong, someDouble -> println("") }

// 🟢 do
val function: (Int, Long, Double) -> Unit = { _, _, _ -> println("") }
```

### When function definition is starting to be unreadable, put parameters in separate lines

```kotlin
// 🔴 don't
fun someFunction(parameterA: ParameterOfTypeA, parameterB: ParameterOfTypeB, parameterC: ParameterOfTypeC, parameterD: ParameterOfTypeD)

// 🟢 do
fun someFunction(
parameterA: ParameterOfTypeA,
parameterB: ParameterOfTypeB,
parameterC: ParameterOfTypeC,
parameterD: ParameterOfTypeD,
)
```

### When function contains a large mix of string and non standard classes, consider adding explicit param names

```kotlin
// 🔴 don't
someFunction(
"a",
CustomObject(),
"b",
"c"
)

// 🟢 do
someFunction(
parameterA = "a",
customObject = CustomObject(),
parameterB = "b",
parameterC = "c",
)
```

### When there is need to use line separator, use system provided one

```kotlin
// 🔴 don't
listOf("hello", "world").joinToString(separator = "\n")

// 🟢 do
listOf("hello", "world").joinToString(separator = System.lineSeparator())
```

---

## Enums

### Short enums

```kotlin
enum class ShortEnum {
VALUE_1,
VALUE_2,
VALUE_3,
}
```

### Enums with value

```kotlin
enum class EnumWithValue(val value: String) {
VALUE_1("value1"),
VALUE_2("value2"),
VALUE_3("value3"),
}
```

### Enums with value and method

**Note:** Semicolon in new line, between enum entries and body

```kotlin
enum class EnumWithValueAndMethod(val value: String) {
VALUE_1("value1"),
VALUE_2("value2"),
VALUE_3("value3"),
;

companion object {
fun fromString(string: String) = values().firstOrNull { it.value == string }
}
}
```

---

## Annotations

### For parameters keep annotations in the same line

```kotlin
data class SomeClass(
@SerializedName("field1") val field1: String,
@SerializedName("field2") val field2: String?,
)
```

### For methods, fields and classes keep them in new line

```kotlin
@AnnotationForClass
class SomeClass constructor(
@FieldAnnotation private val field1
) {

@FieldAnnotation
val field2: String

@AnnotationForMethod
fun annotatedMethod() {
doNothing()
}
}
```

### In other cases refer to Official Kotlin Style Guide

[Google Kotlin Style Guide](https://android.github.io/kotlin-guides/style.html)

### Don't use Hungarian notation, if it's still on your mind for some reason

[Just Say mNo to Hungarian Notation (Jake Wharton)](http://jakewharton.com/just-say-no-to-hungarian-notation/)

### RxJava/Stream chains

Those can get pretty ugly sometimes.

If possible, avoid deep nesting in RxJava/Stream chains. Instead, try to refactor operator arguments into separate methods. A good rule of thumb for clean and readable chains: **one line = one operator**.
Loading

0 comments on commit 247afd2

Please sign in to comment.