Object lifetime question... again

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

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)).SomeDatabaseCheck())
{
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
 
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.
 
void Func1()
{
CUtil u = new CUtil(SomeDataBaseConnectionParams);
if (u.SomeDatabaseCheck())
{
DoWhatever();
}
}

vs

void Func2()
{
if ((new CUtil(SomeDataBaseConnectionParams)).SomeDatabaseCheck())
{
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top