file What kind of diagrams, besides classes, do you need to do to generate code?

7 months 4 weeks ago #6513 by aleb
Hi plo,

Class diagram are enough to generate code.

BR
aleb
The following user(s) said Thank You: plo

Please Log in or Create an account to join the conversation.

7 months 4 weeks ago #6515 by Robert Hairgrove
As already mentioned, you can create some code already just with class diagrams. This will allow you to define some structures, classes, and namespaces.

But in order to create implementations that actually do something, you'll need some behavioral aspects which are usually defined by sequence, collaboration, or activity diagrams.
The following user(s) said Thank You: aleb, plo

Please Log in or Create an account to join the conversation.

7 months 3 weeks ago #6518 by plo
Thanks for the information. A diagram of components may be needed when generating code?

Please Log in or Create an account to join the conversation.

7 months 3 weeks ago #6519 by Robert Hairgrove
It depends perhaps on how many (or how important they are) third-party libraries, etc. you use in your application. If everything is self-contained, you might not need them. Otherwise, one could make a case for having a few component diagrams showing the dependencies between your application and the libraries you are using.

If you are writing a library instead of an executable application, the perspective might be reversed: i.e. one would want to show how other applications could use your library, perhaps without actually generating the code for that use case.

I did not mention use case diagrams before, but this would also help to visualize the user-interactions (even if the "user" is some other component, e.g. the operating system or an external application). They would also fall under the category of behavioral modeling.
The following user(s) said Thank You: plo

Please Log in or Create an account to join the conversation.

7 months 3 weeks ago #6520 by plo
Is it possible to generate code from a diagram that depicts functions rather than classes? What type of diagram can be used to depict analogs of a function, or just to end up with code without classes and functions that we wrote too?

Please Log in or Create an account to join the conversation.

7 months 3 weeks ago - 7 months 3 weeks ago #6521 by Robert Hairgrove
Well, the UML was always about object-oriented programming from the very beginning. If you are doing strictly functional programming, I would use maybe something like flow charts for design only and then write the code directly. I think it would be much more precise and efficient, than to (mis)use a UML diagram and try to let it generate the code for you.

Here are a few considerations:
In order to generate code, the complete model is what is needed. Diagrams are merely "windows" into different perspectives of the model as a whole. You can choose to show only certain elements in a diagram and hide others. However, if you want to generate working code, the model must be at least as detailed and correct as the code you wish to build. And to make a model like that is at least as much work as writing the code to begin with.

The generated code will never be perfect, except perhaps for very small toy programs. The big reason for this is that the UML language is still not 100% exactly specified, and it tries to be suitable for a wide range of programming languages. I think that doing code generation would perhaps be good for a rough starting point, but you will still have to write a lot of the actual code yourself to get it to work. I am using UML strictly for documentation purposes, as it is.

Some implementations have only recently begun to support the UML 2 specification, and I don't know if any tools out there are really up-to-date. Then there is the problem with the programming languages which are also evolving. How many UML tools support forward and reverse engineering of C++20 code, or even C++17?

For C++, UML doesn't handle some things -- like pointers and references -- very well, unless you use a special profile for that language. And even then, only the programmer might know whether to pass an argument by const reference or as an object to a function. Then there is the question of data types: UML has built-in "string" data type, for example, but in C++ you would need to tell the implementation what kind of string class to generate in the code, because C++ has no built-in string type. There is std::string, but maybe you really want some different kind of string. If you are using the Qt framework, for example, you might want to use QString instead. Some libraries, like Xerces-C++, have their own "XMLChar" type, for example. How do you convert between the two?

You could try to write some code, or use some existing code, and try to let Modelio or some other tool try to generate a model from it. Then see what it does. Then take the model, perhaps make some tweaks, and let it generate code and see how close it comes to the original input. I think you might be surprised.

What programming language are you targeting, BTW?
The following user(s) said Thank You: plo

Please Log in or Create an account to join the conversation.

Moderators: tma
Time to create page: 0.045 seconds
^ Back to Top