• Stars
    star
    387
  • Rank 110,971 (Top 3 %)
  • Language
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kodeco's Kotlin style guide

The Official Kodeco Kotlin Style Guide

This style guide is different from others you may see, because the focus is centered on readability for print and the web. We created this style guide to keep the code in our tutorials consistent.

Our overarching goals are conciseness, readability and simplicity.

You should also check out our other style guides:

Inspiration

This style-guide is somewhat of a mash-up between the existing Kotlin language style guides, and a tutorial-readability focused Swift style-guide. The language guidance is drawn from:

Alterations to support additional readability in tutorials were inspired by the Kodeco Swift style guide.

Android Studio Coding Style

It is possible to get Android Studio to adhere to these style guidelines, via a rather complex sequence of menus. To make it easier, we've provided a coding style that can be imported into Android Studio.

The file can be found here.

To install the file, open Android Studio Settings and go to Editor > Code Style > Kotlin, then click the gear menu and choose Import Scheme....

From now on, projects you create should follow the correct style guidelines.

Table of Contents

Nomenclature

On the whole, naming should follow Java standards, as Kotlin is a JVM-compatible language.

Packages

Package names are similar to Java: all lower-case, multiple words concatenated together, without hypens or underscores:

BAD:

com.YourCompany.funky_widget

GOOD:

com.yourcompany.funkywidget

Classes & Interfaces

Written in UpperCamelCase. For example RadialSlider.

Methods

Written in lowerCamelCase. For example setValue.

Fields

Generally, written in lowerCamelCase. Fields should not be named with Hungarian notation, as Hungarian notation is erroneously thought to be recommended by Google.

Example field names:

class MyClass {
  var publicField: Int = 0
  val person = Person()
  private var privateField: Int?
}

Constant values in the companion object should be written in uppercase, with an underscore separating words:

companion object {
  const val THE_ANSWER = 42
}

Variables & Parameters

Written in lowerCamelCase.

Single character values must be avoided, except for temporary looping variables.

Misc

In code, acronyms should be treated as words. For example:

BAD:

XMLHTTPRequest
URL: String? 
findPostByID

GOOD:

XmlHttpRequest
url: String
findPostById

Declarations

Visibility Modifiers

Only include visibility modifiers if you need something other than the default of public.

BAD:

public val wideOpenProperty = 1
private val myOwnPrivateProperty = "private"

GOOD:

val wideOpenProperty = 1
private val myOwnPrivateProperty = "private"

Access Level Modifiers

Access level modifiers should be explicitly defined for classes, methods and member variables.

Fields & Variables

Prefer single declaration per line.

GOOD:

username: String
twitterHandle: String

Classes

Exactly one class per source file, although inner classes are encouraged where scoping appropriate.

Data Type Objects

Prefer data classes for simple data holding objects.

BAD:

class Person(val name: String) {
  override fun toString() : String {
    return "Person(name=$name)"
  }
}

GOOD:

data class Person(val name: String)

Enum Classes

Enum classes without methods may be formatted without line-breaks, as follows:

private enum CompassDirection { EAST, NORTH, WEST, SOUTH }

Spacing

Spacing is especially important in Kodeco code, as code needs to be easily readable as part of the tutorial.

Indentation

Indentation is using spaces - never tabs.

Blocks

Indentation for blocks uses 2 spaces (not the default 4):

BAD:

for (i in 0..9) {
    Log.i(TAG, "index=" + i)
}

GOOD:

for (i in 0..9) {
  Log.i(TAG, "index=" + i)
}

Line Wraps

Indentation for line wraps should use 4 spaces (not the default 8):

BAD:

val widget: CoolUiWidget =
        someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line)

GOOD:

val widget: CoolUiWidget =
    someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line)

Line Length

Lines should be no longer than 100 characters long.

Vertical Spacing

There should be exactly one blank line between methods to aid in visual clarity and organization. Whitespace within methods should separate functionality, but having too many sections in a method often means you should refactor into several methods.

Comments

When they are needed, use comments to explain why a particular piece of code does something. Comments must be kept up-to-date or deleted.

Avoid block comments inline with code, as the code should be as self-documenting as possible. Exception: This does not apply to those comments used to generate documentation.

Semicolons

Semicolons are dead to us should be avoided wherever possible in Kotlin.

BAD:

val horseGiftedByTrojans = true;
if (horseGiftedByTrojans) {
  bringHorseIntoWalledCity();
}

GOOD:

val horseGiftedByTrojans = true
if (horseGiftedByTrojans) {
  bringHorseIntoWalledCity()
}

Getters & Setters

Unlike Java, direct access to fields in Kotlin is preferred.

