473,218 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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

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
Jul 19 '05 #1
2 2503
"Jenna Olson" <wa*******@comcast.net> 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
Jul 19 '05 #2
On Fri, 25 Jul 2003 12:31:18 GMT, "Jenna Olson" <wa*******@comcast.net> 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.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
3
by: news.microsoft.com | last post by:
Hi folks, I got samples on http://www.aspfaq.com/show.asp?id=2059 and runs ok to dir ms-dos command, but to others commands like del, rd, deltree, etc... it has not had any effect, even it has...
14
by: Vilmar Brazăo de Oliveira | last post by:
Hi all, I was using: <% Set objWshell = Server.CreateObject("WScript.Shell") objWshell.Run "%COMSPEC% /C ren topoX.jpg topo_cliente.jpg", 0, TRUE %> BUT the comand COMSPEC disapeared of all my...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
2
by: Rafi B. | last post by:
Sorry for the English. I mean, when building my applications, I often need to do several database actions, and I'm thinking, what's better? 1) Open connection with DB, for instance mySQL, at...
3
by: =?Utf-8?B?SmFuIEhlcHBlbg==?= | last post by:
Hi, I've a question. I'm developing a windows application and for the application i need to run functions and procedures that are stored in a database. Here is an example that i tried to get...
4
hsn
by: hsn | last post by:
hello everyone. i am trying to run the sudo -i command in my mac. here is the code String s = null; try {
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.