Saturday, April 21, 2018
On preferring the use of getters over direct field access
You often find advice that says you should prefer getters over directly using a field. Fowler terms this SelfEncapsulation.
I've sometimes thought this is a bit abstract and a case of over-engineering...but today I found a good reason why it's necessary.
Consider the following code:
It turns out this NPE happens because of the implementation of java.io.File#isInvalid.
Because this uses this.path instead of getPath(), even providing a canned answer for an external mock doesn't work well. Since #isInvalid is final (but according to the JavaDoc has rudimentary logic that they probably intend to evolve).
Encapsulating even your use of private fields behind getters provides unexpected hooks for testability and maintainability.
Tuesday, February 20, 2018
Using ECMAScript 6 modules
This may be obvious to everyone else, but today I found myself constructing a toy HTML/JS example and wanting to use the new hotness of ECMAScript 6 Modules.
I constructed the following page.
This refused to work. I kept getting
ReferenceError: greetCustomer is not defined
Reading the docs a little more, I decided to change it. I changed my script tag to type module and removed the onready function bit. No joy.
I tried importing directly, using an example I'd seen. Now I got:
SyntaxError: import not found: getUserPreferredLanguage
I finally got things working with the following
It seems like importing modules via the destructuring syntax by default is not the way to go.
Subscribe to:
Posts (Atom)