If custom getters and setters are required, they should be declared following Kotlin conventions rather than as separate methods.

Brace Style

Only trailing closing-braces are awarded their own line. All others appear the same line as preceding code:

BAD:

class MyClass
{
  fun doSomething()
  {
    if (someTest)
    {
      // ...
    }
    else
    {
      // ...
    }
  }
}

GOOD:

class MyClass {
  fun doSomething() {
    if (someTest) {
      // ...
    } else {
      // ...
    }
  }
}

Conditional statements are always required to be enclosed with braces, irrespective of the number of lines required.

BAD:

if (someTest)
  doSomething()
if (someTest) doSomethingElse()

GOOD:

if (someTest) {
  doSomething()
}
if (someTest) { doSomethingElse() }

When Statements

Unlike switch statements in Java, when statements do not fall through. Separate cases using commas if they should be handled the same way. Always include the else case.

BAD:

when (anInput) {
  1 -> doSomethingForCaseOneOrTwo()
  2 -> doSomethingForCaseOneOrTwo()
  3 -> doSomethingForCaseThree()
}

GOOD:

when (anInput) {
  1, 2 -> doSomethingForCaseOneOrTwo()
  3 -> doSomethingForCaseThree()
  else -> println("No case satisfied")
}

Types

Always use Kotlin's native types when available. Kotlin is JVM-compatible so [TODO: more info]

Type Inference

Type inference should be preferred where possible to explicitly declared types.

BAD:

val something: MyType = MyType()
val meaningOfLife: Int = 42

GOOD:

val something = MyType()
val meaningOfLife = 42

Constants vs. Variables

Constants are defined using the val keyword, and variables with the var keyword. Always use val instead of var if the value of the variable will not change.

Tip: A good technique is to define everything using val and only change it to var if the compiler complains!

Nullable Types

Declare variables and function return types as nullable with ? where a null value is acceptable.

Use implicitly unwrapped types declared with !! only for instance variables that you know will be initialized before use, such as subviews that will be set up in onCreate for an Activity or onCreateView for a Fragment.

When naming nullable variables and parameters, avoid naming them like nullableString or maybeView since their nullability is already in the type declaration.

When accessing a nullable value, use the safe call operator if the value is only accessed once or if there are many nullables in the chain:

editText?.setText("foo")

XML Guidance

Since Android uses XML extensively in addition to Kotlin and Java, we have some rules specific to XML. These can be found in our Java code standards

Language

Use en-US English spelling. πŸ‡ΊπŸ‡Έ

BAD:

val colourName = "red"

GOOD:

val colorName = "red"

Copyright Statement

The following copyright statement should be included at the top of every source file:

