├── LICENSE ├── README.md └── arts └── AndroidStudio.jpg /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin style guide of FRESH! 2 | 3 | ## Vision 4 | * Write Kotlin like code. (Lamda, Collections, Extension functions) 5 | - Use lambda expression 6 | - Use extension functions (i.g. scope functions) 7 | 8 | * Write shorter 9 | - Code that is difficult to read for shortening doesn't mean anything 10 | - Bad code if you need to think 11 | 12 | ## Rule 13 | 14 | * Use a formatter in `/.idea/codeStyleSttings.xml` 15 | * Start a new line at right vertica line on android studio. (about 130 characters) 16 | 17 | ### Coding rules 18 | 19 | * bad 20 | not recommend 21 | * not good 22 | No problem in Kotlin but it is not coding rules in FRESH! 23 | * good 24 | recommend in FRESH! 25 | 26 | Both `bad`, `not good` and `good` are correct in Kotlin. 27 | These definitions are on coding rules. 28 | 29 | ## Idiom 30 | 31 | ### Use an IDE suggestion 32 | 33 | ```Kotlin 34 | val array = ArrayList() 35 | 36 | // bad 37 | array.get(0) 38 | 39 | // good 40 | array[0] 41 | 42 | 43 | // bad 44 | getActivity().finish() 45 | 46 | // good 47 | activity.finish() 48 | ``` 49 | 50 | ### Use a smart cast 51 | 52 | ```Kotlin 53 | fun hoge(value: Boolean?) { 54 | value ?: return 55 | if (value) { 56 | 57 | } 58 | } 59 | 60 | fun hoge(context: Context) { 61 | context as Activity 62 | context.finish() // finish activity 63 | } 64 | 65 | // bad 66 | if (hoge !is Hoge) { 67 | throw IllegalArgumentException("not Hoge!") 68 | } 69 | hoge.foo() 70 | 71 | // good 72 | hoge as? Hoge ?: throw IllegalArgumentException("not Hoge!") 73 | hoge.foo() 74 | ``` 75 | 76 | ### Line break 77 | 78 | * Start a new line at right vertical line on Android studio. (About 130 characters) 79 | 80 | ![RightVerticalLine][AndroidStudio] 81 | 82 | * Start a new line by a symbol which are `,`, `:`, `{`, `=` in case characters over vertical line. 83 | 84 | ```Kotlin 85 | fun hoge(aaa: Int, bbb: Int, ccc: Int, ddd: Int, eee: Int, 86 | fff: Int, ggg: Int) { 87 | } 88 | 89 | fun hoge(aaa: Int, bbb: Int, ccc: Int, ddd: Int, eee: Int) = 90 | hoge().apply { 91 | } 92 | 93 | data class Hoge(private val aaa: Int, private val bbb: Int) : 94 | AbstractHoge() { 95 | } 96 | ``` 97 | 98 | ### `it` in Lambda expression applies to wide scope. 99 | 100 | ```Kotlin 101 | // bad 102 | { hoge -> 103 | hoge?.let { it.foo() } 104 | } 105 | 106 | // good 107 | { 108 | it?.let { hoge -> hoge.foo() } 109 | } 110 | ``` 111 | 112 | ### Should use a type interface 113 | 114 | You can write a type if it is difficult to understand. 115 | 116 | * property 117 | 118 | ```Kotlin 119 | val hoge = 0 // Int 120 | val foo = 10L // Long 121 | val bar = 100f //Float 122 | ``` 123 | 124 | * function 125 | 126 | ```Kotlin 127 | // return Boolean 128 | fun Context.isConnectToWifi() = 129 | (getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) 130 | .activeNetworkInfo?.type == ConnectivityManager.TYPE_WIFI 131 | 132 | // return Point 133 | fun Display.getSize(): Point = Point().apply { getSize(this) } 134 | ``` 135 | 136 | ### Don't write for loop 137 | 138 | You don't have to write for loop because threre is `forEach` in collections package of Kotlin. 139 | 140 | ```Kotlin 141 | // not good 142 | for (i in 0..9) { 143 | } 144 | 145 | // good 146 | (0..9).forEach { 147 | } 148 | 149 | // good (if you want to know index) 150 | (0..9).forEachIndexed { index, value -> 151 | } 152 | ``` 153 | 154 | ### Use `to` expression creating `Pair` class 155 | 156 | ```Kotlin 157 | // bad 158 | val pair = Pair(foo, bar) 159 | 160 | // good 161 | val pair = foo to bar 162 | ``` 163 | 164 | * `to` is infix function 165 | 166 | `public infix fun A.to(that: B): Pair = Pair(this, that)` 167 | 168 | ### Use `range` 169 | 170 | ```Kotlin 171 | val char = 'K' 172 | 173 | // bad 174 | if (char >= 'A' && 'c' <= 'Z') print("Hit!") 175 | 176 | // good 177 | if (char in 'A'..'Z') print("Hit!") 178 | 179 | when (char) { 180 | in 'A'..'Z' -> print("Hit!") 181 | else -> return 182 | } 183 | ``` 184 | 185 | ### `when` 186 | 187 | #### Use `when` in case there are two or more branches of if-else 188 | 189 | ```Kotlin 190 | // bad 191 | val hoge = 10if (hoge > 10) { 192 | 193 | } else if (hoge > 5) { 194 | 195 | } else if (hoge > 0) { 196 | 197 | } else { 198 | 199 | } 200 | 201 | // good 202 | when { 203 | hoge > 10 -> print("10") 204 | hoge > 5 -> print("0") 205 | hoge > 0 -> print("0") 206 | else -> print("else") 207 | } 208 | ``` 209 | 210 | #### Use `is` in case of comparing a class type 211 | 212 | ```Kotlin 213 | val hoge: Hoge = Hoge() 214 | when (hoge) { 215 | is Hoge -> { 216 | 217 | } 218 | else -> { 219 | 220 | } 221 | } 222 | ``` 223 | 224 | #### Use `range` in case comparing Int values 225 | 226 | ```Kotlin 227 | val hoge = 10 228 | when (hoge) { 229 | in 0..4 -> print("0..4") 230 | in 5..10 -> print("5..10") 231 | } 232 | ``` 233 | 234 | ### Don't start a new line in variable declaration using if-else 235 | 236 | ```Kotlin 237 | val foo: Int = 5 238 | 239 | // bad 240 | val bar = if (foo > 10) { 241 | "kotlin" 242 | } else { 243 | "java" 244 | } 245 | 246 | // good 247 | val bar = if(foo > 10) "kotlin" else "java" 248 | ``` 249 | 250 | ## Null 251 | 252 | ### Don't use `!!` 253 | 254 | Don't use `!!`, it will erase the benefits of Kotlin. 255 | You use it only when you want to explicitly raise a Null Pointer Exception. 256 | 257 | ### Use a scope function in case of checking a null value 258 | 259 | ```Kotlin 260 | class Hoge { 261 | fun fun1() {} 262 | fun fun2() {} 263 | fun fun3() = true 264 | } 265 | 266 | var hoge: Hoge? = null 267 | 268 | // not good 269 | if (hoge != null) { 270 | hoge.fun1() 271 | } else { 272 | val hoge = Hoge() 273 | hoge.fun1() 274 | hoge.fun2() 275 | } 276 | 277 | // good 278 | hoge?.run { 279 | fun1() 280 | } ?: run { 281 | hoge = Hoge().apply { 282 | fun1() 283 | fun2() 284 | } 285 | } 286 | 287 | // good 288 | if (hoge != null && hoge.fun3()) { 289 | hoge.fun1() 290 | } else { 291 | hoge = Hoge().apply { 292 | fun1() 293 | fun2() 294 | } 295 | } 296 | ``` 297 | 298 | ## Property 299 | 300 | ### Property rules 301 | 302 | * Use non-null value as much as possible 303 | * Don't use a m-prefix 304 | * Property priority 305 | - non-null & val 306 | - non-null & var 307 | - nullable & var 308 | 309 | Use a `lateinit` or `Delegates.notNull()` if you cannot set a initial value. 310 | `lateinit` is better than `Delegates.notNull()` because `Delegates.notNull()` uses reflection. 311 | But primitive values is not applied to `lateinit`. 312 | 313 | ```Kotlin 314 | // non-null & val 315 | private val hoge: Hoge = Hoge() 316 | private val drawablePadding: Int by lazy { activity.resources.getDimensionPixelSize(R.dimen.drawable_padding) } 317 | 318 | // non-null & var 319 | private lateinit var hoge: Hoge 320 | private var hoge: Hoge = Delegates.notNull() 321 | 322 | // nullable & var 323 | private var hoge: Hoge? = null 324 | ``` 325 | 326 | ### Constant 327 | 328 | Use `const val` for `static final` in Java. 329 | But primitive values is not applied to `const`. 330 | 331 | Write a `companion object` block under class declaration. 332 | 333 | 334 | ## Scope function 335 | 336 | ### Should use scope function 337 | 338 | ```Kotlin 339 | class Hoge { 340 | fun fun1() {} 341 | fun fun2() {} 342 | } 343 | 344 | // bad 345 | val hoge = Hoge() 346 | hoge.fun1() 347 | hoge.fun2() 348 | 349 | // good 350 | val hoge = Hoge().apply { 351 | fun1() 352 | fun2() 353 | } 354 | ``` 355 | 356 | You don't need to use `with` because you can substitute `run`, `let`, `apply`, `also`. 357 | 358 | ### Don't declare variables that used only once 359 | 360 | You can express using a scope function. 361 | But it becomes hard to read in case it excessibely use. 362 | You should declare variables if you need to think. 363 | 364 | ```Kotlin 365 | class Foo 366 | 367 | class Bar(val foo: Foo) 368 | 369 | class Hoge { 370 | fun fun1() {} 371 | fun fun2(bar: Bar) {} 372 | fun fun3(foo: Foo) {} 373 | } 374 | 375 | // bad 376 | val hoge = Hoge() 377 | val foo = Foo() 378 | val bar = Bar(foo) 379 | hoge.fun1() 380 | hoge.fun2(bar) 381 | hoge.fun3(foo) 382 | 383 | // good 384 | Hoge().run { 385 | fun1() 386 | Foo().let { 387 | fun2(Bar(it)) 388 | fun3(it) 389 | } 390 | } 391 | ``` 392 | 393 | ### which use `run` or `let` 394 | 395 | * Use `let` to substitute into functions 396 | * Use `run` to use outside functions 397 | 398 | ```Kotlin 399 | class Foo 400 | 401 | class Bar(val foo: Foo) 402 | 403 | class Hoge { 404 | fun fun1() {} 405 | fun fun2(bar: Bar) {} 406 | fun fun3(foo: Foo) {} 407 | } 408 | 409 | // not good 410 | Hoge().let { 411 | it.fun1() 412 | Foo().run { 413 | it.fun2(Bar(this)) 414 | it.fun3(this) 415 | } 416 | } 417 | 418 | // good 419 | Hoge().run { 420 | fun1() 421 | Foo().let { 422 | fun2(Bar(it)) 423 | fun3(it) 424 | } 425 | } 426 | ``` 427 | 428 | ## Function 429 | 430 | ### Omit a type of return value 431 | 432 | Basically, we omit a type of return value as declaration variables using type interface. 433 | You should write it in case we cannot guess from a method name. 434 | 435 | ```Kotlin 436 | fun createIntent(context: Context, foo: Int, bar: Boolean) = 437 | Intent(context, HogeActivity::class.java).apply { 438 | putExtra("foo", foo) 439 | putExtra("bar", bar) 440 | } 441 | 442 | fun hoge(value: Int): String = when (value) { 443 | in 0..10 -> "foo" 444 | in 100..500 -> "bar" 445 | else -> "else" 446 | } 447 | ``` 448 | 449 | 450 | ### Don't name `setHoge`, `getHoge` 451 | 452 | Don't name `setHoge`, `getHoge` at property and function because it is used in Kotlin language. 453 | 454 | ```Kotlin 455 | // bad 456 | fun setHoge() { 457 | } 458 | ``` 459 | 460 | ### Use named arguments for function overloading 461 | 462 | ```Kotlin 463 | // bad 464 | class Hoge { 465 | fun hoge() { 466 | print("hoge") 467 | } 468 | 469 | fun hoge(prefix: String) { 470 | print(prefix + "hoge") 471 | } 472 | } 473 | 474 | // good 475 | class Hoge { 476 | fun hoge(prefix: String = "") { 477 | print(prefix + "hoge") 478 | } 479 | } 480 | ``` 481 | 482 | ### Use typealias 483 | 484 | ```Kotlin 485 | // bad 486 | interface CallBackListener { 487 | fun onHoge(foo: String, bar: Int) 488 | } 489 | 490 | // caller 491 | var callback: CallBackListener? = null 492 | callback?.onHoge("foo", 100) 493 | 494 | // callee 495 | val callback = object : CallBackListener { 496 | override fun onHoge(foo: String, bar: Int) { 497 | print("$foo : $bar") 498 | } 499 | } 500 | 501 | // good 502 | typealias CallBackListener = (foo: String, bar: Int) -> Unit 503 | 504 | // caller 505 | var callback: CallBackListener? = null 506 | callback?.invoke("foo", 100) 507 | 508 | // callee 509 | val callback = { foo, bar -> 510 | print("$foo : $bar") 511 | } 512 | ``` 513 | 514 | ### Extension functions 515 | 516 | * Use it as utility functions in Java 517 | * It is not necessary to use extension functions in case it is defined wide scope. 518 | i.g. String 519 | In case we recommend the private extension functions. 520 | 521 | * Extension class name : `HogeExt.kt` 522 | 523 | * Structure of package 524 | 525 | ``` 526 | main 527 | |-data 528 | |-ui 529 | |-util 530 | |-ext 531 | |-util 532 | |-ext 533 | ``` 534 | 535 | ### Private extension functions 536 | 537 | Functions taking only one object as an argument replace private extension functions. (not necessary) 538 | 539 | ```Kotlin 540 | enum class Hoge() { 541 | FOO, 542 | BAR, 543 | NONE 544 | } 545 | 546 | // not good 547 | fun toHoge(arg: String): Hoge { 548 | if (!arg.startsWith("hoge")) { 549 | return Hoge.NONE 550 | } 551 | return when (arg) { 552 | "hogeFoo" -> Hoge.FOO 553 | "hogeBar" -> Hoge.BAR 554 | else -> Hoge.NONE 555 | } 556 | } 557 | 558 | // good 559 | fun String.toHoge(): Hoge { 560 | if (!startsWith("hoge")) { 561 | return Hoge.NONE 562 | } 563 | return when (this) { 564 | "hogeFoo" -> Hoge.FOO 565 | "hogeBar" -> Hoge.BAR 566 | else -> Hoge.NONE } 567 | } 568 | } 569 | ``` 570 | 571 | ### Don't create extension functions in same class. 572 | 573 | ```Kotlin 574 | class Hoge { 575 | 576 | // bad 577 | fun Hoge.foo() { 578 | } 579 | 580 | // good 581 | fun foo() { 582 | } 583 | } 584 | ``` 585 | 586 | 587 | ## Others 588 | 589 | ### Rx 590 | 591 | ```Kotlin 592 | // bad 593 | Observable.just(10) 594 | .map { 595 | it * 3 596 | } 597 | .filter { 598 | val rand = Random() 599 | it % rand.nextInt(10) == 0 600 | } 601 | .map { it.toFloat() } 602 | .withLatestFrom(Observable.just("hoge"), BiFunction { num: Float, hoge: String -> num to hoge }) 603 | .subscribe ({ print(it) }, { print(it) }) 604 | 605 | // bad 606 | // You must start a new line as operators 607 | Observable.just(10).map { it * 3 } 608 | .filter { 609 | val rand = Random() 610 | it % rand.nextInt(10) == 0 611 | }.map { it.toFloat() } 612 | .withLatestFrom(Observable.just("hoge"), 613 | BiFunction { num: Float, hoge: String -> num to hoge }) 614 | .subscribe ({ 615 | print(it) 616 | }, { 617 | print(it) 618 | }) 619 | 620 | // good 621 | Observable.just(10) 622 | .map { it * 3 } // one line 623 | .filter { // line break in case of multiple lines 624 | val rand = Random() 625 | it % rand.nextInt(10) == 0 626 | } 627 | .map(Int::toFloat) // method reference 628 | .withLatestFrom(Observable.just("hoge")) { num, hoge -> num to hoge } // Use rx-kotlin 629 | .subscribe ({ // start a new line in subscribe block if it implements `onError` 630 | print(it) 631 | }, { 632 | print(it) 633 | }) 634 | ``` 635 | 636 | # License 637 | 638 | ``` 639 | Copyright (C) 2017 CyberAgent, Inc. 640 | 641 | Licensed under the Apache License, Version 2.0 (the "License"); 642 | you may not use this file except in compliance with the License. 643 | You may obtain a copy of the License at 644 | 645 | http://www.apache.org/licenses/LICENSE-2.0 646 | 647 | Unless required by applicable law or agreed to in writing, software 648 | distributed under the License is distributed on an "AS IS" BASIS, 649 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 650 | See the License for the specific language governing permissions and 651 | limitations under the License. 652 | ``` 653 | 654 | [AndroidStudio]: /arts/AndroidStudio.jpg 655 | -------------------------------------------------------------------------------- /arts/AndroidStudio.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openfresh/android-kotlin-style-guide/53395748d645f4b848572d5b21cfdbf9c5df07a3/arts/AndroidStudio.jpg --------------------------------------------------------------------------------