assertions

Do you use assertions? [closed]

走远了吗. 提交于 2019-11-29 02:18:45
问题 This is not really a "question" so I'm making it CW. The assert Keyword is great! It should make, feel your self more confident with the code you wrote, but, until today when I was creating a small test class ( < 20 lines ) I realize a never use it since it was introduced. Heck! I barely use logger which is very useful indeed, but it wasn't until today I realize I don't use assertions. Do you use assertions? If no, what's the reason? 回答1: I was taught to use lots of assertions back in the 90s

Debug.Assert vs Exceptions

♀尐吖头ヾ 提交于 2019-11-28 18:49:30
Surprisingly I was only able to find one previous question on SO about this subject, and I'd just like to get the community "Vote of Confidence" (or not!) on my approach. The way I see it is thus: use Debug.Assert to state things you EXPECT would be true. This would be used when we are in complete control over our environment, for example in a method to verify some pre and post-conditions. use Exceptions when exceptional circumstances arise. Dealing with external resources, i.e. files, databases, networks etc is a no-brainer. But... It gets a little murky in the following scenario. Please note

what is the difference between triggers, assertions and checks (in database)

一曲冷凌霜 提交于 2019-11-28 15:51:19
问题 Can anybody explain (or suggest a site or paper) the exact difference between triggers, assertions and checks, also describe where I should use them? EDIT: I mean in database, not in any other systems or programing languages. 回答1: Triggers - a trigger is a piece of SQL to execute either before or after an update, insert, or delete in a database. An example of a trigger in plain English might be something like: before updating a customer record, save a copy of the current record. Which would

Debug.Assert vs Exception Throwing

Deadly 提交于 2019-11-28 15:14:29
I've read plenty of articles (and a couple of other similar questions that were posted on StackOverflow) about how and when to use assertions, and I understood them well. But still, I don't understand what kind of motivation should drive me to use Debug.Assert instead of throwing a plain exception. What I mean is, in .NET the default response to a failed assertion is to "stop the world" and display a message box to the user. Though this kind of behavior could be modified, I find it highly annoying and redundant to do that, while I could instead, just throw a suitable exception. This way, I

Q_ASSERT release build semantics

主宰稳场 提交于 2019-11-28 07:17:06
问题 I can't find a clear statement on the semantics of Q_ASSERT under release builds. If there is no assertion checking, then is the asserted expression evaluated at all? Consider the following code Q_ASSERT(do_something_report_false_if_failed()); Will do_something_report_false_if_failed() run under all potential Qt build configurations? Would it be safer (even though a bit more verbose and less readable) to do this instead: bool is_ok = do_something_report_false_if_failed(); Q_ASSERT(is_ok) The

How to change the message in a Python AssertionError?

可紊 提交于 2019-11-28 04:51:23
I'm writing per the following, in which I try to produce a decent error message when comparing two multiline blocks of Unicode text. The interior method that does the comparison raises an assertion, but the default explanation is useless to me I need to add something to code such as this below: def assert_long_strings_equal(one, other): lines_one = one.splitlines() lines_other = other.splitlines() for line1, line2 in zip(lines_one, lines_other): try: my_assert_equal(line1, line2) except AssertionError, error: # Add some information to the printed result of error??! raise I cannot figure out

Enabling assertions in Netbeans

。_饼干妹妹 提交于 2019-11-28 00:42:46
I wanna do something like java -enableassertions com.geeksanonymous.TestClass How do I do this? The easiest way is to use the Run properties. The property is labeled 'VM Options'. This tutorial has more detailed info and screen shots from NetBeans 5.5. The dialog is very similar in the most recent release of NetBeans; 6.8, which is available today. With a Maven project in netbeans 7.0, choose "File" -> "Project Properties ()". In the window that appears, choose "Run", and add -enableassertions to the "VM Options" textbox. I dont know about Netbeans, but you also can programmatically enable

Identifer versus keyword

风格不统一 提交于 2019-11-27 22:23:23
问题 I read in the book for OCJP for Java6 the part with assertions. I reached the part where it gives me an overview of how the compiler reacts if the word 'assert' is used as keyword or as an identifier. What is the difference between a Keyword and an identifier ? Can anyone give me a simple explanation and additionally one or more examples for both? 回答1: The terms "keyword" and "identifier" are not Java specific. A keyword is a reserved word from the Java keyword list provide the compiler with

Should one override equals method for asserting the object equality in a unit test?

放肆的年华 提交于 2019-11-27 22:07:41
Let's say we are testing the result of a method by asserting the equality of all the properties of the result object with properties of an expected result object. Should we implement equals method and use Assert.AreEqual(expectedResult, actualResult)... But equals may mean something different in production code. Which is the best practice? Asserting the equality of the objects through overriden equals method or Asserting the equality of all the properties I for one use custom assertions. There are two main reasons: don't force test concerns into production. This means that the meaning of

Avoiding unused variables warnings when using assert() in a Release build

本小妞迷上赌 提交于 2019-11-27 19:57:48
Sometimes a local variable is used for the sole purpose of checking it in an assert(), like so - int Result = Func(); assert( Result == 1 ); When compiling code in a Release build, assert()s are usually disabled, so this code may produce a warning about Result being set but never read. A possible workaround is - int Result = Func(); if ( Result == 1 ) { assert( 0 ); } But it requires too much typing, isn't easy on the eyes and causes the condition to be always checked (yes, the compiler may optimize the check away, but still). I'm looking for an alternative way to express this assert() in a