Put nested classes next to the code that uses those classes. Choose an order (either higher-level stuff first, or vice versa) and stick to it. Instead, put related stuff together, so that someone reading the class from top to bottom can follow the logic of what's happening. Property declarations and initializer blocksĭo not sort the method declarations alphabetically or by visibility, and do not separate regular methods from extension methods. The contents of a class should go in the following order: Avoid creating files just to hold all extensions of some class. When defining extension functions that make sense only for a specific client, put them next to the code of that client. In particular, when defining extension functions for a class which are relevant for all clients of this class, put them in the same file with the class itself. Placing multiple declarations (classes, top-level functions or properties) in the same Kotlin source file is encouraged as long as these declarations are closely related to each other semantically, and the file size remains reasonable (not exceeding a few hundred lines). However, this naming scheme can help you avoid situations when a simple refactoring or an addition could include a top-level function and result in the same "Duplicate JVM classes" error. On JVM, if source files don't have top-level declarations, the file facades aren't generated, and you won't face naming clashes. However, this naming scheme can help you keep file naming consistent. Non-JVM platforms don't have issues with duplicating file facades. There are two cases when these recommendations may seem redundant, but we still advise to follow them: This naming scheme helps avoid clashes while retaining code readability. The simplest way to avoid that is renaming one of the files according to the guideline above. This produces the "Duplicate JVM classes" error. Here both Platform.kt files are in the same package, so the Kotlin JVM compiler produces two file facades, both of which have FQN myPackage.PlatformKt. |- commonMain/kotlin/myPackage/Platform.kt // contains 'fun count() ' For example, commonMain/kotlin/Platform.kt. For example:Īs for the common source set, files with top-level declarations should not have a suffix. In multiplatform projects, files with top-level declarations in platform-specific source sets should have a suffix associated with the name of the source set. Therefore, you should avoid using meaningless words such as Util in file names. The name of the file should describe what the code in the file does. Use an upper camel case with an uppercase first letter (also known as Pascal case), for example, ProcessDeclarations.kt. If a file contains multiple classes, or only top-level declarations, choose a name describing what the file contains, and name the file accordingly. It applies to all types of classes and interfaces. If a Kotlin file contains a single class or interface (potentially with related top-level declarations), its name should be the same as the name of the class, with the. On JVM: In projects where Kotlin is used together with Java, Kotlin source files should reside in the same source root as the Java source files, and follow the same directory structure: each file should be stored in the directory corresponding to each package statement. For example, if all the code in the project is in the package and its subpackages, files with the package should be placed directly under the source root, and files in .socket should be in the network/socket subdirectory of the source root. In pure Kotlin projects, the recommended directory structure follows the package structure with the common root package omitted. Source code organization Directory structure Additional inspections that verify other issues described in the style guide (such as naming conventions) are enabled by default. Switch on Incorrect formatting inspection. Go to Settings/Preferences | Editor | Inspections | General. Verify that your code follows the style guide Go to Settings/Preferences | Editor | Code Style | Kotlin. You can configure them to automatically format your code in consistence with the given code style. Two most popular IDEs for Kotlin - IntelliJ IDEA and Android Studio provide powerful support for code styling. Here we provide guidelines on the code style and code organization for projects that use Kotlin. Commonly known and easy-to-follow coding conventions are vital for any programming language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |