preconditions

preconditions and exceptions

Suppose you have a method with some pre and post-conditions. Is it ok to create an exception class for each pre-condition that is not accomplished? For example: Not accomplishing pre1 means throwing a notPre1Exception instance. ...

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public function, a member function will look like this: edit: (cleaned up my example) void Charcoa...

Binary Searching

Hi So, I want to understand more about binary searching, cause I don't really understand. Binary search requires a precondition that an array is sorted. I got that right? It seems like a method should check this precondition and throw an exception if it is not met. But, why is checking the precondition a bad idea? ...

how do i explain that if (xyz == null) checks are not "protective".

i have a few developers who constantly put If null checks For example: Run(Order order) { if (order == null) return; } in their code as they think they are protecting their class if someone passes in a parameter that is null. I am trying to tell them the flaw in their logic because if someone is passing in null in this case, its m...

Slow Scala assert

We've been profiling our code recently and we've come across a few annoying hotspots. They're in the form assert(a == b, a + " is not equal to " + b) Because some of these asserts can be in code called a huge amount of times the string concat starts to add up. assert is defined as: def assert(assumption : Boolean, message : Any) = .....

Null check error message as "is null" or "was null"

When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use? We tend to use something like this public User getUser(String username){ if (username == null){ throw new IllegalArgumentException("username is null"); } // ... } What is better : "i...

Precondition or preconditions?

Guys what is the formal name PRECONDITION or PRECONDITIONS that fnc must satisfy in order to work correctly? ...

How to generate random numbers of lognormal distribution within specific range in Matlab

My grain sizes are defined as D=[1.19,1.00,0.84,0.71,0.59,0.50,0.42]. The problem is described below in steps. Grain sizes should follow lognormal distribution. The mean of the grain sizes is fixed as 0.84 and the standard deviation should be as low as possible but not zero. 90% of the grains (by weight %) fall in the size range of 1....

What is the proper error message to supply to Google Guava's Preconditions.* methods?

For example when using Preconditions.checkArgument, is the error message supposed to reflect the passing case or the failing case of the check in question? import static com.google.common.base.Preconditions.*; void doStuff(int a, int b) { checkArgument(a == b, "a == b"); // OR checkArgument(a == b, "a != b"); } ...