[program-l] Re: Python: Please Help Me Think Out Loud About When To Make Classes And When Not To Make Them

  • From: Soronel Haetir <soronel.haetir@xxxxxxxxx>
  • To: program-l@xxxxxxxxxxxxx
  • Date: Tue, 30 Oct 2012 11:09:21 -0800

Sure, if the data were both persistent and local to a single function
I might consider using a function static instead of making the
function a member and then working on member variables.

I think it would be easier to demonstrate with what I consider a good
example for _not_ having a class:
I have a utility library with a bunch of stuff I've found useful over
the years, one such function gets the program specific AppData
directory.
This functionality is specialized enough that it doesn't really belong
in the string class (even if I were in control of the string class
source which I'm not, I use standard implementations for that), even
though that is what it returns.  It also doesn't persist any data from
one call to another, nor does it share and persistent data with other
functions.  I consider this function to be an excellent example of
when not to make something a class member.

On 10/30/12, Homme, James <james.homme@xxxxxxxxxxxx> wrote:
> Hi,
> When you say "persistent state" are you talking about static data and
> functions?
>
> Thanks.
>
> Jim
>
>
>
> -----Original Message-----
> From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx]
> On Behalf Of Soronel Haetir
> Sent: Tuesday, October 30, 2012 2:52 PM
> To: program-l@xxxxxxxxxxxxx
> Subject: [program-l] Re: Python: Please Help Me Think Out Loud About When To
> Make Classes And When Not To Make Them
>
> The main item I use when deciding to make classes or not is whether there is
> persistent state that should travel with the function call(s).  If there is
> no persistent state then I see no reason to make a class.  It is also one of
> the things I don't care for about java and .net (I'm aware that the .net
> framework itself can handle global functions, but the languages I use with
> it cannot so it's pretty much a waste of effort, and I find static classes a
> rather silly
> substitute)
>
> This factor is not particular to python, instead it extends to pretty much
> every language I've used that gives the choice of whether to use classes or
> not.
>
> On 10/30/12, Homme, James <james.homme@xxxxxxxxxxxx> wrote:
>> Hi,
>> I think that my chess project is one in which it's OK to make the
>> class that uses the cmd class, but is also OK to import a file that
>> has chess related functions, but is not a class. If I'm wrong, please
>> tell me. Here's how I view the project in my mind right now, and where
>> it's going. But the big question in my mind is this. What makes you
>> either decide to make classes or not make them?
>>
>> I now have a file that I'm going to change so that it returns the
>> results of the functions it calls from my chess board module. In order
>> to make the functions in my command file see the ones I'm making in
>> the chess board file, I put an import statement at the top. When I did
>> this, my functions could call the ones in my chess board file.
>>
>> I'm going to have functions like new game. I think that this makes it
>> so that I don't need to instantiate a chess board class. I say this
>> because of the way this whole thing is constructed. Every time I type
>> a command, it gets ready to receive a new one.  I'm keeping the help
>> functions in my commands file, though. I'm not going to migrate them
>> over, or put functions in the chess board file that return help text.
>> I may have to change my mind, but this is how I see it working right now.
>>
>> The only thing my commands file will do is start and run the command
>> loop.
>>
>> I'm hoping that once I learn how to make GUI programs, I can simply
>> swap out my command line processing file and make the GUI file just
>> use the functions in the chess board file. So, in stead of a command
>> loop, it would make some sort of event loop or whatever that GUI thing
>> is called that waits for you to click stuff and choose stuff.
>>
>> So theoretically, I can put out a command line version of my program,
>> and a GUI version of it.
>>
>> But what concerns me is the question of whether I'm constructing it
>> this way because I come from a procedural programming back ground and
>> subconsciously want to avoid making classes, or whether this is the
>> best way to construct the project.
>>
>> It seems like a nice way to divide things up, though, so that it helps
>> me think of each file having a set of functions. For example, maybe
>> there would be a third file that does nothing but save and load game
>> data from disk, assuming that I want to build in the capability to
>> save and load games in progress, and I see no reason I wouldn't want
>> to do that. Here again, though, I'm thinking that the chess board file
>> would import the one that loads and saves the data. And I'm thinking
>> that the file that does that may not need to contain a class that does
>> this. I'm not that far in my thinking though.
>>
>> Thanks for listening.
>>
>> Jim
>>
>> ________________________________
>>
>> This e-mail and any attachments to it are confidential and are
>> intended solely for use of the individual or entity to whom they are
>> addressed. If you have received this e-mail in error, please notify
>> the sender immediately and then delete it. If you are not the intended
>> recipient, you must not keep, use, disclose, copy or distribute this
>> e-mail without the author's prior permission. The views expressed in
>> this e-mail message do not necessarily represent the views of Highmark
>> Inc., its subsidiaries, or affiliates.
>>
>
>
> --
> Soronel Haetir
> soronel.haetir@xxxxxxxxx
> ** To leave the list, click on the immediately-following link:-
> ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
> ** If this link doesn't work then send a message to:
> ** program-l-request@xxxxxxxxxxxxx
> ** and in the Subject line type
> ** unsubscribe
> ** For other list commands such as vacation mode, click on the
> ** immediately-following link:-
> ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
> ** or send a message, to
> ** program-l-request@xxxxxxxxxxxxx with the Subject:- faq
>


-- 
Soronel Haetir
soronel.haetir@xxxxxxxxx
** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: