Fog Creek Software
Discussion Board

Conflicting advice - what to do?

I have just been noodling around on the website, since we will be using xp to some extent on future projects.

I knew there was something that bothered me about "You’re NOT gonna need it!" ( but it took a few seconds to realise what it was.

Often you will be building some class and you’ll hear yourself saying "We’re going to need...".

Resist that impulse, every time. Always implement things when you actually need them, never when you just foresee that you need them

Compare this to
A class with any of {destructor, assignment operator, copy constructor} generally needs all 3

I think many people do not follow the above "rule", which fits quite nicely with the xp practice described before but can get you into big trouble.

The rule exists at least partly because copy ctors can easily be called implicitly, and it is easy to miss those points where it happens, because the compiler goes behind your back and generates a copy ctor if you do not provide one. You can, of course, just declare the methods concerned without defining them (link errors if the methods are ever called), but then quite possibly someone else, who should not even care about the implementation of your class, is going to have to write those methods because you did not (after all, they were not needed at that time...). Is it in this case better to write some redundant code if you think "We're going to need... a copy ctor (but we don't need it yet)"? Are there other instances where this, or other, xp practices go against "best practice for xyz language"?


Friday, May 24, 2002

Personally I think your answer is in your faq link. 

E.G.:"Yes, there are some practices which are generally considered dangerous. However none of these are universally "bad," since situations arise when even the worst of these is needed"

There is no one size fits all for all situations - there is no "best practice" outside of context. 

XP wants to encourage the practice  (to paraphrase) "do no work before its time".  Given a choice, I would follow the XP guideline... for no other reason than the one you gave.

p.s.  I also don't litter my C++ with "const" either.


Joe AA.
Friday, May 24, 2002


I really don't think that your example is one of the things Kent Beck meant when he said not to plan in things you don't need yet. If you have a destructor, you need a copy constructor and an assignement operator! It is not "you might need it in future" It is just not save to have one without the other. This is more a language specific detail of
C++ than something connected with your project design. This makes it explicitly different from problems like "I have one document type now and maybe I want another one in the future, so should I implement a base class now?" To this the XP answer would probably be "No", because you might well be content with your one document type for all times. And if you are not, what you thought to be a good common ground for a base class might not be any longer once a new document type turns up.

Have fun,

Jutta Jordans
Friday, May 24, 2002

I don't actually see a conflict here. XP says "don't build it until you need it", but that is meant to be interpreted at a feature level, rather than an individual lines of code level, I think.

I think the point that is being made in the C++ FAQ Lite is just that {destructor, assignment operator, copy constructor} is really a single feature, not three independent functions.


Mark Bessey
Friday, May 24, 2002

The advice isn't really in conflict. Treat the C++ advice as "if you need one of {destructor, copy constructor, assignment operator} then you must *declare* all three."

It's perfectly reasonable to write
private: Foo (const FOO&);
without ever providing an implementation. If code outside your class attempts to (inadvertantly) use the copy constructor, it will get a compile-time error. If code inside your class attempts to use it, it will get a link-time error.

After you get the compile-time error, then you can follow XP's advice and implement the routine.

Jim Lyon
Friday, May 24, 2002

*  Recent Topics

*  Fog Creek Home