Chronology Current Month Current Thread Current Date
[Year List] [Month List (current year)] [Date Index] [Thread Index] [Thread Prev] [Thread Next] [Date Prev] [Date Next]

Re: [Phys-l] modern programming



On 07/05/2011 03:09 AM, Brian Blais wrote:
Then I saw what was, in my mind, the simplest example that showed a
clear advantage. It is a class, from the Python Cookbook, which does
temperature conversions
(http://code.activestate.com/recipes/286226-temperature-class/).

I'm not convinced by that example. In particular, the idea of
converting everything to a standard form and converting it back
again (if/when necessary) is a good idea _whether or not_ you are
using classes, objects, et cetera.

This is a long-established widely-practiced strategy: 2N converters,
used in pairs, give you the full N^2 capability.

The advantage is that, in this implementation, you only have to know
how to convert to and from Kelvin from whatever units you're using.
In a procedural program (like C), you'd have to have all of the
different combinations (c to f, c to r, r to f, etc...) which is
clearly less efficient.

Au contraire, would be as easy as π to implement the converters
in plain old C ... converters that can be used efficiently and
conveniently:

result = c_of_k(k_of_f(-40)); /* works fine */

This uses the aforementioned strategy.

The temperature-class-recipe is OK as a pedagogical illustration of
language features. Pedagogical examples are often a bit contrived.

=============

I added several more sections (and several more code samples) to:
http://www.av8n.com/computer/htm/modern-programming.htm
including some not-very-detailed remarks about STL container classes,
overloading, templates, call by reference, and exceptions.