Re: Is 'Final' for classes and methods a good feature or something that should be eliminated?



Daniel T. wrote:
Michael Feathers <mfeathers@xxxxxxxxxxxxxxxx> wrote:


It's not just a simple matter of being annoying. Final can make code untestable.

The sad fact is that API designers don't yet know how to use it without rendering code untestable.


I think blame is being placed incorrectly in this case. It isn't 'final' that is causing the problem.

Let's look at the example again... We have a class that contains a method that causes some side effect that the test harness doesn't want occurring during testing. There is several problems here without ever even mentioning the word final. If the side effect is part of the documented behavior of the method, then I expect that any class that calls the method needs that side effect to happen for it to be considered correct. If that isn't the case, then the real problem has nothing to do with final, it has to do with the fact that no interface was defined for the user to use. After all, if the user doesn't depend on the documented effects of the method for correct operation, then it shouldn't care what class the object it sends its message to is.

It sounds to me like the blame should be placed on the lack of an interface for the client to use, not in the fact that this particular server isn't designed to be sub-classed.

I think, instead of insisting that 'final' is at fault, you should be insisting that interfaces be more prevalent in the library in question.

Yes, that's a way around it. I guess it comes down to why people are using final. If the intention is to prevent misuse via object substitution, I suspect that many library designers would be just as resistant to using interfaces.

----
Michael Feathers
http://www.objectmentor.com
http://michaelfeathers.typepad.com
author of: Working Effectively with Legacy Code (Prentice Hall 2005)
.



Relevant Pages

  • Re: 3d Interface GUI
    ... Richard James ... I wouldn't mind an interface like in the movie Minority Report. ... But unfortunately for some of us, like programmers and designers we have to ... Systems, Applications Programming etc. ...
    (comp.games.development.programming.misc)
  • Re: How Well Does Word 2008 Doc Transfer to Word 2007 PC User?
    ... the 2007 interface is a PITA. ... And the ribbon ... Unfortunately the designers don't work for Word, ... John McGhie, Microsoft MVP, Word and Word:Mac ...
    (microsoft.public.mac.office.word)
  • RE: bitrates and frame rate
    ... total bit rate to half when you make the frame rate half, ... I guess the designers of the interface decided to give priority in enabling ... Especially with 'lossycompression' such as wmv, ...
    (microsoft.public.windowsmedia.encoder)
  • Re: Amazon MP3 Store is pretty damn good, but of course theres _always_ a catch :-(
    ... The iTMS uses a custom XML format (it is not a website because it ... it's a limited interface and often badly ... web designers rarely get this and often opt for glitter and ...
    (comp.sys.mac.system)
  • Re: Interface standards (was Re: Dual Port RAM)
    ... We designers want standard interfaces to FPGA bits and bobs. ... I don't want the silicon defined by the interface. ...
    (comp.arch.fpga)