r/androiddev • • Nov 26 '16

Effective Java for Android (cheatsheet)

https://medium.com/rocknnull/effective-java-for-android-cheatsheet-bf4e3433889a
223 Upvotes

49 comments sorted by

View all comments

5

u/Paradiesstaub Nov 26 '16

How about making everything possible final (class, method, variables).

7

u/Phreakhead Nov 26 '16

Please don't make classes final. I hate when I just want to extend some class to have an extra tiny bit of functionality, but I can't because the author either made it final or made some members private when they could have been protected. Android is the worst at this. So many useful widgets and gesture detectors and what not, all having to be copy/pasted if you want to improve them at all.

12

u/nhaarman Nov 26 '16

Instead, prefer composition over inheritance.

3

u/Zhuinden Nov 26 '16

There are times when you can't just wrap the code with some decorator to fix internal behavior.

So you have to go and copy the whole library into your project and make the tiny bits of changes that 'd be easily done if they were at least package-visible or protected, or the class weren't final.

Flow was the most annoying at this, I had to copy both Flow 0.12 back in the day, and also Flow 1.0-alpha into my project initially to fix its bugs, because most of its stuff just can't be fixed from the outside. Well, technically copying it and then putting it in a forked Github repository was the easiest solution, so there's that.

2

u/Phreakhead Nov 27 '16

Heck even just ListView has scrolling animators, gesture detectors, etc. that would be so nice if you could just override them. But no, you have to hack your way around it with Compat everything or just writing it over again.

1

u/nhaarman Nov 27 '16 edited Nov 27 '16

A library developer, especially the folks over at Square, chooses their public api very carefully. Anything that is not private or final becomes a point some developer will rely on. The moment you change that, you will break their code.

When you expose that your internal data structure is a LinkedList instead of a List, you cannot change that anymore, because somewhere, someone will use that. You decide to rename your internal state variable i to counter? Someone is accessing i.

Suppose you wrote some internal function that does some nasty stuff using private fields and setting some state. One moment between library versions you decide to clean that up and remove the fields, or even just rename them. If you did not declare them as private or final, you're probably gonna break someone's code. And that may cause a developer like you to spend a whole day figuring out why your beautiful changes won't work anymore.

About classes being final, there is an item in EJ that you should design your classes for inheritance, or else prohibit it. There's ton of stuff potentially breaking when you change your 'base' class.

Heck, there's even a commit in AOSP where the fact that Samsung used reflection to access a private field caused the refactoring of some private stuff to be partially undone.

1

u/Zhuinden Nov 28 '16

Heck, there's even a commit in AOSP where the fact that Samsung used reflection to access a private field caused the refactoring of some private stuff to be partially undone.

I'd just keep it package-local as of late so that reflection can be avoided if someone wants to tinker with it. Package visibility is a lesser evil compared to reflection.


Otherwise, I guess that makes sense, although our code wasn't bad enough for this to come up as an issue, or maybe just wasn't complex enough. Not sure.

1

u/philipwhiuk K-9 Mail Developer (GH: philipwhiuk) Nov 29 '16

Heck, there's even a commit in AOSP where the fact that Samsung used reflection to access a private field caused the refactoring of some private stuff to be partially undone.

There's always this: https://www.facebook.com/notes/facebook-engineering/under-the-hood-dalvik-patch-for-facebook-for-android/10151345597798920/

1

u/s73v3r Nov 28 '16

If the thing is fixing internal bugs, then that's how you should be doing it. Then send the fix to the maintainer. Don't subclass to fix behavior, as someone new to the project won't have any idea that you're using MyBaseClass instead of the one the library provides.

1

u/Zhuinden Nov 28 '16

Well I did file issues but I needed the fix sooner than 9+ months later