• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assertion  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which conditions assertions should not be used ? (i know the difference between appropriate and correct over here)
more clearly,
In what are all the conditions using assertions is inappropriate?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Prasath AV:
In which conditions assertions should not be used ? (i know the difference between appropriate and correct over here)
more clearly,
In what are all the conditions using assertions is inappropriate?


When you are writing "software".
Using assertions is appropriate for academic demonstrations or many other contexts outside of writing software. Within the context of software, there is no legitimate circumstance for using assertions since there are two well established contradictions with deductive logic. The hyperbole hides this fact so be careful.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah, as so often you say that something that is well-established should never be used, and you use high-falutin' language to show that you're so clever that you must be right.

I find assertions very useful, for adding additional consistency checks within an application. Such checks provide lots of useful bug-finding info within the development environment, but can be turned off on release so that performance is not impacted.

Could you explain what your objections are to the concept of assertions? Or perhaps it's not the concept that you object to, but something specific about how they are implemented in Java? Could you try to use language that a reasonably savvy programmer, rather than an advanced theoretician, could understand?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Oh yeah, as so often you say that something that is well-established should never be used, and you use high-falutin' language to show that you're so clever that you must be right.

I find assertions very useful, for adding additional consistency checks within an application. Such checks provide lots of useful bug-finding info within the development environment, but can be turned off on release so that performance is not impacted.

Could you explain what your objections are to the concept of assertions? Or perhaps it's not the concept that you object to, but something specific about how they are implemented in Java? Could you try to use language that a reasonably savvy programmer, rather than an advanced theoretician, could understand?


I certainly don't intend to somehow purport correctness by using "high-falutin' language" - I merely intended to use English. I'm sorry I cannot help you any further with that problem.

In terms of what is well-established, I have the usual awful problem of trading off between:
1) making sense
2) fitting in with the agenda of this forum (and in particular this beginner's forum).

I hope it is sufficient to say two things:
1) definite assignment, what it means, how it works, how it should be used - I admit, there are very few people who can thoroughly explain this, so perhaps elaboration is in order.
I'll give you this much:

Take a good long hard look at that code then try to spot the difference:

Another thing I will say is that Haskell - one of the most correct languages that expresses the foundations of computer science (not to be confused with CS taught at universities) - must have an else associated with any if. I hope that is enough.
2) An optional pre-condition? Give me a break! I hope this flaw is obvious. DbC at least attempts to explain what a pre-condition is and why they are unavoidable albeit in a not-so-correct way (but at least it is appealing). A thorough knowledge of DbC is at least enough to highlight the absurdity of "on/off assertions".
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Prasath AV:
In which conditions assertions should not be used ? (i know the difference between appropriate and correct over here)
more clearly,
In what are all the conditions using assertions is inappropriate?


Short answer is: Assertions are typically enabled at the time of debugging and testing but you should disable them at the time of deployment. Although assertions are still in your code but they are simply ignored by JVM.

Naseem
 
R Buck
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that Mr. Morris is saying that asserts don't really have any place in production code because they effectively define error cases that you are deliberately ignoring; in production quality code asserts should always be replaced with exceptions or other code to actually handle the error case - the assert just says "This shouldn't happen, but I'll just ignore it and carry on".

In practice however we as developers often know in advance that there are things we can assert in our code - we may for example know that a certain function will never be passed a negative number, and therefore add an assert to that effect simply for our peace of mind, which I think is a good thing; code changes over time, and assertions help to catch unexpected changes in behavior.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!