Validate function preconditions in android
Asked Answered
A

2

6

Usually when writing a public method I do some error checking e.g.

public SomeResult processSomething (int i, List<String> items) {
   if( i < 0 ) {
      throw new IllegalArgumentException();
   }
  if(items == null) {
     throw new NullPointerException();
  }
etc
}

In android programming what is the standard approach for this? I noticed that when a fragment crashes the emulator goes to the previous fragment so from behavior shown to the user I guess it is ok. But what is the best way to deal with exceptional/error conditions?

Arboriculture answered 22/3, 2015 at 11:11 Comment(0)
F
2

The best practices here would be very similar to those used elsewhere in the Java world:

1. The first lines of a method are usually devoted to checking the validity of method arguments. The method should fail as quickly as possible in the event of an error.

When validating an argument, an Exception is thrown if the test fails. It's often one of these unchecked exceptions that are thrown:

  • IllegalArgumentException
  • NullPointerException
  • IllegalStateException

These are all derived from RuntimeException.

2. If every object parameter of every method in a class needs to be non-null in order to avoid throwing NullPointerException, then it's acceptable to state this once in the general class javadoc, instead of repeating it for each method.

References:

Preconditions, Postconditions, and Class Invariants.

EDIT:

To answer your question about "view specific for errors": while it is certainly possible to do that, the idea is that an Exception indicates the presence of programming errors in the code. Therefore, apps should be allowed to crash so that the user can report the error, and the developer thereby gets the error logs from the app's Play Store account. This way he can correct the sources of those errors. The process should continue till, hypothetically, the app is completely free of errors.

Foretell answered 22/3, 2015 at 11:44 Comment(4)
I will accept this also I am not clear on how to gracefully deal with exceptions. I mean create view specific for errors?Arboriculture
To be honest when I have an app crash in my phone I don’t send a report. And I don’t know anyone who actually does. So would it be perhaps best to catch the exception and send the report somewhere by the app and somehow graceful so that an error happened instead of crashing? I am asking because I am new in android and not sure if it is possible/viable solution?Arboriculture
I do get bug reports from my apps on Play Store, although yes, not everyone reports those errors. They should actually be reported by default, but people consider even that to be a "violation of privacy" ... sigh.Foretell
You can certainly create a View that is shown that catches every thrown Exception, I believe something like that is present in Java SE. The only problem I see is that you may not become aware of the reason for certain exceptions. I think if you let the app crash, sooner rather than later somebody will point out the error to you :)Foretell
E
1

Nowadays we can use Kotlin Preconditions.kt:

data class User(val active: Boolean, val email: String?)

class UserHelper (private val user: User) {

    fun mergeUsers(otherUser: User) {
        // To verify enclosing class state we use "check methods".
        // If check fails IllegalStateException will be thrown
        checkNotNull(user.email) { "user email is null" }
        check(user.active) { "user is not active" }

        // To verify argument we use "require methods".
        // If check fails IllegalArgumentException will be thrown
        requireNotNull(otherUser.email) { "otherUser email is null" }
        require(otherUser.active) { "otherUser is not active" }

        // All the preconditions has been meet, so we can merge users
        // ...
    }
}
Everetteverette answered 10/4, 2020 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.