I am now doing a UML test section, I understand what does performance and maintainability mean in general, but I don't understand how to rate them with UML class diagram. The test question is:
Consider the model depicted with the UML class diagram in the following figure.
Now consider a change of the model in terms of adding a subtotal attribute (with the value article.getPrice()*quantity) and a get method for it to the OrderLine class, and using it within getTotal(). What effect would this change have on the non-functional properties of the system?
A) worse performance of Order.getTotal(), better maintainability of the system
B) better performance of Order.getTotal(), worse maintainability of the system
C) no effect
D) worse performance of Order.getTotal(), worse maintainability of the system
E) better performance of Order.getTotal(), better maintainability of the system
The right answer here is A, so please explain how to come to this answer.
Thank you.
Maintainability is better in scenario 2 because the encapsulation of OrderLine is improved (i.e. someone maintaining this code would thank you for not including logic that should be in OrderLine in Order instead).
Performance will be worse in scenario 2 since the (probable) compiler optimisations will mean that both getArticle().getPrice() and getQuantity() calls will optimise to the same as directly accessing the price and quantity variables in memory (being primitives these will be on the stack, assuming these get methods are simple accessors), whereas subTotal() will not optimise in this way since it is not a simple accessor (i.e. it contains logic itself) which means you are therefore forcing a traversal to the heap (where the OrderLine instances live) in order to get access to the price variables. The stack is much faster to access than the heap.
So in scenario 1, the price and quantity variables are accessed directly on the stack by the loop in Order. In scenario 2 you're forcing a reference/pointer traverse to the heap due to calling the subTotal() method, which takes time to process, albeit a minute difference in reality for today's CPU and memory bus speeds.
Stack = primitives (i.e. price, quantity) and references/pointers. Heap = objects (i.e. Order, OrderLine and Article instances).