Fractions in Study class 1.11


#1

Hello guys…
I’m working in the studio for the class 1.11

I’m reading this case, but I don’t understand what is the logic in the method simplify. I understand a fraction should be able to simplify itself, but in my logic, it should change itself and that is it, why it says that the method should return a new fraction? What is the logic there?

Fraction

A fraction has a numerator and denominator. A fraction should be able to add itself to another fraction, returning a new fraction that represents the sum. A fraction should be able to multiply itself by another fraction, returning a new fraction as the product. A fraction should be able to take the reciprocal of itself, returning that value as a new fraction. A fraction should be able to simplify itself, returning a new fraction as that simplification.


#2

Hi,

This is an interesting question. Before answering, I reviewed my solution and realized that I had implemented mine just as you would, using the logic “it should change itself and that is it”. :laughing:

Mathematically, both approaches work. But perhaps there is a business logic for NOT destroying the original value.

Reasons for returning a new fraction instead of modifying the original one:

  • To learn the difference between mutator/modifier functions and pure functions
  • To avoid potential undesirable side-effects (when a value/object is used by two processes and is unexpectedly modified by one of them)
  • To demonstrate an ability to understand, follow and meet the technical requirements/specifications (I failed this one, apparently. :fearful::joy:)

This is a great question, and you should ask it on our class’s Slack. Perhaps the instructor and the TAs have a different take on it. I am curious to hear other opinions about it too.


#3

Hi Leonid, Thank you for your answer.
Yes… Exactly this is what I thought, could have some uses… bur really is not logical to return the new fraction, because finally is the same value of the actual state of the fraction.
I wish I can ask some teacher. Unfortunaly Im not in USA yet.
Thank you for your answer, because it seems we had the same idea, or the same logical process…
Was funny your 3th reason… lol… thank you for sharing!


#4

If you are a member of the South Florida class, there is an online Slack channel. It’s a real-time messaging system that you should be able to access from anywhere.

By the way, in Chapter 13, Topic 4. Arithmetic Methods, the textbook implements the simplify method as a mutator (modifying the original fraction). Perhaps they indeed wanted us to see both versions/approaches.


#5

Thank you Leonid… I never got this email. But now Im in USA, I will ask the first class that I have. Thank you Again!