3

i am doing one Android app.My client is asking me to include assertions in the android code.I have googled across and found that Commonsware says that assertions should be avoided in android code.But i need a strong reason why using assertions is avoided? Please let me know if i should use assertions or not.And if so what are the rules or suggestions using assertions in android.

Community
  • 1
  • 1
anshul
  • 982
  • 1
  • 11
  • 33
  • Maybe your client wants unit tests from you? Are they technical person? – MaciejGórski May 11 '13 at 14:26
  • @MaciejGórski no they are not . – anshul May 11 '13 at 16:27
  • Then I would suggest asking them what they really want as our programming world and "normal" world may use `assertion` with a different meaning. Answers provided here are good, but I wouldn't use them when talking to non-technical person. – MaciejGórski May 11 '13 at 16:31
  • @MaciejGórski no actually are technical in terms of technology but they are not technical about Android – anshul May 11 '13 at 16:43

2 Answers2

8

But i need a strong reason why using assertions is avoided?

An assertion is intentionally introducing an unhandled, uncatchable error (AssertionError, specifically).

Assertions do no work in Android by default. You have to specifically enable them. This means that whatever logic you are trying to validate via assertions will not be employed on production devices.

Hence, I agree with this assessment:

Step zero: Refactor comments into assertions

Step one: Refactor assertions out of the code into unit tests

Step three: Escalate the remaining assertions into program exceptions

So you are certainly welcome to validate inputs, confirm outputs, etc. Just do not use assert. Instead, handle the condition in some other fashion. If nothing else, throw a RuntimeException like an IllegalArgumentException, so that your top-level unhandled-exception logic can get control.

A trivial search of the Internet will turn up many articles regarding the choice of assertions versus exceptions, such as:

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Doesn't Effective Java recommend using asserts to check private methods when a result check isn't inherent in the code? Why would this be different only on android? – Rarw Jan 30 '14 at 13:25
  • @Rarw: "Doesn't Effective Java recommend using asserts" -- if the author of that book does, then I and others disagree with that author. "Why would this be different only on android?" -- it's not, other than that assertions are disabled. My answer contains hyperlinks to materials that disagree with the use of assertions in standard Java, not Android. – CommonsWare Jan 30 '14 at 13:32
2

What would be the point of such an Assert ?
On a production code Asserts are not going to do much good, they will only detect why what you considered as an invariant is not as you expected, but it won't prevent your app from crashing.
Furthermore, the Assert Class is part of junit.framework.Assert, it is here to be used in an unit test. So it is a way better practice to establish unit tests that will tests these invariants, that way you can act when a future development breaks them.

Teovald
  • 4,369
  • 4
  • 26
  • 45
  • 3
    `assert` is a Java keyword, triggering an `AssertionError` if the assertion fails. http://stackoverflow.com/a/2758645/115145 – CommonsWare May 11 '13 at 14:24