10

I'd like to know if UML is useful to describe heavily-templated code. I know that there is a notion of parametrized classes in UML (vector<T> <---- <<bind>> ---- vector<int>), but this won't be enough to describe complex duck-typed relationships that we can have with templates. I can always put associations between such classes, but it seems to me that it won't actually help to understand how it works.

Moreover, considering for example the STL library, the algorithms are often implemented as functions, and thus don't fit very well into the OOP model, though they still behave quite like objects introducing associations between classes.

So, does it make sense to draw UML diagrams to describe such templated code? If so, which kinds of diagrams would be most useful - in "standard" OOP I've seen class diagrams being used most of the time, but probably for templated code some other kinds of diagrams would be more helpful? If UML is not well suitable for that, what else could I use?

13
  • 1
    +1 Since, I remembered the tortures to model templates with Telelogic Rhapsody. Feb 24, 2011 at 15:13
  • 6
    "No," because the answer to the more general question "Is UML useful?" is "no." Feb 24, 2011 at 18:34
  • 2
    @James McNellis: what is useful then? How would you make a high-level overview of a system? Digging into the source/comments to all those classes to understand how they are related doesn't look like a good option.
    – Roman L
    Feb 24, 2011 at 19:04
  • 1
    Good documentation and a couple of block diagrams depicting the system architecture. Feb 24, 2011 at 22:14
  • 2
    @James: As far as I know, UML is just about standardization of block diagrams to avoid everyone inventing their own visual language. So what kind of block diagrams are you talking about exactly?
    – Roman L
    Feb 25, 2011 at 0:17

1 Answer 1

2

I think it depends.

If different instantiations will lead to significantly different behaviours or relationships, then while it would be possible to model with UML, it would be ugly. On the other hand, if the behaviour is really that different with different instantiations, I'm not sure templates are a good idea, anyway.

But if the relationships between classes remain more or less the same no matter how they're instantiated then an UML class diagram is going to be just as useful to model those relationships as it would be for non-parameterized classes.

7
  • This is true only until the templated classes behave as classes. Imagine an algorithm (templated function) that takes an iterator and a predicate, does something and writes results to an output container. How would I represent this kind of relations with a diagram? It is the function that creates an association between the iterator, predicate and container, but simply drawing this via associations wouldn't explain anything, moreover, there is far more than just one function...
    – Roman L
    Feb 24, 2011 at 19:10
  • @7vies: OOP (among other things) states "Everything is an object", so don't worry about functions:)... Remember Template Method, Strategy, Abstract Factory, Visitor...? Feb 25, 2011 at 7:58
  • @Gabriel: Yes, that's why template metaprogramming is not so much OOP. The big difference that I can see is that OOP is about "complex objects, simple interaction", while templated code usually has way more complex interaction between classes. Some diagrams could still be useful, and that leads to my question.
    – Roman L
    Feb 25, 2011 at 13:21
  • @7vies: I don't think you understood my comment. OOP is about simple objects with simple interactions... AFAIK it is true templates (parametrized types) come from algebraic specifications, not from OOP. But metaprogramming is in the veins of Smalltalk. The patterns are all objects for behaviour - functors in C++. Feb 25, 2011 at 16:48
  • 1
    @Gabriel: Now I don't understand you for sure :) Objects in OOP might have simple interfaces, but they can have a very complex underlying structure, so I wouldn't say they are always "simple". Anyway, the OOP approach clearly separates objects and messages between them. Functors/higher-order functions are related to functional programming which is clearly different from OOP. Metaprogramming applied to classes is a kind of mix of the two, so it is not obvious how this can be described using OOP-targeted instruments.
    – Roman L
    Feb 25, 2011 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.