/* 
 * Copyright (c) 2023 Kodeco Inc.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
 * distribute, sublicense, create a derivative work, and/or sell copies of the
 * Software in any work that is designed, intended, or marketed for pedagogical or
 * instructional purposes related to programming, coding, application development,
 * or information technology.  Permission for such use, copying, modification,
 * merger, publication, distribution, sublicensing, creation of derivative works,
 * or sale is expressly withheld.
 * 
 * This project and source code may use libraries or frameworks that are
 * released under various Open-Source licenses. Use of those libraries and
 * frameworks are governed by their own individual licenses.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

Smiley Face

Smiley faces are a very prominent style feature of the kodeco.com site! It is very important to have the correct smile signifying the immense amount of happiness and excitement for the coding topic. The closing square bracket ] is used because it represents the largest smile able to be captured using ASCII art. A closing parenthesis ) creates a half-hearted smile, and thus is not preferred.

Bad:

:)

Good:

:]

Credits

This style guide is a collaborative effort from the most stylish kodeco.com team members:

More Repositories

1

swift-algorithm-club

Algorithms and data structures in Swift, with explanations!
Swift
28,779
star
2

swift-style-guide

The official Swift style guide for Kodeco.
12,820
star
3

objective-c-style-guide

A style guide that outlines the coding conventions for Kodeco
3,092
star
4

flta-materials

The projects and the materials that accompany the Flutter Apprentice book
Dart
2,344
star
5

SKTUtils

Sprite Kit helper classes and functions. From the book iOS Games by Tutorials.
Swift
661
star
6

c-sharp-style-guide

C# Style Guide for Game Tech tutorials
484
star
7

ios-interview

The resources and instructions for the iOS Sample Project interview question.
429
star
8

met-materials

The projects and the materials that accompany the Metal by Tutorials book.
Swift
318
star
9

comb-materials

The projects and the materials that accompany the Combine: Asynchronous Programming With Swift Book
Swift
290
star
10

deprecated-books

Kodeco Deprecated Books
258
star
11

sui-materials

The projects and the materials that accompany the SwiftUI by Tutorials book
Swift
215
star
12

adva-materials

The projects and materials that accompany the Real-World Android by Tutorials book
Kotlin
213
star
13

mcon-materials

The projects and materials that accompany the Modern Concurrency in Swift book
Swift
207
star
14

mvc-modern-approach

Sample project for an article at raywenderlich.com.
Swift
170
star
15

english-style-guide

Style guide for writing in English for tutorials and articles at Kodeco.
157
star
16

alg-materials

The projects and the materials that accompany the Data Structures & Algorithms in Swift book
Swift
152
star
17

vapor-til

The TIL Application for the Vapor book
Swift
147
star
18

rxs-materials

The projects and the materials that accompany the RxSwift: Reactive Programming with Swift Book
Swift
147
star
19

java-style-guide

The official Java style guide for raywenderlich.com
Shell
146
star
20

da-materials

The projects and the materials that accompany the Dart Apprentice book
Dart
139
star
21

dsk-materials

The projects and the materials that accompany the Data Structures & Algorithms in Kotlin book
Kotlin
137
star
22

jet-materials

The projects and the materials that accompany the Jetpack Compose book
Kotlin
134
star
23

ia-materials

The projects and materials that accompany the UIKit Apprentice book
HTML
131
star
24

sa-materials

The projects and the materials that accompany the Swift Apprentice book.
Swift
124
star
25

dsad-materials

The projects and the materials that accompany the Data Structures & Algorithms in Dart book
Dart
121
star
26

vpr-materials

The projects and materials that accompany the Server-Side Swift with Vapor book
Swift
110
star
27

arch-materials

The projects and the materials that accompany the Advanced iOS App Architecture book
Swift
97
star
28

atdd-materials

The projects and the materials that accompany the Android Test-Driven Development by Tutorials book
Kotlin
95
star
29

rwf-materials

The projects and materials that accompany the Real-World Flutter by Tutorials book
Dart
93
star
30

learning-roadmaps

Learning roadmaps for students at raywenderlich.com.
82
star
31

apr-materials

The projects and materials that accompany the Apple Augmented Reality by Tutorials book
Swift
81
star
32

suia-materials

The projects and the materials that accompany the SwiftUI Apprentice book
Swift
80
star
33

itdd-materials

The projects and the materials that accompany the iOS Test-Driven Development by Tutorials book
Swift
72
star
34

rwi-materials

The projects and materials that accompany the Real-World iOS by Tutorials book.
Swift
69
star
35

ka-materials

The projects and the materials that accompany the Kotlin Apprentice book.
Kotlin
65
star
36

ana-materials

The projects and materials that accompany the Advanced Android App Architecture book
Kotlin
60
star
37

kco-materials

The projects and materials that accompany the Kotlin Coroutines by Tutorials book
Shell
59
star
38

aa-materials

The projects and the materials that accompany the Android Apprentice book.
Kotlin
59
star
39

mad-materials

The projects and materials that accompany the App Design Apprentice book
Shell
58
star
40

des-materials

The projects and materials that accompany the Design Patterns by Tutorials book
Shell
58
star
41

swiftui-example-app-koober

Porting the example app from our Advanced iOS App Architecture book from UIKit to SwiftUI.
Swift
57
star
42

cdt-materials

The projects and the materials that accompany the Core Data by Tutorials book
Swift
51
star
43

video-yfsa1-materials

Student Materials for Your First iOS and SwiftUI App: An App from Scratch.
Swift
49
star
44

kmpf-materials

The projects and materials that accompany the Kotlin Multiplatform by Tutorials book
Kotlin
48
star
45

iat-materials

The projects and the materials that accompany the iOS Animations by Tutorials book
Shell
44
star
46

dbg-materials

The projects and the materials that accompany the Advanced Apple Debugging & Reverse Engineering book
C
43
star
47

mos-materials

The projects and materials that accompany the macOS by Tutorials book
Swift
42
star
48

advs-materials

The projects and materials that accompany the Expert Swift book
Swift
41
star
49

daf-materials

The projects and the materials that accompany the Dart Apprentice: Fundamentals book
Dart
36
star
50

rxa-materials

The projects and the materials that accompany the Reactive Programming with Kotlin Book
Shell
35
star
51

MyRWTutorial

A reusable starter project for raywenderlich.com tutorials
Swift
34
star
52

video-jcomp-materials

The projects and the materials that accompany the Jetpack Compose course
Kotlin
34
star
53

sat-materials

The projects and materials that accompany the SwiftUI Animations by Tutorials book
Swift
32
star
54

dag-materials

The projects and the materials that accompany the Dagger by Tutorials book
Kotlin
31
star
55

bkk-materials

Kodeco Book Materials Template Repo
Shell
30
star
56

con-materials

About The projects and the materials that accompany the Concurrency by Tutorials book
Shell
28
star
57

RWDevCon-App

The RWDevCon app
Swift
28
star
58

sda-materials

The projects and the materials that accompany the Saving Data on Android book
Kotlin
28
star
59

wos-materials

The projects and the materials that accompany the watchOS With SwiftUI by Tutorials book
Swift
27
star
60

recipes

27
star
61

fpk-materials

The projects and materials that accompany the Functional Programming in Kotlin by Tutorials book
Kotlin
26
star
62

pasi-materials

The projects and the materials that accompany the iOS App Distribution & Best Practices book.
Swift
26
star
63

agit-materials

The projects and the materials that accompany the Advanced Git Book
Shell
25
star
64

SC_SusmitaHorrow

Swift
24
star
65

dabb-materials

The projects and the materials that accompany the Dart Apprentice: Beyond the Basics book
Dart
24
star
66

gita-materials

The projects and the materials that accompany the Git Apprentice book
Shell
24
star
67

not-materials

The projects and the materials that accompany the Push Notifications by Tutorials book
Swift
22
star
68

maca-materials

The projects and materials that accompany the macOS Apprentice book
Swift
21
star
69

RWAndroidTutorial

A reusable starter project for raywenderlich.com Android tutorials.
Kotlin
20
star
70

programmer-jokes

Sample repository for the Git Apprentice book
19
star
71

video-ps1-materials

The projects and the materials that accompany the Programming in Swift: Fundamentals course
Swift
19
star
72

alt-materials

The projects and materials that accompany the Auto Layout by Tutorials book
Swift
19
star
73

aat-materials

The projects and materials that accompany the Android Animations by Tutorials book
Kotlin
19
star
74

mlt-materials

The projects and the materials that accompany the Machine Learning by Tutorials book
Shell
18
star
75

video-insta-materials

The projects and the materials that accompany the How to Make an App Like Instagram in iOS video course
Swift
17
star
76

video-yfsa2-materials

Student Materials for Your First iOS and SwiftUI App: Polishing the App.
Swift
17
star
77

universal-links

HTML
17
star
78

video-comb-materials

The projects and the materials that accompany the Reactive Programming in iOS with Combine course
Swift
16
star
79

cat-materials

The projects and the materials that accompany the Catalyst by Tutorials Book
Swift
16
star
80

video-sdios-materials

The projects and the materials that accompany the Saving Data in iOS course
Swift
15
star
81

video-suif-materials

The projects and the materials that accompany the SwiftUI Fundamentals course
Swift
15
star
82

uapp-materials

The projects and the materials that accompany the Unity Apprentice book
C#
15
star
83

android-bootcamp-summer-2020

Homework solutions for the Android Bootcamp, class of Summer 2020.
Kotlin
15
star
84

video-nurls-materials

The projects and the materials that accompany the Networking with URLSession video course
Swift
15
star
85

kodeco-android-cookiecutter-template

A cookiecutter πŸͺ template for bootstrapping new Android Tutorial projects for Kodeco!
Kotlin
14
star
86

ideas

The "ideas" repository for the raywenderlich.com book Mastering Git
14
star
87

video-ukf-materials

The projects and the materials that accompany the UIKit Fundamentals video course
Swift
13
star
88

git-materials

The projects and the materials that accompany the Mastering Git Book
Shell
13
star
89

video-tv-materials

The projects and the materials that accompany the Table Views video course
Swift
12
star
90

rwdevcon-materials

Required materials for RWDevCon attendees.
12
star
91

video-jca-materials

The projects and materials that accompany the Jetpack Compose Animations course
Kotlin
12
star
92

video-iosa-materials

The projects and the materials that accompany the UIKit Animation course
Swift
12
star
93

saf-materials

The projects and the materials that accompany the Swift Apprentice:Fundamentals book.
Swift
12
star
94

video-kf1-materials

The projects and the materials that accompany the Kotlin Flow: Getting Started video course
Kotlin
11
star
95

DadJokes

A way to improve the programmer's day.
Swift
11
star
96

video-aidp-materials

The projects and materials that accompany the Advanced iOS Design Patterns course
Swift
11
star
97

video-abp-materials

The projects and the materials that accompany the Android Background Processing video course
Kotlin
10
star
98

adf-materials

The projects and the materials that accompany the Android Debugging by Tutorials book
Kotlin
10
star
99

video-yffa-materials

The projects and the materials that accompany the Your First Flutter App: An App From Scratch course
Dart
10
star
100

video-sli-materials

The projects and the materials that accompany the SwiftUI: Layout & Interfaces video course
Swift
10
star