[Tutor] Code critique please (OOP strategy)

Timothy Wilson wilson@visi.com
Mon, 31 Dec 2001 08:46:48 -0600 (CST)


On Mon, 31 Dec 2001, Sean 'Shaleh' Perry wrote:

> In my opinion, constructing a class should never cause the program to stop at a
> prompt.  This prevents later use.  If I wanted to make a list of portfolio's
> and then give them files to open, I see no way to do this.  In C++ parlance you
> have no default constructor.

You're talking about the Portfolio constructor, right? Should I create a
separate method to unpickle the data from disk?

> I am also curious as to why the prices are * 100 every time.  There is no
> comment in the code for this.

All the prices are stored in cents. My understanding is that a lot of
software that works with financial data keeps track of everything in
cents and converts to dollars when necessary. This should help eliminate
rounding errors.

> you could make a menu class or function, but all that would do is move the
> code out of main.

Here's an example:

The main() function contains the following code to delete a stock:

elif choice.upper() == 'D':
    ticker = raw_input("Delete which stock (type a ticker symbol): ")
    for stock in p.contents:
        if stock.ticker == ticker.upper():
            p.delStock(stock)
            print "Removed %s" % stock.ticker

The delStock method is:

def delStock(self, stock):
    self.contents.remove(stock)
    self.modified = 1

Is is correct to ask for the ticker symbol in the 'elif' or should I do
that in 'delStock'? My reason for doing it the way I did was to make the
code as reusable as possible. That may or may not be a valid reason in
this case. I'd appreciate feedback on this point.

-Tim

--
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com