views:

330

answers:

2

c++ has come a long way, it has lot of feature that lead to do same task in n number of ways. What feature do you think should be avoided and list better alternative for the same.

Like use SmartPointers in places of pointers

+15  A: 

Avoid malloc, use new.

Avoid memcpy, use a copy constructor.

Avoid #defines, use consts.

Avoid (C-style) casts, use static_cast<C++>( style casts ).

Avoid macros, use templates.

Avoid post-increment if you can use pre-increment.

Avoid new, use collections of value types, let the collection deal with memory management.

Avoid pointers to new'd objects, use boost::scoped_ptr or boost::shared_ptr.

Avoid writing your own code to traverse collections, use <algorithm>s

Avoid reinventing the wheel, see if boost has what you need.

Avoid "char * name" for storing strings, use std::string.

Avoid using namespace foo, use using foo::bar.

Avoid "static (internal linkage) declarations" use unnamed namespaces.

tpdi
I can't agree with the generalization of avoiding using plain pointers by using smart pointers. There are at least two situations where plain pointers are useable: in case of interfacing with 3rd party libraries that use them, and in case of aggregations (zero-or-one multiplicity) where the aggregator doesn't own the refered object (implemented as pointer data member).
Cătălin Pitiș
I actually use plain pointers for everything, and I sincerely don't remember the last bug I had which was even loosely related to them. Most of the times it's for consistency with existing code, but I would write brand new classes with them, too. Of course pointer members are private, you initialize them, you have design by contract, you don't play with ownership around, etc.
Daniel Daranas
Sorry Daniel, I just wanted to correctly attribute. On the pointer issue, there are legitimate reasons to use them,and it is possible to use them safely (after C, does just that). The advice is "avoid", not "never use".
tpdi
@tpdi Thanks for the intention, I just thought that removing my name improved readability. I agree with your point. On pointers. :)
Daniel Daranas
"Avoid writing your own code to traverse collections, use <algorithm>s" Although it's generally better to write your own new generic algorithm than to torture the existing ones, if there's no good fit for what you need. This usually happens when what you need is copy_if...
Steve Jessop
+1  A: 

use new style casts

static_cast, const_cast, reinterpret_cast, dynamic_cast,

instead of C style casts which are no longer considered fully safe

also prefer using STL containers over dynamically resizing arrays at runtime

Stowelly