Java to Kotlin by Duncan McGregor
Author:Duncan McGregor
Language: eng
Format: epub
Publisher: O'Reilly Media
Published: 2021-08-20T00:00:00+00:00
Calculate per-currency totals, so the calculation doesnât accumulate rounding errors.
Convert costs to the travelerâs preferred currency.
Calculate the grand total in the travelerâs preferred currency.
Sort the currency conversions in alphabetical order of the original currency code.
Store the currency conversions and grand total so they can be displayed to the traveler.
Such smearing of responsibilities across classes is common when we compute by mutating shared state. Weâd like to disentangle the responsibilities and simplify the implementation. What final structure we should we aim for?
One clue is in the name of the CostCurrencyCalculator class. In linguistic jargon, the CostCurrencyCalculator is an agent noun: a noun derived from a verb that means no more than a thing that performs the action identified by the verb, like driver or baker or calculator. CostCurrencyCalculator is a so-called âdoer class.â
Another clue is in the data that the class holds. The travelerâs preferred currency and source of exchange rates are the context for the calculation. They are managed elsewhere in the application and held by CostCurrencyCalculator so that they are close at hand for its calculations. The map of totals by currency (currencyTotals) contains transient, intermediate results of the calculation that are irrelevant after the calculation is complete and, in fact, should be discarded to avoid aliasing errors. The class doesnât own any data, only holds it temporarily for operational reasons.
The CostCurrencyCalculator class doesnât represent a concept in our application domain model, but a function that we perform upon elements of that domain model. In Kotlin, we usually implement functions not with objects but with, well, functions.
Letâs refactor the calculation from mutable classes to functions that work with immutable data.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9412)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9375)
The Mikado Method by Ola Ellnestam Daniel Brolund(9342)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7883)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7429)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7388)
Grails in Action by Glen Smith Peter Ledbrook(7321)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7150)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5994)
Kotlin in Action by Dmitry Jemerov(4674)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(3967)
Functional Programming in JavaScript by Mantyla Dan(3735)
Windows APT Warfare by Sheng-Hao Ma(2989)
Learning Java by Patrick Niemeyer & Daniel Leuck(2881)
Mastering Python for Finance by Weiming James Ma;(2867)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2858)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(2683)
WordPress Plugin Development Cookbook by Yannick Lefebvre(2660)
Mastering Java 9 by Dr. Edward Lavieri(2583)