[Numpy-discussion] Proposed Roadmap Overview

Sturla Molden sturla@molden...
Mon Feb 20 14:12:51 CST 2012


Den 20.02.2012 20:14, skrev Daniele Nicolodi:

> Hello Sturla, unrelated to the numpy tewrite debate, can you please 
> suggest some resources you think can be used to learn how to program 
> C++ "the proper way"? Thank you. Cheers, 

This is totally OT on this list, however ...

Scott Meyer's books have been mentioned. Also look at some litterature 
on the STL (e.g. Josuittis). Getting the Boost library is essential as 
well. The Qt library have many examples of beautiful C++.

But the most important part, in my opinion, is to put the "C with 
classes" mentality away. Look at it as compiled Python or Java. The STL 
(the standard C++ library) has classes that do the same as the types we 
use in Python  --- there are parallels to tuple, dict, set, list, deque, 
etc. The STL is actually richer than Python. Just use them the way we 
use Python. With C++11 (the latest standard), even for loops can be like 
Python. There are lamdas and closures, to be used as in Python, and 
there is an 'auto' keyword for type inference; you don't have to declare 
the type of a variable, the compiler will figure it out.

Don't use new[] just because you can, when there is std::vector that 
behaves lika Python list.

If you need to allocate a resource, wrap it in a class. Allocate from 
the contructor and deallocate from the destructor. That way an exception 
cannot cause a resource leak, and the clean-up code will be called 
automatically when the object fall of the stack.

If you need to control the lifetime of an object, make an inner block 
with curly brackets, and declare it on top of the block. Don't call new 
and delete to control where you want it to be allocated and deallocated. 
Nothing goes on the heap unless STL puts it there.

Always put objects on the stack, never allocate to a pointer with new.  
Always use references, and forget about pointers. This has to do with 
putting the "C with classes" mentality away. Always implement a copy 
constructor so the classes work with the STL.

std:: vector<double>  x(n); // ok
void foobar(std:: vector<double>&  x); // ok

double* x = new double [n]; // bad
std:: vector<double> *x = new std:: vector<double> (n); // bad
void foobar(std:: vector<double>*  x); // bad

If you get any textbook on Windows programming from Microsoft Press, you 
have an excellent resource on what not to do. Verbose functions and 
field names, Hungarian notation, factories instead of constructors, 
etc.  If you find yourself using macros or template magic to avoid the 
overhead of a virtual function (MFC, ATL, wxWidgets, FOX), for the 
expense of readability, you are probably doing something you shouldn't. 
COM is probably the worst example I know of, just compare the beautiful 
OpenGL to Direct3D. VTK is another example of what I consider ugly C++.

But that's just my opinion.


Sturla


More information about the NumPy-Discussion mailing list