## Fragile Base Class Problem MidTerm Exam vs. Slides

jonasmolina
Neuling
Beiträge: 5
Registriert: 7. Feb 2015 09:19

### Fragile Base Class Problem MidTerm Exam vs. Slides

Hi there,

I hope that someone of the teaching staff takes a look at this and, hopefully, can provide a reasonable answer.

After today's lecture me and some colleagues talked to Mr. Eichberg about the solution to the question regarding the fragile base class problem in the midterm exam. As in the lecture where he discussed the reference solution, he said that the fragile base class problem occurs if some small changes are made to the base class. Here, a small change is something that does not change the existing methods of the base class, but rather adding some method to the base class. The newly added method is, of course NOT implemented by the subclass, s.t. problems can arise.
Up to here everything sounds reasonable.
Now, the problem we encountered is that the inheritance slides (http://stg-tud.github.io/sedc/Lecture/s ... e-2on1.pdf) explicitly state on slide 15 that "in the future the designers of HashSet decide to re-implement addAll [...]". This is more than the small change which was suggested in the discussion of the exam, and it stands in contrast to NOT changing the existing classe, as we were told today.

It would be helpful if someone could clarify the situation.

Cheers!

Boddlnagg
Mausschubser
Beiträge: 54
Registriert: 10. Dez 2012 12:07

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

I've been there when you were asking the question yesterday, and I've heard Mr. Eichberg's answer. If I understood it correctly, he defined a small (or "benign" change) to be something that does not affect ordinary clients of the class, i.e. clients that only use the class and do not inherit from it. Therefore, changing existing methods is not a benign change if you change the method signature, but it is a benign change if you only change implementation details. The slide that you referenced talks about the self-call structure (i.e. not using the add method to implement addAll). Since that is an implementation detail, it would be considered a benign change, and the fragile base class problem does indeed occur.

JohannesRudolph
Windoof-User
Beiträge: 31
Registriert: 7. Nov 2010 01:42

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

I'd be curious about the reference solution here. I find the example to be a bit arbitrary... It could be me but I'd find the question easier to answer if we were told that a certain change should be made to the base class / has been made to the base class and deciding whether that breaks or does not break subclasses.

As it stands, the exam question is poorly designed in my opinion (and I'd appreciate if Dr. Eichberg takes this criticism into account for designing the real exam).

I found these issues.

- LogBankAccount does not override the deposit method (which could have been added later to BankAccount). But it could also be intentionally only log withdrawals. How am I supposed to know? I have no requirements for the code.
- The withdraw override logs the withdrawal even if the super call throws an AccountException (and no withdrawal occured). This could be an instance of the FBCP _if_ that exception condition was added to the base class later on - but it may also have been there in the first place and since it's also documented(!!) in the base class method's doc it's rather an implementation error in LogBankAccount. Or maybe the requirement underlying the code was to log all withdrawal attempts? In that case the code is correct.

Now, one could argue that potential implementation errors in base classes are indeed an instance of the FBCP, but it's all very blurry. If so, the question should say: "please list _potential_ issues of the present design related to the fragile base class problem. Give an example for two possible changes that could have been made to the base class and how they did break the LogBankAccount class deriving from it."

Visions
Neuling
Beiträge: 4
Registriert: 14. Jan 2015 12:35

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

Might be a bit late of an answer and i'm not 100% sure if it's correct, but in my opinion the 2 problems that are related to the fragile base class are:

1. if BankAccount defines a method log() and calls it after withdraw, it will be overriden by LogBankAccount and gets called twice (one time from the overriden withdraw method and once from super.withdraw()) for each withdrawel.
2. if BankAccount changes the transfer method, to not call withdraw but implement the withdraw code in transfer (yea, shouldnt be done, DRY, but still possible) withdraw won't get called from transfer and won't be logged anymore from LogBankAccount

Both changes won't be "visible" in the derived classes and will work for normal clients the same way as before and can therefore be considered as benign chances.

Your assumptions are all that there are "errors" in the provided code. But Fragile Base Class defines the problem, that if i change the base class the derived class may malfunction. Therefore i would guess the provided code works as it should.

ayushmaan_jain
Mausschubser
Beiträge: 64
Registriert: 18. Okt 2014 11:11

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

Visions hat geschrieben:1. if BankAccount defines a method log() and calls it after withdraw, it will be overriden by LogBankAccount and gets called twice (one time from the overriden withdraw method and once from super.withdraw()) for each withdrawel.
Wouldn't it show compile time error in LogBankAccount class because if log() method existed and later BankAccount adds it, then compiler will complaint that this method should use override keyword ?

Visions
Neuling
Beiträge: 4
Registriert: 14. Jan 2015 12:35

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

Hm true, in Scala you do need the override keyword. In Java you don't afaik.
So in Scala my first problem described would not happen, it seems.

In that case, consider the following:

change deposit so it does not increase balance but calls withdraw with a negative amount.

Code: Alles auswählen

    def deposit(amount: Long): Unit = {
if (amount < 0) throw new AccountException("amount has to be positive")
withdraw(-amount)
}

In that case the log method gets called on deposit, which might not be wanted.

phopner
Mausschubser
Beiträge: 45
Registriert: 26. Apr 2012 02:11

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

If a log method gets added in the base class, it would not compile in Scala, but according to the slides ( page 19 ) that also counts as FBCP.

JohannesRudolph
Windoof-User
Beiträge: 31
Registriert: 7. Nov 2010 01:42

### Re: Fragile Base Class Problem MidTerm Exam vs. Slides

Visions hat geschrieben: Your assumptions are all that there are "errors" in the provided code. But Fragile Base Class defines the problem, that if i change the base class the derived class may malfunction. Therefore i would guess the provided code works as it should.
That's what I meant saying that the question is poorly designed as it doesnt state whether we should list potential issues we can create by modifying the base class or if we are looking at the design post-change. In any case you can make up pretty arbitrary issues because we don't have requirements for the code or the change made to it.