473,396 Members | 1,827 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,396 software developers and data experts.

Object lifetime question... again

Hi,

As a follow up for my previous post, i have one more question:
Do following two examples differ in terms of optimization(execution + memory):

void Func1()
{
CUtil u = new CUtil(SomeDataBaseConnectionParams);
if (u.SomeDatabaseCheck())
{
DoWhatever();
}
}
vs
void Func2()
{
if ((new CUtil(SomeDataBaseConnectionParams)).SomeDatabaseC heck())
{
DoWhatever();
}
}

I don't see much difference, only one assignment operation more in the first case,
but the second case seems to me much more clear in terms of readability.

PS. No need to note about Dispose(), as this particuar class doesn't implement it. Database
disconnect occures in Finalizer.. Don't ask me why, that's just the way it was done long before me...

Any suggestions are appreciated!

Thank you,
Andrey
Nov 16 '05 #1
3 944
On Thu, 17 Feb 2005 11:29:07 -0500, MuZZy wrote:
void Func1()
{
CUtil u = new CUtil(SomeDataBaseConnectionParams);
if (u.SomeDatabaseCheck())
{
DoWhatever();
}
}

vs

void Func2()
{
if ((new CUtil(SomeDataBaseConnectionParams)).SomeDatabaseC heck())
{
DoWhatever();
}
}


In my opinion the first is more readable. The first is easier to debug,
especially if an exception is thrown. If an exception is thrown in the
second, without digging a little are you going to know that the error is in
the constructor of CUtil or the call to SomeDatabaseCheck?

From an optimization standpoint you should find no difference. In fact, I
believe you will find that the same IL is generated in either case.
--
Tom Porterfield
Nov 16 '05 #2
Andrey,

In this case, honestly, I think you should not be concerned. Unless you
are doing a gazillion of these operations, in uber-tight loops, and you are
controlling nuclear reactors, or something of that sort, code readability
and maintenance always wins out.

Basically, go with #1. It will be easier to read, easier to upgrade and
maintain, and if there is a performance difference, it will be negligible.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"MuZZy" <le*******@yahoo.com> wrote in message
news:4r********************@comcast.com...
Hi,

As a follow up for my previous post, i have one more question:
Do following two examples differ in terms of optimization(execution +
memory):

void Func1()
{
CUtil u = new CUtil(SomeDataBaseConnectionParams);
if (u.SomeDatabaseCheck())
{
DoWhatever();
}
}
vs
void Func2()
{
if ((new CUtil(SomeDataBaseConnectionParams)).SomeDatabaseC heck())
{
DoWhatever();
}
}

I don't see much difference, only one assignment operation more in the
first case,
but the second case seems to me much more clear in terms of readability.

PS. No need to note about Dispose(), as this particuar class doesn't
implement it. Database disconnect occures in Finalizer.. Don't ask me why,
that's just the way it was done long before me...

Any suggestions are appreciated!

Thank you,
Andrey

Nov 16 '05 #3
Thanks, guys!
MuZZy wrote:
Hi,

As a follow up for my previous post, i have one more question:
Do following two examples differ in terms of optimization(execution +
memory):

void Func1()
{
CUtil u = new CUtil(SomeDataBaseConnectionParams);
if (u.SomeDatabaseCheck())
{
DoWhatever();
}
}
vs
void Func2()
{
if ((new CUtil(SomeDataBaseConnectionParams)).SomeDatabaseC heck())
{
DoWhatever();
}
}

I don't see much difference, only one assignment operation more in the
first case,
but the second case seems to me much more clear in terms of readability.

PS. No need to note about Dispose(), as this particuar class doesn't
implement it. Database disconnect occures in Finalizer.. Don't ask me
why, that's just the way it was done long before me...

Any suggestions are appreciated!

Thank you,
Andrey

Nov 16 '05 #4

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

Similar topics

4
by: johny smith | last post by:
Suppose there is a policy that all objects are statically declared. For example: static Car car(); Then, is there a reason to have a destructor defined for the class Car. It would seem...
8
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
3
by: Bob Altman | last post by:
Hi all, I have a basic question regarding holding references to objects in unmanaged C++. Suppose my unmanaged C++ class has a method that accepts a reference to an object as an argument, and...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
0
by: Stodge | last post by:
Hi folks, new to Boost Python and struggling to build a prototype at work. I thought I'd start with a conceptual question to help clarify my understanding. I already have a basic prototype working...
6
by: Rajesh | last post by:
I read Global Object's constructor will be called before main() function; In which situation it can be really helpful? Is it good practice use Global object and its constructor ? Thanks,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.