Muhammad Talha
04/26/2022, 6:18 AMephemient
04/26/2022, 6:32 AMinline fun for higher-order functions for reified types, only static and extension functions work because they must be resolved statically
• for effectively final functions, they don't go through virtual function resolution anyway, so pick whichever organizes the code bestRuckus
04/26/2022, 7:11 AMRob Elliot
04/26/2022, 8:28 AMval x: T? = foo?.bar() is a lot better than val x: T? = foo?.run { bar(it) }, or val x: T? = if (foo != null) bar(foo) else null or making bar accept a nullable parameter.
This is particularly true with chaining - x?.foo()?.bar() where foo and bar are both extension functions on a non-null receiver can save you a lot of null types.
• Even within a codebase there are places where subject-verb-object is a desirable word order (or subject-verb) but where the verb and/or the object are not appropriate to the core domain of the subject.
e.g. you might have a data class representing an entity. Your data class should not know how to persist itself, but it's quite nice to be able to call entity.save(). save() can then be an extension function on Entity which has access to your repository. Of course you could just call save(entity) but it's surprising how much more legible it can be changing the order, and it can reduce your nesting quite a bit too.Rob Elliot
04/26/2022, 8:34 AMval status: HttpStatus = statusStr.toInt().toHttpStatus() than val status: HttpStatus = HttpStatus(Integer.parseInt(statusStr)). The former lets me read left to right - I start with a String, turn it into an Int, turn that into an HttpStatus. The latter I have to hunt to the end for the input and expand out through two layers of nesting.Rob Elliot
04/26/2022, 8:37 AMval status: HttpStatus? = statusStr?.toIntOrNull()?.toHttpStatus() which is a lot easier than handling null without the extension functions.Muhammad Talha
04/26/2022, 6:45 PM