code review

Alister alister.ware at ntlworld.com
Sat Jun 30 05:22:23 EDT 2012


On Sat, 30 Jun 2012 02:28:52 +0000, Steven D'Aprano wrote:

> On Fri, 29 Jun 2012 19:41:11 +0000, Alister wrote:
> 
>> also this section in main strikes me as a bit odd and convoluted
>> 
>>     w = world()
>>     serv = server(client)
>>     w.server = serv serv.world = w
>> 
>> I think you are cross referencing classes & would be better to
>> investigate inheritance.
> 
> What you show above is composition, and is a perfectly valid technique,
> and in fact should often be *preferred* to inheritance.
> 
> The only slightly dubious part, and I stress *slightly*, is that there
> is a circular reference: w refers to serv, and serv refers back to w.
> While legitimate, it is a very slight "code smell" that should be
> investigated.
> If there is a way to get the same result without the circular reference,
> that would be preferred.
> 
> For example, perhaps server methods that need to know the world could
> take it as an explicit argument, rather than fetching it implicitly from
> server.world.
> 
> Or, a moderately advanced technique, use a weak-ref instead.
> 
> Inheritance should only be used to model "is-a" relationships. For
> example, Herbie the Love Bug is a Volkswagen Beetle, so inheritance is
> appropriate.
> 
> http://en.wikipedia.org/wiki/Herbie
> 
> 
> class Vehicle(object):
>     pass
> 
> class Car(Vehicle):
>     pass
> 
> class Beetle(Car):
>     pass
> 
> herbie = Beetle()
> 
> Composition should be used to model "has-a" relationships. For example,
> a car has an engine, it is not a kind of engine, and so inheritance is
> inappropriate and composition should be used. I would re-write the Car
> class as follows:
> 
> class Engine(object):
>     pass
> 
> class Car(Vehicle):
>     def __init__(self):
>         self.engine = Engine()
> 
> So now we can talk about Herbie's engine:
> 
> herbie.engine  # Herbie, being a car, has an engine, he is not an engine

I probably was not to clear (due to my own inexperience) it was the 
circular references that grabbed my attention, it may be OK but suggests 
to me there may be a better approach.




-- 
((lambda (foo) (bar foo)) (baz))



More information about the Python-list mailing list