Is it bad practice for lines between different classes to cross in a UML diagram? Is it a design smell? Does it indicate that your classes are too tangled or independent or anything? Is it always/generally unavoidable? Or am I overthinking this?
Here is my current UML diagram if interested. You will see that I routed a couple lines all the way around to avoid overlapping....
Is it bad practice for lines between different classes to cross
Yes, because it can impede understanding of the graph/diagram.
Is it always/generally unavoidable
Not all graphs are planar (see planarity testing), so sometimes it is unavoidable.
But the problem is not as simple as "I will just route the line around". There are many rules and recommendations that constitute a good layout or contribute to good visual aesthetics; to name just a few:
lines crossing (you might have seen in some diagrams a small bridge when lines are crossing)
number of line bends
total line length
hierarchy (typically inheritance lines should be drawn upwards)
You can already see that some of the rules are competing; so layouting is always a balancing act. If you are interested in more detail I suggest googling around for "graph aesthetics" or "automatic (graph) layouting".
Unfortunately most research papers I have are behind paywall, so I cannot link to the pdfs, but some of it can be google-able; e.g. UML Diagram Layouting: the State of the Art or Aesthetics of Class Diagrams
In my opinion, line crossings may be unavoidable, but often, it happens when I am trying to show too many things with a single diagram. I see this as a "communication" smell rather than as a design smell.
I often found that explicitly writing down the question I am trying to answer helps spot the model elements that are not strictly necessary and that I should omit. Besides, several diagrams may better convey an idea/design than a single saturated one.
In your example, you could maybe break down your diagram following the colored areas you already identified.
When ever there are crossing lines, it is a good time to stop and think if the diagram has grown to be too complex.
I feel your diagram is trying to be abstract and concrete at the same time. I see it as being too complex. It is abstract in the sense that it provides an architectural overview, and concrete in that it shows structural detail related to implementation.
Personally, I might split your diagram into a component diagram; then express each component's structure using separate class diagrams.
Related
I have created a sequence diagram, and found it has multiple nested alt.
Is this a good or bad practice?
If it is a bad practice, what should I do instead?
The good, the bad and the ugly
It is difficult to give advice about good and bad practices without objective criteria, and these will depend on the purpose of the diagram:
If you use UML for some kind of visual programming, where a comprehensive diagram should show all the details of a specific interaction, nested alt could be a good practice if there is no alternative. Since different lifelines are driving independent alternatives (FusionAuth the outer alt, Occupations the inner one), the nesting represents the behavioral logic appropriately. But if a same lifeline would drive decision, a flattened alt could a be a more readable way, trading the more complex nesting against more but simpler branches.
If you use UML to communicate and to reason about a system, the diagrams should be easily understood: nesting would be a bad practice, since it adds a level of complexity.
We fortunately have avoided the ugly: identical nested alt in several branches.
Alternatives to nested alt
The UML secret is to have more diagrams but smaller ones, each focusing on a single aspect. You can find this advice at the end of almost every chapter of Booch, Jacobson and Rumbaugh's book The UML User guide.
Two strategies are applicable here:
Diagrams per scenario: the main success scenario would be one diagram, and the different failure scenarios would be the others. Super-easy to read.
Separation of concerns: Different concerns would be addressed in different diagrams you could for example separate the use of Occupations by its clients and the way Occupations does its business: separate Manager, Client, Occupations, and Occupations, FusionAuth and Database in two diagrams; You'd avoid nested alt, the inner one being in the second diagram and not necessarily relevant for the same audience.
Remark: I'm not a big fan of visual programming. But if you are, the second strategy is fully compatible with it, with the advantage of preventing the ugly repeating of the same nested fragments in several places.
Sort of. You can do that and it will be okay. However, once you start doing that you are in danger to fall in graphical programming. Once upon a time people dreamed of graphical programming as the solution for the future. Simply, it isn't. Code is much denser and easier to read. So don't start writing programs that way.
Now for the use of that construct. Where ever you want to show some complex collaboration a graphical overview of the participants will be most helpful. But only if you stick to the most important part of tha communication. Rather than nesting fragments it's better to have different SDs to focus. You can use message endpoints to cross between detail diagrams. Again, it depends on how you make the split. It needs some experience to find the golden ratio.
Im in a confusion on the sequence of drawing UML diagrams. I cannot understand where to start drawing them. Which diagram should come first if I have the scenario with me?
Start with Use Cases (and the related diagrams). Once Actors and Use Cases are identified, you can start designing a Class model. Create Collaborations realizing the Use Cases. Then inside show how the Instances of the Classes collaborate to actually realize what is needed for the Use Cases.
I recommend the ICONIX approach since that is what got me going a long time ago.
The other option is to start with Activity Diagram. I.e. make a business process analysis.
I always start with a problem-domain class diagram. (Some examples of what I mean are here and here.) Understanding the concepts in the domain and the relationships between them is extremely important. If you don't understand the problem domain, everything else will be confused and brittle, including business processes, requirements, use cases, and collaborations. Here is an example of an improper domain model, and here are the recommended corrections.
I would like to know which diagram from those two should be created first when designing large IT system.
In every article I read about UML, the component diagram is mentioned as almost last.
I think that, when designing a larger system is better to divide it to smaller parts/modules on the component diagram and then for every component create a detailed class diagram. Is that correct?
How it look in practice from your experiences?
Do you know of any article/tutorial that shows from beginning to end, the design of system using UML?
There is no mandatory order for UML diagram design - in fact, you will often find yourself creating several simple diagrams of different types, and refining them later. The process is generally iterative but doesn't have a follow a particular order.
That being said, some UML diagrams provide a more abstract view than others, and it may sometimes make sense to start with the most abstract views and work your way down to the most specialised ones. Use case diagrams typically come early, while object diagrams or state diagrams often come later in the design process, once the original, abstract solution has been refined.
You will also want to take a look at consistency between your diagrams: indeed, with several views you will introduce redundant elements, and it is important to make sure that they do not contradict each other. For example, if you have a sequence diagram and a class diagram, any call message in the former should correspond to an operation in the latter. See Alexander Egyed et al's work for automated consistency management and correction in UML models.
I had some questions regarding the structure and behavior of a model, using UML, and the relationship between the two :
Did you find any limitations for UML regarding the specification or understanding of the relationship between structure and behavior?
I was wondering if you have any practical ideas of how one can optimize the relationship between structure and behavior, using UML.
Do you know any UML tools that help understand better this relationship or represent it much easier?
Thanks
Yes:
A sequence diagram is readable at a high level, showing how a transaction involves a few components; but it's not good (not readable) at the detailed level, showing how a transaction involves dozens of methods (method A calls method B, which gets data from methods D and E, and then invokes method F, etc.).
Looking at a class diagram, you might see a based class with several subclasses; this tells you nearly nothing about the behaviour of the classes (it only tells you that they may have some behaviour in common, or at least a common API, plus some individual behaior that's unique to each subclass).
That's a big question. A quick answer is, "Attach text notes to the objects: diagrams aren't sufficient without descriptive text."
No, I don't really; a UML tool help you create UML diagrams (and generate code from the diagrams), but it's up to you how you use it. There was a neat product described in the book titled Real-Time Object-Oriented Modeling (1994) which was an executable model, i.e. the model itself had behaviour, but I know of no UML tool quite like that. The closest I know of is being able to "round trip" between the model and code (i.e. generate code from the model, and the model from code).
Sounds like a homework problem. Wiki can tell you all about UML.
The limitations of UML are the same as any form of communication. The simpler your language, the fewer things you can communicate and the clearer your communications will become. A shape like a square or circle identifies a structure, a line indicates relationship, an arrow indicates movement, or flow. You could enhance this by defining the meaning of other properties, like direction, boldness, color, number count, different shapes. You could incorporate multimedia layers like audio or video, motion, tooltips- but now we're not talking about UML anymore.
My favorite UML tools are a whiteboard and some dry erase markers.
I think that things have changed, regarding UML's usefulness to melculetz.
In Visual Studio 2010, I can define an association relationship, that will generate composite classes. I can specify the multiplicity and class qualifiers. I can also generate classes from the model.
Presently, I am attempting to visually model the phases of a system, in order to visually define the methods for a state-machine object. That is my attempt to integrate structure and behaviour. Check my blog to see how I get on.
Class Analyser visually expresses the behaviour of class objects. Limitation removed.
I think that the answer is to turn your development methods towards MDA. You will generate more classes, but the payoff is in terms of manageability and re-use (where you template your efforts).
I am still working through my model but, I find VS2010 promises good tools for managing the development process. I have yet to investigate UI modelling, but have heard the rumours. I may have it all wrong but I think that, by working with Lightswitch, I may be able to model the UI also.
UML allows you to specify the signature of a method, and group methods into classes, but it says nothing at all about what code you use as implementation. If that's what you mean by "behavior", I don't think UML addresses it at all at the class level.
It's even worse at the UI level. My impression of UML is that it's woefully inadequate for specifying UIs.
I think the effort required to embed everything into UML is greater than or equal to coding the application, with the added burdens of UML tools being poor IDEs and inability to prove correctness of UML the way you can with unit testing.
UML is way oversold, IMO. I consider it a convenient notation for informal communication between developers, nothing more. It has never been and never will be the object oriented equivalent of engineering drawings.
I'm fond of using UML diagrams to describe my software. In the majority of cases the diagrams are for my own use and I use them for more involved pieces of code, interactions etc. where I'll benefit from being able to look back over them in the future.
One thing I've found myself doing a few different ways is diagramming threads. Threads by their nature tend to pop up in the more involved pieces of code and keeping track of them is often a primary purpose of my design documents.
In the past I've used a symbol in a sequence diagram to show the creation of a new thread but looking back at some diagrams doing that it's sometimes ambiguous between an object's lifetime - which sequence diagrams are for - and a thread's lifetime. Is there a better approach for incorporating threads into UML?
I managed to produce a diagram that makes sense to me at the time of drawing it. The basic premise is that I've overlaid grey boxes representing class instances with blue boxes representing thread lifetimes. The main thing it lets me keep track of is knowing which thread I will be executing on when I call certain methods.
No doubt there's better and more intuitive ways to do thread and class modeling. The measure of success for me is whether my own diagram still gives me the same level of understanding 6 months down the track.
Activity, Sequence, and State Diagrams are all correct ways of showing thread behavior.
1st: (To vs's comments) There are two sets of diagrams or modeling elements in UML, static structure, as you put it, and behavioral. Any book will help you understand the split, typically in the contents/TOC, additionally it can be seen on page 11 of Martin Fowler's UML Distilled a near defacto standard for beginning UML in my opinion.
2nd: (To sipwiz's question and comment) Activity diagrams are not commonly understood to model business process, they can be used for that however, and most examples or simple tutorial would approach it from a business standpoint.
Discussion on your options to model threads:
Activity diagrams - Allows for forking and specifying concurrency by using a BAR and usage lines. Note the example at the bottom is no a business process, example. Most people can read these, business, management, and developers, though sometimes they can lack detail or get messy.
Sequence Interaction diagrams - In the same post, example, you will see sequence diagrams allow you to specify parallel behavior within a sequence by boxing parallelizable behavior with a label "par", this is useful to show the reader what methods can or should be called in parallel, ie, by different threads. This is the method I would use for detailed developer like discussions around building an object.
State diagram - The state chart just like the activity allows for concurrency by using a BAR and usage lines.
NOTE: These will not model a specific thread and it's exact lift cycle, as that is part of the instance/run-time level of modeling, if this what you want clarify your question and I will respond. I would just model it using one of the above as no one other than a MDA/UML expert will call you out, and you are not generating a running system.
Also: Please note that further details can be found in most UML books.
Also leveraged: http://www.jguru.com/faq/view.jsp?EID=56322
Traditionally threading has been depicted diagramatically using Petri Nets. Rob Martin has an article on multithreading in UML which you may find useful.
Update- just remembered you can represent threads with forks in activity diagrams- I've managed to find something that explains this.
It is very hard to find any free tutorials for Petri Nets, however I know Petri Nets are good for modeling concurrency, so I Google'd "producer-consumer Petri Nets" (my favourite threading thing) and found this.
I've also found some slides that show Petri Nets modeling a Semaphore.
UML activity diagrams have fork and join elements to show parallel flow of logic.
I don't know of a way, but using a sequence diagram does not seem entirely inappropriate, considering that a thread is in many languages implemented as a Thread (or similar) class.
The most UML-compatible way would probably be to add an annotation of some sort indicating that the 'object' represents a thread.
The UML is defined by the UML Superstructure, you can find it here http://www.omg.org/spec/UML.
If you read the specification you find that a UML class can be active. An Active Class is a class with the meta-attribute isActive set to true. It is also depicted differently.
An object instances of an active class automatically executes a "classifier behavior". As for any behavior you can define it by means of an activity in which you wait for asynchronous signals (AcceptEventActions) and invokes methods (CallOperationAction) or other behaviors (CallBehaviorActions). That is how active objects are modeled in UML. You just have to read the UML specification.
Activity diagrams will model the internal workings of your software with forks and joins to represent threads. To find out exactly how to model this properly, please see Conrad Bock's excellent series of articles. Here is the article that covers forks and joins, but you should follow the links back to the first article in the series to learn how to properly model using "Colored Petri Nets". It's not how you think (and it's pretty easy)!
There is a new, in-process standard at the OMG for a language called Alf that provides a more convenient surface notation for activity diagrams and is intended for representing code. From the spec:
A primary goal of an action language is to act as the surface notation for specifying executable
behaviors within a wider model that is primarily represented using the usual graphical notations of
UML. For example, this might include methods on the operations of classes or transition effect
behaviors on state machines.
For a programmer, you probably can't get more intuitive than Alf. And it will convert perfectly into UML activity diagrams.
UML strongest point is depicting the static structure. If you use short-lived threads, I also don't see any easy way of diagramming them. Maybe you can find a solution by turning things around a bit: why do you use/need threads? What's the functionality they provide? If they interact with each other and follow some (message passing) API, drawing them as components might make sense.