Using Kotlin Extension Functions: The Good, the Bad, and the Ugly | HackerNoon

  • 📰 hackernoon
  • ⏱ Reading Time:
  • 42 sec. here
  • 2 min. at publisher
  • 📊 Quality Score:
  • News: 20%
  • Publisher: 51%

United States Headlines News

United States Latest News,United States Headlines

'Using Kotlin Extension Functions: The Good, the Bad, and the Ugly' kotlin kotlinextensions

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=.... // ... } // ... }

 

Thank you for your comment. Your comment will be published after being reviewed.
Please try again later.
We have summarized this news so that you can read it quickly. If you are interested in the news, you can read the full text here. Read more:

 /  🏆 532. in US

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.

China goes beyond 5G as it sets record for data streaming using '6G' technologyChina continues to grab the early lead in 6G as it sets a new record for streaming speed using vortex millimetre. 😮😯😲6G 🤔🤔🤔...
Source: PhoneArena - 🏆 322. / 59 Read more »

India.Arie Reacts To Joe Rogan's Apology For Repeatedly Using The N-WordThe singer, who had posted a video compilation of the podcaster using the slur, said her message to Spotify was not about 'censorship.'
Source: HuffPostWomen - 🏆 27. / 68 Read more »

3 Successful Examples of Using an MVP (Minimum Viable Product) | HackerNoonA concept first introduced on the Lean Startup book, MVP means minimum viable product and it is a way to get the most knowledge about the target of your project
Source: hackernoon - 🏆 532. / 51 Read more »

A Growing Number Of Carmakers Are Using Lidar Sensors, Tesla Ain't One Of Them | CarscoopsA Growing Number Of Carmakers Are Using Lidar Sensors, Tesla Ain't One Of Them | Carscoops carscoops ⬇️
Source: Carscoop - 🏆 306. / 63 Read more »

10 exceptional people who are using Black joy as a form of resistanceBlack joy is about 'manifesting the joy that you need, deserve, or desire,' says Kleaver Cruz of The Black Joy Project.
Source: TODAYshow - 🏆 389. / 55 Read more »

If You Bought a Geek Heat Heater at Walmart or Target, Stop Using ItGeek Heat personal heaters sold online by Walmart, Target, and Amazon are being recalled due to a potential fire hazard they pose.
Source: BestLifeOnline - 🏆 533. / 51 Read more »