Hi Atul,
I read again about Feature Envy. A short overview is available at http://sis36.berkeley.edu/projects/streek/agile/bad-smells-in-code.html#FeatureEnvy. Unfortunately I don’t have the Refactoring book from Martin Fowler, et al, which introduced this bad code smell, and as I haven’t seen further examples, I may not have understand Feature Envy correctly. I think your main concern is that the public class is feature envy at the private class because the public class accesses the data of the private one. You further don’t like that private classes don’t have ctors of their own and in your opinion the Private class is calling for some refactoring and more “classy” look ;-). I hope I get you right.
Here is how I see the role of the Private class. I have to admit I borrowed the concept form Qt, which is in fact a wonderful framework developed by obviously quite good folks ;-). Earlier Anders Larsen commented that private data is used (in KDE for example) to retain binary compatibility with older versions of libraries. That’s definitely one of the reasons why Qt uses private classes but there are also others. Reference counting on the private data can easily be implemented. Getters can have the same names as the properties without ugly prefixing or suffixing of the private property members. The header file dependencies can be reduced if the headers required for the implementation are included in the cpp file only. There are probably others…
I think what is important to point out what is the problem that the Private class is trying to solve. Languages like C# and Objective C have the concept of partial classes. You can declare functions and member variables in both the header and the source files and have the compiler combine them together. That’s not possible in C++ and the private class tries to circumvent that limitation. That’s the reason why Qt declares the private classes friends and also the private classes declare the public ones friends. Although syntactically separate, the public and private classes are semantically one and the same thing! That said it should be obvious why Private does not need ctors and dtors of its own and why the public class cannot be feature envy of the private part of itself ;-).
Best regards,
Peter
