My name is Viacheslav Aksenov. I am a professional Java/Kotlin backend developer in one of the largest Russian Fintech companies. I am responsible for designing and developing microservices for internal employees.In this article, I want to explain how to use the Kotlin extension functions the right way.Extension functions in Kotlin allow you to natively implement the "decorator" pattern.
private static final String getFormattedAmount { return "Account " + $this$getFormattedAmount.getId + " stores " + $this$getFormattedAmount.getAmount; } In this way, there is an extension with a signature that tells everybody from outside that this function just converts some class to another. But inside there is a lot of calls third-party APIs. It can be ambiguous for anybody who reads the code where this extension is using.
data class OwnClass { // public method is good! fun someMethod: String { // some logic } } // extension is very bad! fun OwnClass.someMethod: String=//some logicfun OwnClass.someMethod: { val metaField1=thirdPartyClient.getMetaField1 val metaField2=thirdPartyClient.getMetaField2 val metaField2=thirdPartyClient.getMetaField2 if { val metaField=.... // ... } // ... }
United States Latest News, United States Headlines
Similar News:You can also read news stories similar to this one that we have collected from other news sources.
Source: PhoneArena - 🏆 322. / 59 Read more »
Source: HuffPostWomen - 🏆 27. / 68 Read more »
Source: hackernoon - 🏆 532. / 51 Read more »
Source: Carscoop - 🏆 306. / 63 Read more »
Source: TODAYshow - 🏆 389. / 55 Read more »
Source: BestLifeOnline - 🏆 533. / 51 Read more »