Linked Questions
15 questions linked to/from When should assertions stay in production code?
4439 votes
73 answers
1.5m views
How do I avoid checking for nulls in Java?
I use x != null to avoid NullPointerException. Is there an alternative? if (x != null) { // ... }
273 votes
20 answers
84k views
When should I use Debug.Assert()?
I've been a professional software engineer for about a year now, having graduated with a CS degree. I've known about assertions for a while in C++ and C, but had no idea they existed in C# and .NET at ...
134 votes
14 answers
28k views
Design by contract using assertions or exceptions? [closed]
When programming by contract a function or method first checks whether its preconditions are fulfilled, before starting to work on its responsibilities, right? The two most prominent ways to do these ...
39 votes
6 answers
32k views
Should you assert not null with the assert statement in production code? [closed]
I've seen this question but have a few more questions about the usage of the assert keyword. I was debating with a few other coders about using assert. For this use case, there was a method that can ...
20 votes
11 answers
5k views
Are assertions always bad? [closed]
I used to work for a company where some of the lead architect/developers had mandated on various projects that assertions were not to be used, and they would routinely be removed from code and ...
20 votes
9 answers
15k views
Exception vs. error-code vs. assert
I'm working on a library that generates reports of devices. The generate_report (const std::string& no) member function can fail due to various reasons: invalid report no. invalid state (the ...
20 votes
6 answers
10k views
Library to facilitate the use of the "design by contract" principle [closed]
Is there any library that aids in implementing the design by contract principle in a C++ application? In particular, I'm looking for a library that facilities the usage of the principle, something ...
8 votes
8 answers
13k views
What is the strategy if assertion fails
Assertion is used to check whether a condition is met(precondition, postcondition, invariants) and help programmers find holes during debugging phase. For example, void f(int *p) { assert(p); p-&...
2 votes
8 answers
4k views
C# / Object oriented design - maintaining valid object state
When designing a class, should logic to maintain valid state be incorporated in the class or outside of it ? That is, should properties throw exceptions on invalid states (i.e. value out of range, ...
14 votes
3 answers
8k views
Node.js: should I keep `assert()`s in production code?
A methodological question: I'm implementing an API interface to some services, using node.js, mongodb and express.js. On many (almost all) sites I see code like this: method(function(err, data) { ...
9 votes
1 answer
10k views
Are assert methods acceptable in production?
At work, I ran upon this method and it troubled me. The fact that the name of the method, the documentation and the implementation don't really fit each other aside (and the pass true to make the ...
6 votes
3 answers
7k views
When to use assert() in Matlab?
Since Matlab is interpreted, typically spend a lot of time at the beginning of the function enforcing the function signature. For example if nargin ~= 2; error('must provide two input args a and b');...
-3 votes
1 answer
9k views
fluentassertions C# I Don't understand Should().NotBeNull()
I have read through fluentassertions.com and still confused as to what happens if a var is null or not null. so for instance if you have object someitem = null; then have someitem.Should()....
2 votes
4 answers
919 views
Using assert within methods - Python
is it bad practice to use asserts within methods? e.g. def add(x, y): assert isinstance(x, int) and isinstance(y, int) return x + y Any ideas?
-3 votes
2 answers
374 views
Programme crashes when I try to add data to struct
I have a programme that adds new positions to structure list. Structure definition is: struct data { char name[50]; char surname[50]; float income; float taxed_income; char ...