Re: C or C++?
Gianni Mariani wrote:
J) In C++, restrict your interface by 'public', 'private' and
'protected' members, in C everything is public.
In my experience one common difficulty self-taught C programmers,
who haven't learned nor understand object-oriented programming and
the abstraction/modularity related to it, is that they often have
hard time accepting the notion of public member variables being,
in the general case, a bad thing. They just can't understand why
everything can't just be public and why information hiding and
abstraction are a good thing. It often takes long time for them
to finally understand why.
Another slightly related issue is that, for some reason, they
(and in fact many other coders too, not just C ones) have an odd
aversion towards having to write anything extra. For example, many
coders prefer putting a member variable in the public part of the
class instead of putting it in the private part and writing an
accessor method for it, which they detest. They just don't like
having to write any extra code and can't see the benefit in abstracting
that member variable away.
I often see this phenomenon in C++ code out there. One good example
of this is the PopCap library. The vast majority of member variables
in the classes of that library are public, and the only reason for
this is that the developers were lazy and wouldn't bother creating a
more abstract design.
Of course this causes some negative side-effects. For example,
certain optimizations are impossible to implement in the library
without breaking tons of existing programs which use the library
and access the member variables directly (the library itself probably
also constantly accesses the variables directly, and thus would
require a major cleanup if this was changed).