Hi this question is very broad.
SCENARIO --
I want to diagrammatically represent the functions of my class and how they call each other? The functions have a definite pattern and I want to do this to accommodate future changes such as adding a new functionality which will require to write a new function.
Eg:
function_1() ---> calls function_master()
function_2() ---> calls function_master()
If tomorrow function_3 is created then the makers should know that they need to make a call to function_master().
There are many such complex relationships.
The Real Question --> What I'm trying to achieve, does it have a name? like function mapping diagram.
I know about class diagrams. I don't want to include them. I want detailed diagram of only functions calling each other.
Can you suggest some tools so that I can make it?
It is called a function call graph.
A tool that may be used is doxygen
Imagix 4D will be of great help to you.
You may also look at wiki's link for static Analysis tools
Related
Admittedly a quite theoretical question.
And I would like to ask it more from the perspective of a library designer, than a library user.
Although the goal is to provide the easiest possible design for the user.
Is there any gideline / best practice of how to communicate that a given interface is supposed to be implemented by the user all the time? Or that somewhere there are factory functions provided, creating reasonable objects implementing that interface?
Of course in almost all cases this should be clear from the context. Another library function expecting such an interface as parameter could be self-explanatory of where to get that from. Because it will just be a link in some chain.
But I hope some of you can imagine a rather evolved system or library that is not that easily understood anymore.
How can you prevent the understanding of interfaces from getting more and more difficult, regarding the basic question of whether there are some factory functions somewhere or whether the user always needs to provide its own implementation?
Does the answer lie in comments, documentation or code?
I would just guess that factory functions should always be declared in the very vicinity of the interface. And if not, there is none.
But I don't know if this is too soft a guideline or perhaps cannot be realized all the time anyhow.
I'm working on a fairly large project that involves 3D drawing, and I want to add some visualizers (for example, to see the bounding boxes of the objects) to make debugging easier. However, I'm having a problem in deciding how to go about this.
One option would be to create a public function to draw the visualizers, and call this function when I enable debugging from the UI. This would have the advantage of not modifying the existing functions, but extending the class with a new function. The disadvantage would be "creating dependencies", as one of my colleagues said, we would need to modify the base class, and all the deriving classes to add this function.
Another option would be to modify the existing drawing function so that it handles the drawing of the visualizers. This hides the implementation details, but also it seems to me it makes the code less modular.
Yet another option is extending the class, adding the visualizer in the drawing function, and swapping classes when debugging is enabled. Mixins would be of help, but C++ doesn't support that.
What would be the best approach to do this? I'm looking for a solution that is modular and respects the SOLID principles.
It looks like you are looking for the "Delegation Pattern". See http://en.wikipedia.org/wiki/Delegation_pattern
In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator.
See also http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/delegation.html
I have a question concerning good programming style. I have a group of methods that is handling the flow of my program. These functions uses objects from another class. Is it fine to make a class called something like Functions and list these functions I have there? Not a single instance would be created of this class. Should I rather not include these functions in a class at all? Basically they do stuff like opening the Main Menu and alike.
Not sure if this is a stupid question, I couldn't find any similar topics on this forum. I'm not asking how to do this, rather how to handle it regarding style.
Thanks a lot in advance.
This is exactly what namespaces are for. Don't try to wedge things into "the OOP way" when they don't fit. If your design says that you'll never create an object of a class type then it's not a class, just an agglomeration of functions.
While reading your question, I instantly thought about the Math class in Java (not sure what is the C++ equivalent). You probably know it, it is basically a collection of math operations. Math class
In my opinion, the kind of class you're talking about in your question must have a "collection of general functions" objective, similar to the Math class. Things like opening a menu should be a method of that menu or that menu's parent.
On the other hand, things like calculating some values using specific objects from your program or formatting elements following a customized pattern should be gathered in a "static" class in order to make that class an utilitary tool in your program.
Math operations are very general functions that can apply to any program. Try to transpose this concept within the context of your program! ;)
Put them in a class as static functions for now. You may realize later you'll have some commonality... perhaps state? Storing that in the class might make sense. And you'll want to check things there perhaps before calling your other objects... before you know it, you'll remove your static aspects and instantiate your own object. Perhaps even later consider multiple threads and several of these objects. You get the idea... leave yourself room for an object-oriented approach...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ code dependency / call-graph “viewer”?
I am working on a huge C++ code base and currently I am stuck with the problem of modularizing my code. I have to divide my code into separate independent modules.
One approach I can think of is to generate the dependency graph and then do a higher level categorization. Other approach is to start with a entry point (some function abc()) and generating a function call tree whose each node will contain the name of the file in which that function resides. Thereafter I can use some script to extract those functions into separate modules.
My question is, is there any tool that can help me do this task? Has anybody faced such a problem earlier. Or can you suggest any approach to achieve the same?
First level of modularization - and I hope you already have done that - is structuring your code in classes. If your code is merely a collection of functions - i.e., C plus some syntactic suggar - then it's high time to rewrite your code, because no amount of dependency-graph-building will save you from maintenance hell.
If you have classes, modularizing should be a matter of finding classes that closely work together (like Customer, Order and Invoice) and separate them from classes that are only losely coupled with them (like Employer or Facility). Then take it from there.
Modularizing code is something that requires, first and foremost, thought. No automatic procedure is a replacement for that. Actually, from what little you wrote, I would fear that any automated process would make things worse, because apparently there has been too little thought invested in this project already. I.e., you wrote 1 million lines of code without thinking about modularization, and now you want modularization to happen while still not actually thinking about it. You are heading for epic fail.
To get some overview doxygen might help. But you have to play around a little with the doxyfile settings to generate dependency graps and if your Code base is huge you should disable dynamic stuff from the generated methods.
Doxygen can create include, inheritance, call and caller graphs using graphviz.
Here are simple examples but it also works for bigger ones.
But doxygen will only give you an overview and no refactoring capabilities.
I regularly use "Understand for C/C++" to investigate these kind of dependencies.
If the code base is really huge and you start your modularization from scratch, you might want to look at some other tools, like:
Cytoscape (which can take the output of "Understand for C/C++" to visualize the dependencies
Lattix
It sounds like you are looking for a refactoring tool. Try taking a look at the answers on this question: Is there a working C++ refactoring tool?
One method will be a bit long but what you can do is to remove a method and compile to find dependencies and than group the decadencies into one component. Although this does not resolve your issue fully but it is an approach to start off with.
I am currently refactoring a very useful but poorly designed class in C++, and I'm running into a problem with the design: rather passing data around using arguments to methods, the data is passed around by setting private state variables in the class. This makes it very difficult for me to diagram out how data moves through functions. It's my weekend task to try and remove this style of passing data around as much as possible, as makes the program very impossible to understand from just the method signatures, as the signatures only tell a part of the story. I've decided
My current approach to test if a method communicates using private class-level variables is the following:
Edit the method and make it a function rather than a method, which removes its access to the state variables in the class.
Edit all of the calls to the method so that they call the function rather than the method.
Compile, see if anything breaks. Make a list of accessors to add to the original class.
Run the unit tests to see if I've broken anything in a very subtle way.
Is there a better way of doing this, perhaps one that can be easily automated? Is this refactoring a well-known technique that I can cite if I show it to other people?
The only mention of this problem that I've found so far is this quote from Coders at Work via the Object-oriented programming Wikipedia entry:
"The problem with object-oriented languages is they've got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle." - Joe Armstrong
Edit in response to a good question from Oli Charlesworth:
I understand that the point of OOP is to sometimes communicate through state variables of the class. The difficulty with my current case is that there are currently 78 different data members in the class, many of which are key-value pairs of strings to other data types, and there are undocumented implicit dependencies on the order in which they need to be initialized. It's possible that given a sufficiently smart programmer working with this class would be easy, but it's currently very difficult for me. I think that several of these data types could be abstracted into their own classes, but before I can do that I need to understand more clearly how the data members interact with each other.
Given the clarification in the question my "are you sure it's not just that you don't like the other programmer's style" comment dies a death ;)
Personally I'd just refactor normally. That is, with 78 data members and lots of bits that are related but not in a class of their own I'd start by grouping the related data and extracting the functionality that works on it. There's no need, IMHO, to go through a stage where you explicitly pass the data into the functions in the existing class. Just pick a group of related data items, come up with a decent name, extract them and work out where they were used and how you need to move functionality into the new class.
Ideally, I'd start writing unit tests for the main class and the new broken out classes as I went along...
Instead of making all of the method's callers call the function, a smaller intermediate change would be to leave the method in place for all callers, and have it simply delegate by calling the function. Later you can inline the method call so all callers are directly calling the function.
Also, from your description it sounds like you are approaching this with manual testing. You will have better success (easier refactoring with reduced risk of error) with comprehensive unit tests in place, although of course the code you describe would be hard to unit test. Nevertheless, work toward more test automation.