Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Capturing Photos and Videos Checking External Storage

Lisa Steendam
Lisa Steendam
6,825 Points

isExternalStorageAvailable() : why if?

Isn't it considered better practice to use

return state.equals(Environment.MEDIA_MOUNTED);

instead of the if else block?

1 Answer

It certainly is acceptable, and Android Studio will even offer to change one to the other (using the alt+enter hotkey).

I would also lean towards people using return state.equals(Environment.MEDIA_MOUNTED) because I consider it to have increased readability and a reduction of redundant code compared to:

if (state.equals(Environment.MEDIA_MOUNTED)) {
    return true;
} else {
    return false;
}

But, this is also a personal preference. Some people prefer the longer way and say that has increased readability and is easier to make changes to in the future. Neither way offers measurable improvement over the other when the code runs[1] or is being maintained. So, the way you choose is up to you and/or the team you are working with if they prefer a certain style.

ps - I'm still interested in other's take on this also. ;)

[1] Disclaimer: I haven't actually looked to the compiled version of these, but don't imagine there to be a significant difference.