[Tutor] Creating class / OOP structure
Alan Gauld
alan.gauld at btinternet.com
Wed Nov 6 10:23:53 CET 2013
On 06/11/13 02:19, Steven D'Aprano wrote:
> Yes! The first thing to do is get rid of the unnecessary class. This is
> not Java where you have to write classes for everything. From the sample
> code that you show below, using OOP here accomplishes nothing except
> making the code more complicated and less efficient.
If that's true I agree, but I assumed that since he called it filedict
he was intending to make it into a filedict and only showed us the
methods that he was asking about (which is what we encourage people
to do...)
> class *needs* only behaviour -- the class doesn't need to store either
> self.fname nor self.parsed_file, since they are both only used once.
Its hard to say that if the OP does intend to implement a filedict. If
the dict is being written back to a file at some point it may need to
store the original name - or to mangle it and store the new target name...
> "Filedict" is neither a file nor a dict -- it doesn't inherit from file,
> or behave like a file; it doesn't inherit from dict, or behave like a
> dict. It is, in fact, *not* a file/dict at all.
Again, I assumed (possibly wrongly!) that tyhe intent ws to produce a
filedict (which sounds like it might be based on a shelve?).
> What you really have is something with a nasty compound name:
>
> FileParserAndDictProcessor
But if you are right and I'm wrong about the intent then I agree!
> When your classes have nasty compound names like this, it is a very
> strong clue that the class doesn't actually represent a thing and
> probably shouldn't exist....
> For a very entertaining -- although quite long -- look at this issue,
> have a read of this:
>
> http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html
I agreed with about half of this, but strongly disagreed with some of it
too. Much of it is Java focused and so I understand his frustrations,
but his comments are a bit to generalised IMHO.
In particular his list of verbs before nouns is completely misguided
since he uses the imperative case which in English has an implied
subject - the person carrying out the command. This his examples should
all be prefixed by a noun to become, for example:
YOU get the garbage bag from under the sink
YOU carry it out to the garage
YOU dump it in the garbage can
And the equivalent OOP constructs become
Person.get(item, location)
Person,put(item, location)
Person.dump(item,receptacle)
In fact in most English sentences the noun does come before the verb.
<Subject Verb Object> is the most common structure.
The bit of his rant thats valid, and often seen in Java, is that the
verbs get applied to the object of the sentence rather than the
subject. Thus we see
Garbage.get(location)
Which I agree makes no sense.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list