Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I dont understand why 'assert' is allowed on private methods but not on public methods

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading the section on Assertion in the book and it lists the following rules:

- Dont use assertion to validate arguments to a public method
- Do use assertions to validate arguments to a private method

I dont understand why it is allowed on one and not the other. Surely if either code is compiled with assertions disabled it wont work or have i misunderstood it. ?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the reason would be:

Public methods are used by lot of other people and you dont want the method to behave differently depending on the arguments passed (and behave differently would be due to the wrong use of asserts- like suppressing the actual exceptions). The Public method (and not asserts) should check for wrong arguments being passed and throw exceptions if any wrong arguments are passed.

Private methods- its under the control of the creator and he knows exactly how and where the method is being used. That way he knows what arguments are being passed to it.

Long time I read about them, someone might be able to correct me or add to it.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion there's nothing wrong with using asserts in public methods, too. The point here is, that "assert" can be enabled/disabled with a simple command line flag of the JVM. So you CAN use them to make some assertions for preconditions, postconditions etc. during the test phase of an application but the logic of your code should definitely NOT depend on asserts. That means asserts can be used in public methods as additional assertions but they should not be used for regular parameter validation and something like taht because they could simply be deactivated at runtime.

Marco
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood that assert is only to be used during debugging or testing. What i didnt understand is why they are recommending that i should use assert when debugging private methods but not when debugging public methods.

Its true that public methods are used by other people and i dont have control of what is passed into the public method. Just because i have control of the private method it doesnt mean that i wont make a mistake. Since i will be debugging/testing both the public or private methods i dont see how it would make a difference.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since i will be debugging/testing both the public or private methods i dont see how it would make a difference.

The difference is that you document some assumptions you made when you wrote the code for a method. For example if you decided that some (private) method should not be called with null as parameter value (under normal conditions) it's OK to have no null checks in this method because it simply expects that parameters should not be null when the method is used correctly. That's were asserts are a good way to document such assumptions inside the code itself.

But it's NOT ok to use asserts to validate input parameters for public methods because you simply cannot (always) contol what the caller of a method will give you as a parameter value. You should make the necessary checks with the usual Java tools but don't depend on asserts for your logic to work correctly. But it's fine to have asserts besides the regular check mechanism.

Marco
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Ehrentreich wrote: it's OK to have no null checks in this method because it simply expects that parameters should not be null when the method is used correctly


Wrong, you should ALWAYS check the preconditions in public methods. Assertions must be explicitly enabled, hence you can't rely on them for validating public methods as they'll be probably called by other people. You can use them to validate your private methods, you enable them during testing to make sure that you're using your methods correctly, once tested you don't need the extra checks...
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong, you should ALWAYS check the preconditions in public methods.

Uhm... I talked about private methods in this case. I'm sorry if I wrote it somewhat misleading but I think my message was the same as yours

Marco

(PS: Unfortunately I'm not a native speaker/writer)
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Ehrentreich wrote:
Wrong, you should ALWAYS check the preconditions in public methods.

Uhm... I talked about private methods in this case.


Yepp, you're right, sorry. I'm too tired...
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic