Design – Should I always start with UML as a good practice when designing classes

classdesignuml

I am in the middle of creating a Bundle for PHP application based on Symfony 2.

I want the bundle to be clear and good designed, so I constantly move and refactor code, rename methods, reorder classes, move them to other folders, add some factories and adapters, proxies, and follow other oop design concepts.

I'm thinking if I should design UML diagram first, then start to work with IDE.

What is the best practice for designing good architecture?

When I start with draw of UML class diagrams before, then after coding, even with empty methods and tests first, I stop following the diagram I drew before, because I found it not matching what I need.

Can you share good practices for designing architecture for a libraries? I'm not talking about model (if so I could follow DDD).

Best Answer

In a question is something should always (or never) be done, the only reasonable answer is: No, there are always exceptions.

Whether it is a good idea to start with a design in UML depends on the scale of the design, the ramifications if you get it wrong and your own comfort level.

If you do decide to start with an UML design, there are still two ways to use UML:

  1. As a specification language. In this case, the UML diagrams are a direct representation of your code and contain the same level of detail. Often, you can use RAD tooling to generate code from the UML diagrams.

  2. As a communication tool. UML is a powerful tool for communicating designs to others (or your future self), but there are no requirements that the diagrams contain all the minutiae in detail. For effective communication, it is often even better not to show all the details and that includes even stuff like helper classes or relations that are irrelevant to the point you want to make with the diagram.

If you use UML in the second way, then it is very much to be expected that the resulting code only looks like the diagram in broad strokes if you look from a mile away. Closer inspection will show large differences, but that does not necessarily invalidate the diagram.

Related Topic