SlideShare a Scribd company logo
Code Style Guidelines
By Sanjay Hans
Nomenclature
Declarations
Spacing
Semicolons
Brace Style
When Statements
Annotations
Types
Xml Guidance
Language
Credit
TOC
Nomenclature
Packages
Package names are similar to Java: all lower-case, multiple words concatenated
together, without hyphens or underscores:
org.example.kotlin
Classes & Interfaces
Names of classes and objects start with an upper case letter and use the
camel case:
class DeclarationProcessor
Function names
Names of functions, properties and local variables start with a lower case letter and use the
camel case and no underscores:
fun processDeclaration(){/*…*/}
var declarionCount = 1
Property names
Constant values in the companion object should be written in uppercase, with an
underscore separating words:
class const val MAX_COUNT = 8
val USER_NAME_FIELD = "UserName"
Variables & Parameters
Written in lowerCamelCase.
Single character values must be avoided, except for temporary looping variables.
Declarations
Visibility Modifiers
Only include visibility modifiers if you need something other than the default of public.
Fields & Variables
Prefer single declaration per line.
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.
Enum Classes
Enum classes without methods may be formatted without line-breaks, as follows:
private enum CompassDirection { EAST, NORTH, WEST, SOUTH }
Spacing
Indentation
Indentation is using spaces - never tabs.
Blocks
Indentation for blocks uses 2 spaces (not the default 4):
Line Wraps
Indentation for line wraps should use 4 spaces (not the default 8):
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.
Semicolons:
Semicolons are dead to us should be avoided wherever possible in Kotlin.
Brace Style
Unlike Java, direct access to fields in Kotlin is preferred.
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.
Annotations
Standard annotations should be used - in particular override.
This should appear on the same line as the function declaration.
Types
Always use Kotlin's native types when available.
Type Inference
Type inference should be preferred where possible to explicitly declared types.
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.
XML Guidance
Since Android uses XML extensively in addition to Java, we have some rules specific to XML.
XML File Names
View-based XML files should be prefixed with the type of view that they represent.
• activity_login.xml
• fragment_main_screen.xml
• button_rounded_edges.xml
Use Context-Specific XML Files
•Strings => res/values/strings.xml
•Styles => res/values/styles.xml
•Colors => res/color/colors.xml
•Animations => res/anim/
•Drawable => res/drawable
Language
Use US English spelling.
String color = "red";
Useful
resources
 https://kotlinlang.org/docs/reference/coding-
conventions.html
 https://developer.android.com/kotlin/style-guide
 https://source.android.com/setup/contribute/code-style
 https://github.com/raywenderlich/kotlin-style-guide
 https://jakewharton.com/just-say-no-to-hungarian-notation/
Q&A

More Related Content

PDF
Java Collection Framework
PPT
Java Presentation
PDF
PPTX
Data Types, Variables, and Constants in C# Programming
PDF
TypeScript: coding JavaScript without the pain
PPTX
Java Annotations
PDF
Java 8 features
PPS
Introduction to class in java
Java Collection Framework
Java Presentation
Data Types, Variables, and Constants in C# Programming
TypeScript: coding JavaScript without the pain
Java Annotations
Java 8 features
Introduction to class in java

What's hot (20)

PDF
Java data types, variables and jvm
PPTX
Sql Basics And Advanced
PPTX
Coding standards for java
PPT
JavaScript Arrays
PDF
Angular - Chapter 4 - Data and Event Handling
PPT
Xml parsers
PDF
Spring annotation
PPTX
PDF
Regular expression in javascript
ODP
Introduction to Java 8
PPTX
Java string handling
PPTX
Java 8 presentation
PPTX
Javascript functions
PPTX
Xml namespace
PDF
NodeJS for Beginner
PDF
Introduction to java (revised)
PPTX
Javascript 101
PPT
Chapter8 pl sql
PPTX
Coding conventions
PPTX
Java database connectivity with MySql
Java data types, variables and jvm
Sql Basics And Advanced
Coding standards for java
JavaScript Arrays
Angular - Chapter 4 - Data and Event Handling
Xml parsers
Spring annotation
Regular expression in javascript
Introduction to Java 8
Java string handling
Java 8 presentation
Javascript functions
Xml namespace
NodeJS for Beginner
Introduction to java (revised)
Javascript 101
Chapter8 pl sql
Coding conventions
Java database connectivity with MySql
Ad

Similar to Kotlin Code style guidelines (20)

