0

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.

The 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.

bruno
  • 32,421
  • 7
  • 25
  • 37
Roman
  • 15
  • 3

2 Answers2

0

I think that C is the right answer. The original situation is that Order accesses OrderLine to get the quantity and via the same object access to Article for the price. With those values it performs a multiplication (within a summing cycle). Now in the new situation it calls subtotal from Order which itself has to access Article for the price and that will return the result of a multiplication. So from a performance view there is absolutely no difference. From a maintainability view the subtotal might be a better approach, though it's advantage to hide a multiplication doesn't seem to be notable anyway.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • 1
    The maintainability thing is linked to the better encapsulation in the second scenario I think. – muszeo May 12 '19 at 22:48
  • @muszeo Probably. But I'D regard this as marginal. Once again a teacher with bad examples to learn from. – qwerty_so May 13 '19 at 05:37
0

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).

muszeo
  • 2,312
  • 9
  • 13
  • Actually, I'm not entirely sure that *getArticle().getPrice()* will optimise to resolve directly to the *price* on the stack. It may just give you a location on the heap for the Article which it traverses to, to then get the *price*. Humm. I think it depends on the compiler. – muszeo May 13 '19 at 02:34
  • In fact, I'm wrong in the above answer. getArticle() will return a reference to an object on the heap. getPrice() then returns a value from the stack. So, in other words, you have to go to the heap to get price from Article in both scenarios 1 and 2. Moreover, because with scenario 2 you're going to the heap one fewer times than in scenario 1, I'd think scenario 2 would be better performing? – muszeo May 13 '19 at 02:47
  • Anyway -- I'm struggling to see why A is correct. I've left the answer up, wrong or not, to discuss. – muszeo May 13 '19 at 02:49
  • 1
    Having done some tests on this (C#, .NET 4.7.2) I can confirm basically no difference between the two in terms of performance. This might be my CLR/compiler optimisations etc. I'd need to write it in C and give it a go, but from a practical standpoint, I'm struggling with A. – muszeo May 13 '19 at 03:19
  • Excellent work! Regarding the maintainability, I agree with your answer and regarding performance, your experiment is the key. I think the performance depends too much on language, environment etc. to give an answer. – www.admiraalit.nl May 13 '19 at 08:00
  • @www.admiraalit.nl agreed! – muszeo May 13 '19 at 08:18