C ++ IDE that supports Scott Meyer's recommendations: Prefer non-member functions other than members

Scott Meyer's argument that non-member functions increase encapsulation and provide a more elegant design (s) seems very true to me. See Here: Article

But I have problems with that. (And it seems like others, especially library developers who usually completely ignore this)

The code usually looks better and more logical when I use member functions. This can be an acquired taste, although it takes some getting used to looking for algorithms first and then objects. (Shiver)

So maybe I have only one problem:

With me member functions And my IDE knows what a class can do.

This is huge for me! I do not use anything that does not support code completion of a member function for programming. In well-designed libraries, it completely replaces the documentation for me. And even if I looked at the api doc, looking at the list of members just felt completely natural, logical, and I can be sure that, well, this is the end. If there is no method, I can safely assume that it does not exist, and I can write my non-member friend.

I agree with this in STL, because, well, it makes sense to consider the algorithms separately from the basic components and because you get used to it.

I have not seen an IDE that could tell me which non-member functions work on a particular class.

And this is actually my question: Is there an IDE (or IDE function) that helps with this convention with the code?

+4
source share
4 answers

Meyers is certainly correct that using non-members increases encapsulation by minimizing the number of functions that can potentially access a private state. However, encapsulation is just one of many (often conflicting) aspects of code quality.

He really concludes that the library writer will not necessarily write functions for every possible use of the class (since there may be cases that they don’t think about). This means that you may have to add non-member "helper" functions yourself, as you would if they followed Meyers’s advice. Therefore, there is no way to know that a set of member functions and friends is really the only set of functions that act on a class.

As a technology, the “IDE” I prefer (text editor and shell) has the following “feature”, which is very good for finding functions that act on a class:

find . -name '*.h' -o -name '*.cpp' | xargs grep MyClass 

I can not comment on the "real" IDE.

+4
source

I have come across this in the past.

My idea was then rather clumsy, but it did the job: namespaces.

What I did was

 namespace myclass { class MyClass { ... }; MyClass operator+(const MyClass& lhs, const MyClass& rhs){...} } 
+4
source

I do not believe that the IDE can tell you all the non-member functions that you can use with your class. Using templates is simply tricky to make a list of all such features. IMO, best of all you can hope that the IDE can tell you before compilation whether the call you are trying to make is really valid. And even this requires some serious compilation process inside the IDE.

I understand how you use member functions as a replacement for documentation in classic classes. But Scott Meyer's design suggests that we are not talking about classes that provide complex functionality, but simply basic ones. Complex functionality comes from other sources, the original class may or may not know about it, it does not really matter. All of this is part of the idea. But you are right. In this case, there is a need for thoughtful documentation.

+1
source

Try using Visual AssistX, it has this nice feature: right click on your class, Refactor (VA X) -> Find Links. It really works.

+1
source

All Articles