Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.