[Tutor] Code critique please (OOP strategy)

Sean 'Shaleh' Perry shalehperry@attbi.com
Mon, 31 Dec 2001 10:03:36 -0800 (PST)


On 31-Dec-2001 Timothy Wilson wrote:
> 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?
> 

yes, you should.  More choices that way.

>> 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 should document this fact.

>> 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.
> 

class methods should not prompt.  You have the right idea in the current code. 
If you follow the example with the dictionary in my code you can clean main up
quite a bit.