PDF
Perfomatix - Android Coding Standards
PDF
Clean Code Style
PDF
9054799 dzone-refcard267-kotlin
PPTX
Kotlin: lo Swift di Android (2015)
PPTX
Kotlin - lo Swift di Android
PPTX
Android & Kotlin - The code awakens #03
PPTX
Android & Kotlin - The code awakens #01
PPTX
kotlin-nutshell.pptx
PPTX
Android Coding Standered
PPTX
Android coding guidlines
PDF
Kotlin Crash Course
PDF
Introduction to kotlin for Java Developer
PDF
1. Coding Conventions [Part 1]
PDF
Lightning talk: Kotlin
PDF
Summer of Tech 2017 - Kotlin/Android bootcamp
PDF
Deggan Android Codestyle v1.0
PPTX
Why kotlininandroid
PDF
Getting Started With Kotlin Development - Rivu
PDF
2017: Kotlin - now more than ever
Perfomatix - Android Coding Standards
Clean Code Style
9054799 dzone-refcard267-kotlin
Kotlin: lo Swift di Android (2015)
Kotlin - lo Swift di Android
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #01
kotlin-nutshell.pptx
Android Coding Standered
Android coding guidlines
Kotlin Crash Course
Introduction to kotlin for Java Developer
1. Coding Conventions [Part 1]
Lightning talk: Kotlin
Summer of Tech 2017 - Kotlin/Android bootcamp
Deggan Android Codestyle v1.0
Why kotlininandroid
Getting Started With Kotlin Development - Rivu
2017: Kotlin - now more than ever
Ad

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
top salesforce developer skills in 2025.pdf
PDF
Cost to Outsource Software Development in 2025
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Introduction to Artificial Intelligence
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
medical staffing services at VALiNTRY
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
System and Network Administraation Chapter 3
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Choose the Right IT Partner for Your Business in Malaysia
top salesforce developer skills in 2025.pdf
Cost to Outsource Software Development in 2025
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Computer Software and OS of computer science of grade 11.pptx
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Designing Intelligence for the Shop Floor.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Introduction to Artificial Intelligence
Softaken Excel to vCard Converter Software.pdf
medical staffing services at VALiNTRY
wealthsignaloriginal-com-DS-text-... (1).pdf
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Why Generative AI is the Future of Content, Code & Creativity?
System and Network Administraation Chapter 3

Kotlin Code style guidelines

  • 3. Nomenclature Packages Package names are similar to Java: all lower-case, multiple words concatenated together, without hyphens or underscores: org.example.kotlin Classes & Interfaces Names of classes and objects start with an upper case letter and use the camel case: class DeclarationProcessor Function names Names of functions, properties and local variables start with a lower case letter and use the camel case and no underscores: fun processDeclaration(){/*…*/} var declarionCount = 1
  • 4. Property names Constant values in the companion object should be written in uppercase, with an underscore separating words: class const val MAX_COUNT = 8 val USER_NAME_FIELD = "UserName" Variables & Parameters Written in lowerCamelCase. Single character values must be avoided, except for temporary looping variables.
  • 5. Declarations Visibility Modifiers Only include visibility modifiers if you need something other than the default of public. Fields & Variables Prefer single declaration per line. username: String twitterHandle: String Classes Exactly one class per source file, although inner classes are encouraged where scoping appropriate.
  • 6. Data Type Objects Prefer data classes for simple data holding objects. Enum Classes Enum classes without methods may be formatted without line-breaks, as follows: private enum CompassDirection { EAST, NORTH, WEST, SOUTH }
  • 7. Spacing Indentation Indentation is using spaces - never tabs. Blocks Indentation for blocks uses 2 spaces (not the default 4): Line Wraps Indentation for line wraps should use 4 spaces (not the default 8):
  • 8. 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. Semicolons: Semicolons are dead to us should be avoided wherever possible in Kotlin.
  • 9. Brace Style Unlike Java, direct access to fields in Kotlin is preferred.
  • 10. 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.
  • 11. Annotations Standard annotations should be used - in particular override. This should appear on the same line as the function declaration.
  • 12. Types Always use Kotlin's native types when available. Type Inference Type inference should be preferred where possible to explicitly declared types. 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.
  • 13. XML Guidance Since Android uses XML extensively in addition to Java, we have some rules specific to XML. XML File Names View-based XML files should be prefixed with the type of view that they represent. • activity_login.xml • fragment_main_screen.xml • button_rounded_edges.xml Use Context-Specific XML Files •Strings => res/values/strings.xml •Styles => res/values/styles.xml •Colors => res/color/colors.xml •Animations => res/anim/ •Drawable => res/drawable Language Use US English spelling. String color = "red";
  • 14. Useful resources  https://kotlinlang.org/docs/reference/coding- conventions.html  https://developer.android.com/kotlin/style-guide  https://source.android.com/setup/contribute/code-style  https://github.com/raywenderlich/kotlin-style-guide  https://jakewharton.com/just-say-no-to-hungarian-notation/
  • 15. Q&A

Editor's Notes