Re: Can we use cpp to write WDM driver ?
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Mon, 4 Jun 2007 08:57:15 -0500
"Maxim S. Shatskih" <maxim@xxxxxxxxxxxxxxxx> wrote in message
news:eb1gInHpHHA.1240@xxxxxxxxxxxxxxxxxxxxxxx
As far as I have seen, any deficiency you can name about C++ has a
similar parallel in C.
Bad sides of C++ is semantics hiding and thus major decrease in code
readability.
Having overloaded operators like "+" saves you typing, yes, and makes the
code
more beautiful from the fully-abstract-ivory-tower point of view.
But in terms of real-world practical code support I would prefer to have
Add()
method instead of "operator +" in 99% of cases (for everything except
strings
and complex numbers).
So you do want operator+, you just think it's overused. Hardly a reason to
forbid it entirely.
These operators can also be grossly misused even by the language author,
like
"<<" for stream output. I consider this particular feature of C++
libraries to
be atrocious enough to be never used at all. printf() is better, and also
can
provide the rich formatting specs. So, I never use "iostream.h" even in
MFC C++
apps.
I see nothing "elegant" in "stream << integer" to print a number. Sorry,
but
"<<" is a bitwise shift.
All of this goes worse if we are speaking on "operator T". For "<<" and
"+",
you at least see the lexical token and understand that there can be an
overload. For "operator T" to be called, you need no lexical tokens, just
slight type mismatch of the 2 sides of the operator.
Even C# is better in this - I don't think they have "operator T".
It does, right here:
http://msdn2.microsoft.com/en-us/library/z5z9kes2(VS.71).aspx
Now the ByRef parameters. Look:
Func(&MyObj);
the "&" is a lexical reminder that MyObj can be changed by Func. In C++,
if
Func is void Func(MyObj&), you can use:
Func(MyObj);
which will change MyObj! This is contra-intuitive.
So mandate that all references be const.
ByRef parameters are only useful for some "operator" functions which
require
lvalue as their operand, like "operator[]". I see no other valid and good
uses
of them. Pass the object by pointer if you want to update it.
No, references are also useful to avoid copying large structures. Use const
references to ensure "least surprise".
C++ has major potential in producing "write-only" code. Not as bad as Perl
in
this, but worse then nearly any other language except Perl.
Now some OOP bad sides in general, not related to a particular C++
language,
and also present in C#, Java and Delphi.
I'm about the "fragile base class" problem, which is _unsolvable_ in
practice
and hardly solvable in theory. This problem is nasty enough to replace
inheritance by code copy-paste sometimes (yes, copy-paste has its own
issues,
but they are not necessary worse then fragile base class issues. Note: MS
developed the cache manager interaction code in their _file systems_ using
copy-paste).
Now about RTTI. First, Stroustrup wrote a very valid chapter in his book
(with
Ellis) about RTTI being evil and thus not supported in the language. And
then... in 90ies, he added this evil thing to the language? for what? his
own
arguments of early 90ies from the book are still valid. Yes, a) RTTI
provocates
non-polymorphic programming b) RTTI can be easily created using macros if
necessary.
RTTI is evil 90% of cases, and the new-style casts like
dynamic_cast<TYPE>,
whose only purpose is to work with RTTI, are just plain evil. The correct
cast
is (TYPE). Lesser visual noise.
Now enough about negative. You can say - "hey! but people do work on C++
and
love it!". I will respond - "yes, a good language, but not for the
situation
where the bug cost is high".
You're mistaking the language for a small number of its features. There are
some features which are very applicable, and some which are dangerous.
In drivers, the bug cost is _rather_ high. Hard-to-repro, not-so-obvious,
requires analyzing the dumps from the client sites and so on. I mean not
monetary cost, but mainly the man*hours cost, which can easily turn out to
become monetary.
With such high a bug cost, _code reviews_ are the very important quality
procedure, and reviewing a stupid C code is easier then C++ one. Yes,
maybe it
takes more hours to type the same C code then C++ one, but _in the
situation
where all typed code is reviewed by your neighbour_ - this cost increase
is
negligible anyway.
The system-level code must be understandable to the idiot and do not
contain
any "smart tricks" which can be mis-interpreted by the human reader.
Yes, if the bug cost is lower - like in most UI apps - then C++ is
eligible. In
the UI app, nearly any bug is trivial to be found - something is not drawn
correctly, the repro is trivial. Also, such apps are often free from
concurrency/synchronization issues (so are most web apps and COM inproc
DLLs).
--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@xxxxxxxxxxxxxxxx
http://www.storagecraft.com
.
- Follow-Ups:
- References:
- Re: Can we use cpp to write WDM driver ?
- From: Maxim S. Shatskih
- Re: Can we use cpp to write WDM driver ?
- Prev by Date: Re: DeviceIoControl blocks even in Overlapped I/O mode? How is this possible?
- Next by Date: Re: DeviceIoControl blocks even in Overlapped I/O mode? How is this possible?
- Previous by thread: Re: Can we use cpp to write WDM driver ?
- Next by thread: Re: Can we use cpp to write WDM driver ?
- Index(es):
Relevant Pages
|