Should an object that totally encapsulates functionality have a "Run" function?

Discussion in 'C++' started by Jenna Olson, Jul 25, 2003.

  1. Jenna Olson

    Jenna Olson Guest

    Hi all-

    I've never seen this particular issue addressed, but was wondering if
    there's anything to support one way or another. Say I have a class:

    class ManipulateData
    {
    public:
    ManipulateData(const char* Path-To-Some-Text-File)
    { ... }

    bool Run();
    long DataCount();

    ... etc ...
    };

    What I'm wondering is if the Run() function is really necessary. If a class
    so completely encapsulates a particular type of functionality that under C
    it probably would have been written as a single function, with no further
    setting of members, is it really necessary or proper to split the start into
    both the constructor *and* the Run()? True the Run() returns a bool that
    would presumably indicate success, but my thought is that because the bool
    only indicates sucess or failure, and in the case failure might be for
    multiple reasons, it would be better to put the object in a try/catch and if
    the object cannot do what it's supposed to do, throw an exception and caught
    by the function that instantiated the object.

    I ask because I realize I've written code that says "ManipulateData
    md("/tmp/blah.txt"); bool bOK(md.Run());" and it seems ... unnecessary.

    Just curious what others thought,

    Jenna
    Jenna Olson, Jul 25, 2003
    #1
    1. Advertising

  2. "Jenna Olson" <> wrote...
    > I've never seen this particular issue addressed, but was wondering if
    > there's anything to support one way or another. Say I have a class:
    >
    > class ManipulateData
    > {
    > public:
    > ManipulateData(const char* Path-To-Some-Text-File)
    > { ... }
    >
    > bool Run();
    > long DataCount();
    >
    > ... etc ...
    > };
    >
    > What I'm wondering is if the Run() function is really necessary. If a

    class
    > so completely encapsulates a particular type of functionality that under C
    > it probably would have been written as a single function, with no further
    > setting of members, is it really necessary or proper to split the start

    into
    > both the constructor *and* the Run()?


    Probably not.

    > True the Run() returns a bool that
    > would presumably indicate success, but my thought is that because the bool
    > only indicates sucess or failure, and in the case failure might be for
    > multiple reasons, it would be better to put the object in a try/catch and

    if
    > the object cannot do what it's supposed to do, throw an exception and

    caught
    > by the function that instantiated the object.
    >
    > I ask because I realize I've written code that says "ManipulateData
    > md("/tmp/blah.txt"); bool bOK(md.Run());" and it seems ... unnecessary.


    It is unnecessary. I think you should only consider doing it if
    the file can be changed during the lifetime of your 'ManipulateData'
    object and you might want to "re-manipulate" the data. Otherwise,
    you could keep the functionality in the constructor and have it set
    some internal flag, which you will query later:

    ManipulateData md("/tmp/blah.txt");
    if (md.isOK())
    ...

    Victor
    Victor Bazarov, Jul 25, 2003
    #2
    1. Advertising

  3. On Fri, 25 Jul 2003 12:31:18 GMT, "Jenna Olson" <> wrote:

    >I've never seen this particular issue addressed, but was wondering if
    >there's anything to support one way or another. Say I have a class:
    >
    >class ManipulateData
    >{
    >public:
    > ManipulateData(const char* Path-To-Some-Text-File)
    > { ... }
    >
    > bool Run();
    > long DataCount();
    >
    > ... etc ...
    >};
    >
    >What I'm wondering is if the Run() function is really necessary.


    What you really should be wondering about is the dependency of
    methods on each other. For example, is a call to DataCount valid
    before calling Run, or after a failed call of Run? If not, then
    DataCount should probably be a method on an object returned by Run,
    enforcing that constraint.

    Designing a class so that the order of method calls matter is
    generally a bad idea, although in some cases there's no way around
    (and there are well-known patterns that address these few cases).

    But if Run is the only exposed functionality then it might make
    sense to have this method. A functor class is one example, with
    operator () playing the rĂ´le of Run. As another example, Run might
    be a virtual method.
    Alf P. Steinbach, Jul 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VtYXIgU2hhbm11Z2Ft?=

    asp.net fail totally, when i run

    =?Utf-8?B?S3VtYXIgU2hhbm11Z2Ft?=, Mar 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    =?Utf-8?B?S3VtYXIgU2hhbm11Z2Ft?=
    Mar 3, 2004
  2. john schultz
    Replies:
    2
    Views:
    464
    Victor Porton
    Jun 5, 2004
  3. Paul

    Server object totally broken

    Paul, Aug 4, 2003, in forum: ASP General
    Replies:
    0
    Views:
    98
  4. Joshua Muheim
    Replies:
    5
    Views:
    128
    Phlip
    Aug 11, 2007
  5. Jeff
    Replies:
    2
    Views:
    101
Loading...

Share This Page