Questions tagged [assertions]
Assertions enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.
49 questions
0 votes
1 answer
272 views
Is interleaving local variable declarations with assertions and function calls a bad practice?
In my experience, it is customary to place local variable declarations at the beginning of their scope. Several questions in this forum ask whether this needs to be so, and their answers tend to agree ...
20 votes
8 answers
9k views
Why assert for null on a object before asserting on some of its internals?
Let's consider the following test. [Fact] public void MyTest() { // Arrange Code var sut = new SystemWeTest(); // Act Code var response = sut.Request(); // Assert ...
-1 votes
2 answers
1k views
Assert same and equals in unit test
I have a function to be tested fn doNothing(Student student) { //do some other operations here. but student is unmodified return student; } And my unit test is var student = new Student("...
6 votes
5 answers
8k views
Assertion statements: Remove or retain in production code
I'm working in a large company and am doing my first Java project here. We have certain code style guidelines. In the Java guideline, it is said that "assertions are only intended for debugging ...
7 votes
5 answers
2k views
Is it a good idea to start a function with a bunch of assert statements?
Sometimes when I look at other people's code I see functions that make a bunch of assumptions about the inputs but do not explicitly assert their assumptions. For example, look at the code below: def ...
4 votes
4 answers
506 views
Design pattern: method that checks consistency of object's internal state
I have got a project in C++. Each of my classes has a method void assert_good() const. The usage of that method is solely for debugging purposes: if the object is an inconsistent/impossible internal ...
7 votes
4 answers
1k views
Should I use assertions in a function that calls a function that have already them?
Let F(x) be a function that calls G(x), in which x must be greater than 0. If G already does assert(x > 0), should F do it as well?
1 vote
2 answers
34 views
Dealing with assumptions in modules that result in duplicate work
Imagine a game that needs to score words. A words needs to be scored immediately, in a list of words or even in a list of words from list of players. I've created a scoring module which has the ...