Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: "valentin tihomirov" <V_tihomirov@xxxxxxx>
- Date: Sun, 10 Jun 2007 13:41:00 +0300
Please explain how the above code prints "10" if object creation is
rolled back when a constructor throws an exception.
Because you do not roll back the assignment.
Again, constructor is used to allocate resources.
It is nonsense to use finally to unconditionally release the resourses
you
need to allocate.
Hence "along with a success flag".
You do not want to use finally, therefore.
No, you don't want to use a finally and rollback *unconditionally*.
Just keep a single boolean flag to say whether or not the whole
operation succeeded, and make each finally block check it. Simple.
Peahaps, the try-catch is in your list of discouraged constructrions along
with goto. That is why you prefer finally with a flag instead of the
construct, which intentionally intorduced to take action in case of error.
I can't remember the last time I needed a finalizer - they're very
rare.
If GC does all the deallocation for you means you do not allocate
anything
besides memory. You have no need to create a destructor.
I use plenty of non-memory resources, but don't rely on the GC to
release them - I use the IDisposable pattern.
Which is a destructor. I told nothing about a need to use finalizers.
The use of macros has made life incredibly painful for millions of
developers, which is why macros don't tend to be in modern languages.
Improperly, unconsciously applied hammer can be even more painful and
dangerous. Macros are code generators. Using them greatly reduces amount of
code (program redability). The alternative of not using them in C is loads
of code. You can live without them in OOP languages because we have
exceptions supported at language level. Nevertheless, occasionally, the lack
of macros is observed. For instance, in logging
public void log (int severity, str message) {
if (severity >= debug_level)
print(message);
}
you have a lot of log function invocations. If the debug level is high
(normally, you log only errors), the function will do nothing. According to
Log4j project, the most logging time is spent on constructing the text
message. So, you construct a message to discard it. And the function is
public, so it cannot be inlined. In Delphi, which lacks ternary operators, I
also felt the lack of macros to implemement it. However in C, avoiding
macros is huge waste of labour and storage resourses.
.
- Follow-Ups:
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: Jon Skeet [C# MVP]
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- References:
- already used in a 'child' scope to denote something else
- From: valentin tihomirov
- Re: already used in a 'child' scope to denote something else
- From: Jon Skeet [C# MVP]
- Go ahead. Stop programming. This ensures you from any mistakes.
- From: valentin tihomirov
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: Göran Andersson
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: valentin tihomirov
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: Jon Skeet [C# MVP]
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: valentin tihomirov
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: Jon Skeet [C# MVP]
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: valentin tihomirov
- Re: Go ahead. Stop programming. This ensures you from any mistakes.
- From: Jon Skeet [C# MVP]
- already used in a 'child' scope to denote something else
- Prev by Date: Mapping a Decorator Pattern in NHibernate
- Next by Date: Re: Class Instances
- Previous by thread: Re: Go ahead. Stop programming. This ensures you from any mistakes.
- Next by thread: Re: Go ahead. Stop programming. This ensures you from any mistakes.
- Index(es):
Relevant Pages
|