Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Arithmetic with measures on dynamically typed object oriented languages

Presentation at OOSPLA 2005

  • Login to see the comments

  • Be the first to like this

Arithmetic with measures on dynamically typed object oriented languages

  1. 1. Arithmetic with Measures on Dynamically Typed OO Languages Hernán Wilkinson* Mercap Development Manager Tacuarí 202, 7mo Piso C1071AAF, Buenos Aires, Argentina 54-11-4878-1118 (ext. 120) h.wilkinson@mercapsoftware.com Máximo Prieto** Lifia – Facultad de Informática Universidad Nacional de La Plata cc11, 1900, La Plata, Argentina +54 221 422-8252 (ext. 215) maximo.prieto@lifia.info.unlp.edu.ar Luciano Romeo Mercap Software Architect Tacuarí 202, 7mo Piso C1071AAF, Buenos Aires, Argentina 54-11-4878-1118 l.romeo@mercapsoftware.com * Also Universidad de Buenos Aires, UBA, Argentina ** Also Universidad Nacional de la Patagonia Austral, Unidad Académica Caleta Olivia (UNPA-UACO)
  2. 2. Presentation of the Problem  Common mistakes in Financial Software  Adding amounts of different currencies  Applying interest rates with different time units  Confusing Nominal and Residual quantities  Mars Climate Orbiter  Confused English and Metric Units  Star Wars laser-beam experiment  Confused Miles with Feet
  3. 3. Why?  We are NOT used to creating the right ABSTRACTIONS  We are accustomed to using “primitive objects”  Java/.Net still lacks a Fraction class!  Java/.Net do not have a “DayOfMonth” class!  Therefore, we have:  Duplicated Code all over  Maintenance Problems  Communication and Design Problems
  4. 4. Motivating Example  Simple Interest Investment Formula finalCapital:=initialCapital+(initialCapital*interestRate*investmentTime)  Algorithm to invest 100 dollars just with Numbers:  Interest Rate: 10% per Year  Investment Time: 6 Months initialCapital := 100. interestRate := 0.1. investmentTime := 6. finalCapital:=initialCapital+(initialCapital*interestRate*investmentTime) = 100 +( 100 * 0.1 * 6 ) = 160  Is it right?
  5. 5. Motivating Example  Investing 100 dollars just with Measures:  Interest Rate: 10% per Year  Investment Time: 6 Months initialCapital := 100 dollars. interestRate := 0.1 / 1 year. investmentTime := 6 months. finalCapital:=initialCapital+(initialCapital*interestRate*investmentTime) = 100 dollars +( 100 dollars *0.1 / 1 year* 6 months ) = 105 dollars We got a Measure!!! Not only a Number!!!
  6. 6. What have we done?  We created new abstractions!  Our System has objects to represent 100 dollars, just like in real life  There are objects to represent 1 year and 6 months, JLIRL  We let those abstractions fulfill their responsibilities  We enriched our “Programming Language” with new “words”
  7. 7. Why do we need to do it?  Numbers are too generic  2 ¿What does it mean?  Units give numbers a particular meaning  2 dollars, 2 months, 2 miles  Operations with numbers are context free  2 + 3 = 5  Operations with measures are context aware  3 days + 1 week = 10 days  2 dollars + 3 euros = ?... It depends
  8. 8. What do we want?  Do arithmetic on Measures like in physics  The Measures to take care of unit  Simplification  Validation  Conversion  We want to relieve the programmer from those responsibilities
  9. 9. How did we do it?  Definition by National Institute of Standards and Technology (NIST)  “The value of a measure is its magnitude expressed as the product of a number and a unit, and the number multiplying the unit is the numerical value of the quantity expressed in that unit.”  We already have numbers…  We need Units and we need to make them collaborate with numbers to have Measures  So …
  10. 10. Units
  11. 11. Measures
  12. 12. Behavior  Arithmetic operations  1 year + 6 months  18 months  5 days + 3 weeks  26 days  3 miles * 4  12 miles  Comparison  3 miles < 1 kilometer  false  Conversion  1 day convertTo: hour  24 hours
  13. 13. Dynamically Typed Languages  Since  Variables are not statically typed then…  We did not need to create a common interface or superclass  We could reuse exactly the same code for existing arithmetic expressions  Smalltalk is designed for supporting unexpected behavior so…  We did not change the Number implementation  Therefore  We did not change a single line of code  We just added new functionality
  14. 14. Other Languages  Java:  + is not a message, it is hardcoded in the VM, numbers are not objects  It is statically typed  .Net:  operator + (Integer operand)  Statically typed  Can not be changed
  15. 15. Compound Units
  16. 16. Measure Bags
  17. 17. Singularities of the Measure Domain  Equality  24 hours = 24 hours  true  24 hours = 1 day  true  Immutability  Just like numbers  Zero  0 miles = 0 miles  0 miles = 0 feet  0 miles = 0 dollars ?  But, by definition: 0 A = 0 B   0 miles = 0 dollars = 0 kelvin = 0
  18. 18. Financial Measures  Interest Rate:  0.1 / 1 year – We can do it better!!  (InterestRate yearlyOf: 10 %) value  0.1 / 1 year  Price:  10 dollars/2 apples – We can do it better!!  (Price of: 2 apples is: 10 dollars) value  10 dollars/2 apples  Abstraction for Valuations!
  19. 19. Conclusions  Generic Measure Model that:  Add vocabulary from the Problem Domain  Supports Arithmetic Operations  Encapsulates Unit Conversion  A Better Programming Language  We believe this model should be part of any programming environment
  20. 20. Future Work  To reify the measure dimensions (distance, force, etc.)  Named composed units  Joule equivalent to m2 *Kg*s-2  Pascal equivalent to m-1 *Kg*s-2  To have units understand some messages to facilitate the creation of exponential units  meter pow: 2 to represent square meter.  To restrict the amount of some measures to be valid
  21. 21. Questions

×