{"id":13697335,"url":"https://github.com/kodecocodes/kotlin-style-guide","last_synced_at":"2025-10-04T08:14:50.001Z","repository":{"id":60312090,"uuid":"92320743","full_name":"kodecocodes/kotlin-style-guide","owner":"kodecocodes","description":"Kodeco's Kotlin style guide","archived":false,"fork":false,"pushed_at":"2023-03-08T15:23:01.000Z","size":89,"stargazers_count":396,"open_issues_count":0,"forks_count":113,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-05-03T20:33:39.730Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kodecocodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-24T17:54:26.000Z","updated_at":"2025-04-14T08:20:17.000Z","dependencies_parsed_at":"2024-07-17T15:41:44.300Z","dependency_job_id":null,"html_url":"https://github.com/kodecocodes/kotlin-style-guide","commit_stats":null,"previous_names":["raywenderlich/kotlin-style-guide"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kodecocodes/kotlin-style-guide","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fkotlin-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fkotlin-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fkotlin-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fkotlin-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kodecocodes","download_url":"https://codeload.github.com/kodecocodes/kotlin-style-guide/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fkotlin-style-guide/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278283598,"owners_count":25961334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T18:00:56.330Z","updated_at":"2025-10-04T08:14:49.987Z","avatar_url":"https://github.com/kodecocodes.png","language":null,"funding_links":[],"categories":["Cross-Platform Mobile Guide for Native iOS \u0026 Android Developers","Others"],"sub_categories":["Android"],"readme":"# The Official Kodeco Kotlin Style Guide\n\nThis 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.\n\nOur overarching goals are __conciseness__, __readability__ and __simplicity__.\n\nYou should also check out our other style guides:\n\n* [Swift](https://github.com/kodecocodes/swift-style-guide)\n* [Objective-C](https://github.com/kodecocodes/objective-c-style-guide)\n* [Java](https://github.com/kodecocodes/java-style-guide)\n\n## Inspiration\n\nThis 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: \n\n- The [Android Kotlin style guide](https://android.github.io/kotlin-guides/style.html)\n- The [Kotlin Coding Conventions](https://kotlinlang.org/docs/reference/coding-conventions.html) \n- The [Android contributors style guide](https://source.android.com/source/code-style.html)\n- The [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html).\n\nAlterations to support additional readability in tutorials were inspired by the [Kodeco Swift style guide](https://github.com/kodecocodes/swift-style-guide).\n\n## Android Studio Coding Style\n\nIt 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.\n\nThe file can be found [here](https://koenig-media.raywenderlich.com/uploads/2018/03/rwstyle.xml_.zip).\n\nTo install the file, open Android Studio Settings and go to **Editor \u003e Code Style \u003e Kotlin**, then click the gear menu and choose **Import Scheme...**.\n\nFrom now on, projects you create _should_ follow the correct style guidelines.\n\n\n## Table of Contents\n\n- [Nomenclature](#nomenclature)\n  + [Packages](#packages)\n  + [Classes \u0026 Interfaces](#classes--interfaces)\n  + [Methods](#methods)\n  + [Fields](#fields)\n  + [Variables \u0026 Parameters](#variables--parameters)\n  + [Misc](#misc)\n- [Declarations](#declarations)\n  + [Visibility Modifiers](#visibility-modifiers)\n  + [Fields \u0026 Variables](#fields--variables)\n  + [Classes](#classes)\n  + [Data Type Objects](#data-type-objects)\n  + [Enum Classes](#enum-classes)\n- [Spacing](#spacing)\n  + [Indentation](#indentation)\n  + [Line Length](#line-length)\n  + [Vertical Spacing](#vertical-spacing)\n- [Semicolons](#semicolons)\n- [Getters \u0026 Setters](#getters--setters)\n- [Brace Style](#brace-style)\n- [When Statements](#when-statements)\n- [Annotations](#annotations)\n- [Types](#types)\n  + [Type Inference](#type-inference)\n  + [Constants vs. Variables](#constants-vs-variables)\n  + [Optionals](#optionals)\n- [XML Guidance](#xml-guidance)\n- [Language](#language)\n- [Copyright Statement](#copyright-statement)\n- [Smiley Face](#smiley-face)\n- [Credit](#credits)\n\n\n## Nomenclature\n\nOn the whole, naming should follow Java standards, as Kotlin is a JVM-compatible language.\n\n### Packages\n\nPackage names are similar to Java: all __lower-case__, multiple words concatenated together, without hypens or underscores:\n\n__BAD__:\n\n```kotlin\ncom.YourCompany.funky_widget\n```\n\n__GOOD__:\n\n```kotlin\ncom.yourcompany.funkywidget\n```\n\n### Classes \u0026 Interfaces\n\nWritten in __UpperCamelCase__. For example `RadialSlider`. \n\n### Methods\n\nWritten in __lowerCamelCase__. For example `setValue`.\n\n### Fields\n\nGenerally, written in __lowerCamelCase__. Fields should **not** be named with Hungarian notation, as Hungarian notation is [erroneously thought](http://jakewharton.com/just-say-no-to-hungarian-notation/) to be recommended by Google.\n\nExample field names:\n\n```kotlin\nclass MyClass {\n  var publicField: Int = 0\n  val person = Person()\n  private var privateField: Int?\n}\n```\n\nConstant values in the companion object should be written in __uppercase__, with an underscore separating words:\n\n```kotlin\ncompanion object {\n  const val THE_ANSWER = 42\n}\n```\n\n### Variables \u0026 Parameters\n\nWritten in __lowerCamelCase__.\n\nSingle character values must be avoided, except for temporary looping variables.\n\n### Misc\n\nIn code, acronyms should be treated as words. For example:\n\n__BAD:__\n\n```kotlin\nXMLHTTPRequest\nURL: String? \nfindPostByID\n```\n__GOOD:__\n\n```kotlin\nXmlHttpRequest\nurl: String\nfindPostById\n```\n\n## Declarations\n\n### Visibility Modifiers\n\nOnly include visibility modifiers if you need something other than the default of public.\n\n**BAD:**\n\n```kotlin\npublic val wideOpenProperty = 1\nprivate val myOwnPrivateProperty = \"private\"\n```\n\n**GOOD:**\n\n```kotlin\nval wideOpenProperty = 1\nprivate val myOwnPrivateProperty = \"private\"\n```\n\n### Access Level Modifiers\n\nAccess level modifiers should be explicitly defined for classes, methods and member variables.\n\n### Fields \u0026 Variables\n\nPrefer single declaration per line.\n\n__GOOD:__\n\n```kotlin\nusername: String\ntwitterHandle: String\n```\n\n### Classes\n\nExactly one class per source file, although inner classes are encouraged where scoping appropriate.\n\n### Data Type Objects\n\nPrefer data classes for simple data holding objects.\n\n__BAD:__\n\n```kotlin\nclass Person(val name: String) {\n  override fun toString() : String {\n    return \"Person(name=$name)\"\n  }\n}\n```\n\n__GOOD:__\n\n```kotlin\ndata class Person(val name: String)\n```\n\n### Enum Classes\n\nEnum classes without methods may be formatted without line-breaks, as follows:\n\n```kotlin\nprivate enum CompassDirection { EAST, NORTH, WEST, SOUTH }\n```\n\n## Spacing\n\nSpacing is especially important in Kodeco code, as code needs to be easily readable as part of the tutorial. \n\n### Indentation\n\nIndentation is using spaces - never tabs.\n\n#### Blocks\n\nIndentation for blocks uses 2 spaces (not the default 4):\n\n__BAD:__\n\n```kotlin\nfor (i in 0..9) {\n    Log.i(TAG, \"index=\" + i)\n}\n```\n\n__GOOD:__\n\n```kotlin\nfor (i in 0..9) {\n  Log.i(TAG, \"index=\" + i)\n}\n```\n\n#### Line Wraps\n\nIndentation for line wraps should use 4 spaces (not the default 8):\n\n__BAD:__\n\n```kotlin\nval widget: CoolUiWidget =\n        someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line)\n```\n\n__GOOD:__\n\n```kotlin\nval widget: CoolUiWidget =\n    someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line)\n```\n\n### Line Length\n\nLines should be no longer than 100 characters long.\n\n\n### Vertical Spacing\n\nThere 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.\n\n## Comments\n\nWhen they are needed, use comments to explain **why** a particular piece of code does something. Comments must be kept up-to-date or deleted.\n\nAvoid 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.*\n\n\n## Semicolons\n\nSemicolons ~~are dead to us~~ should be avoided wherever possible in Kotlin. \n\n__BAD__:\n\n```kotlin\nval horseGiftedByTrojans = true;\nif (horseGiftedByTrojans) {\n  bringHorseIntoWalledCity();\n}\n```\n\n__GOOD__:\n\n```kotlin\nval horseGiftedByTrojans = true\nif (horseGiftedByTrojans) {\n  bringHorseIntoWalledCity()\n}\n```\n\n## Getters \u0026 Setters\n\nUnlike Java, direct access to fields in Kotlin is preferred. \n\nIf custom getters and setters are required, they should be declared [following Kotlin conventions](https://kotlinlang.org/docs/reference/properties.html) rather than as separate methods.\n\n## Brace Style\n\nOnly trailing closing-braces are awarded their own line. All others appear the same line as preceding code:\n\n__BAD:__\n\n```kotlin\nclass MyClass\n{\n  fun doSomething()\n  {\n    if (someTest)\n    {\n      // ...\n    }\n    else\n    {\n      // ...\n    }\n  }\n}\n```\n\n__GOOD:__\n\n```kotlin\nclass MyClass {\n  fun doSomething() {\n    if (someTest) {\n      // ...\n    } else {\n      // ...\n    }\n  }\n}\n```\n\nConditional statements are always required to be enclosed with braces, irrespective of the number of lines required.\n\n__BAD:__\n\n```kotlin\nif (someTest)\n  doSomething()\nif (someTest) doSomethingElse()\n```\n\n__GOOD:__\n\n```kotlin\nif (someTest) {\n  doSomething()\n}\nif (someTest) { doSomethingElse() }\n```\n\n## When Statements\n\nUnlike `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.\n\n__BAD:__\n\n```kotlin\nwhen (anInput) {\n  1 -\u003e doSomethingForCaseOneOrTwo()\n  2 -\u003e doSomethingForCaseOneOrTwo()\n  3 -\u003e doSomethingForCaseThree()\n}\n```\n\n__GOOD:__\n\n```kotlin\nwhen (anInput) {\n  1, 2 -\u003e doSomethingForCaseOneOrTwo()\n  3 -\u003e doSomethingForCaseThree()\n  else -\u003e println(\"No case satisfied\")\n}\n```\n\n\n## Types \n\nAlways use Kotlin's native types when available. Kotlin is JVM-compatible so **[TODO: more info]**\n\n### Type Inference\n\nType inference should be preferred where possible to explicitly declared types. \n\n__BAD:__\n\n```kotlin\nval something: MyType = MyType()\nval meaningOfLife: Int = 42\n```\n\n__GOOD:__\n\n```kotlin\nval something = MyType()\nval meaningOfLife = 42\n```\n\n### Constants vs. Variables \n\nConstants 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.\n\n*Tip*: A good technique is to define everything using `val` and only change it to `var` if the compiler complains!\n\n### Nullable Types\n\nDeclare variables and function return types as nullable with `?` where a `null` value is acceptable.\n\nUse 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.\n\nWhen naming nullable variables and parameters, avoid naming them like `nullableString` or `maybeView` since their nullability is already in the type declaration.\n\nWhen 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:\n\n```kotlin\neditText?.setText(\"foo\")\n```\n\n\n\n## XML Guidance\n\nSince 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](https://github.com/kodecocodes/java-style-guide#xml-guidance)\n\n\n## Language\n\nUse `en-US` English spelling. 🇺🇸\n\n__BAD:__\n\n```kotlin\nval colourName = \"red\"\n```\n\n__GOOD:__\n\n```kotlin\nval colorName = \"red\"\n```\n\n## Copyright Statement\n\nThe following copyright statement should be included at the top of every source file:\n\n```\n/* \n * Copyright (c) 2023 Kodeco Inc.\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n * \n * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,\n * distribute, sublicense, create a derivative work, and/or sell copies of the\n * Software in any work that is designed, intended, or marketed for pedagogical or\n * instructional purposes related to programming, coding, application development,\n * or information technology.  Permission for such use, copying, modification,\n * merger, publication, distribution, sublicensing, creation of derivative works,\n * or sale is expressly withheld.\n * \n * This project and source code may use libraries or frameworks that are\n * released under various Open-Source licenses. Use of those libraries and\n * frameworks are governed by their own individual licenses.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\n ```\n \n## Smiley Face\n\nSmiley 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.\n\nBad:\n\n    :)\n\nGood:\n\n    :]\n\n## Credits\n\nThis style guide is a collaborative effort from the most stylish\nkodeco.com team members:\n\n- [Darryl Bayliss](https://github.com/DarrylBayliss)\n- [Tom Blankenship](https://github.com/tgblank)\n- [Sam Davies](https://github.com/sammyd)\n- [Mic Pringle](https://github.com/micpringle)\n- [Ellen Shapiro](https://github.com/designatednerd)\n- [Ray Wenderlich](https://github.com/rwenderlich)\n- [Joe Howard](https://github.com/orionthwake)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodecocodes%2Fkotlin-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkodecocodes%2Fkotlin-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodecocodes%2Fkotlin-style-guide/lists"}