Questions tagged [preconditions]

A precondition is some condition which must be satisfied before running a given section of code. It may be specified in code via assertions, or in the documentation.

If a precondition is violated, errors or security issues may occur. A precondition states what is required for the section of code to run correctly.

Links

Related tags

166 questions
123
votes
7 answers

ReSharper Curiosity: "Parameter is only used for precondition check(s)."

Why is ReSharper judging me for this code? private Control GetCorrespondingInputControl(SupportedType supportedType, object settingValue) { this.ValidateCorrespondingValueType(supportedType, settingValue); …
Corpsekicker
  • 3,276
  • 6
  • 25
  • 34
75
votes
3 answers

What's the point of Guava checkNotNull

I'm pretty new to Guava (let's be honest, I'm not "pretty new", I'm a complete rookie on that subject) and so I decided to go through some documentation and got quite amazed while reading…
Ar3s
  • 2,237
  • 2
  • 25
  • 47
21
votes
4 answers

Precondition functions in Kotlin - good practices

Being a newbie Kotlin coder, I wonder, if there are some good practices or even language constructs for declaring pre-conditions in functions. In Java I have been using Guava's Preconditions checking…
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
17
votes
2 answers

Pros/cons of different methods for testing preconditions?

Off the top of my head, I can think of 4 ways to check for null arguments: Debug.Assert(context != null); Contract.Assert(context != null); Contract.Requires(context != null); if (context == null) throw new ArgumentNullException("context"); I've…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
17
votes
3 answers

shared_ptr that cannot be null?

Using a std::shared_ptr expresses shared ownership and optionality (with its possibility to be null). I find myself in situations where I want to express shared ownership only in my code, and no optionality. When using a shared_ptr as a function…
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
14
votes
3 answers

Liquibase preconditions: How do I check for a column being non-nullable?

I have a db upgrade script to remove the non-null constraint on a column. I want to do a precondition check, and call ALTER TABLE only when it is non-null. The master.xml script is a progressive one where I keep adding scripts and the entire thing…
ashes
  • 631
  • 1
  • 6
  • 9
12
votes
4 answers

Checking preconditions in .NET

I'm a fan of the "fail early" strategy and want to check that methods params have correct values for example. In Java I'd use something like Guava: checkArgument(count > 0, "must be positive: %s", count); Is there something similar for .NET?
deamon
  • 89,107
  • 111
  • 320
  • 448
12
votes
1 answer

Testing Swift code with preconditions

How do you write tests for Swift methods that have preconditions? Here is an example: func doublePositive(n:Int) -> Int { precondition(n >= 0) return 2*n } Using XCTAssertThrowsError does not work: func testDoublePositive() { …
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
12
votes
5 answers

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) { …
Brian Harris
  • 2,735
  • 3
  • 22
  • 34
12
votes
4 answers

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…
David
  • 1,862
  • 2
  • 22
  • 35
11
votes
4 answers

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…
Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
11
votes
5 answers

liquibase preconditions yaml

Is it possible to use Precondition in YAML i didn't find any sources except this page http://www.liquibase.org/documentation/yaml_format.html But I am looking for the equivalent of :
Cifren
  • 364
  • 1
  • 4
  • 15
10
votes
5 answers

Can't weakening preconditions and strengthening postconditions also violate Liskov Substitution Principle?

Actual precondition of a subtype is created by combining ( using logical OR ) preconditions of a base type and preconditions of a subtype, which makes the resulting precondition less restrictive Actual postcondition of a subtype is created by…
7
votes
4 answers

A good assert class for production use? Java's equivalent of Groovy's PowerAssert?

I don't like the java assert keyword, because it is not always enabled in production code. I am looking for a good "ProductionAssert" class to use, that always runs the noted assertions. One candidate is Guava's Preconditions. It's decent, but a bit…
ripper234
  • 222,824
  • 274
  • 634
  • 905
7
votes
3 answers

Liquibase preconditions: How do I check for a column being the correct data type?

I have a db upgrade script to change some datatypes on a few columns. I want to do a preCondition check, and call ALTER TABLE only when it is a DECIMAL datatype, but I will want it to be changed to INTEGER. Couldn't find a predefined precondition…
Ben
  • 71
  • 1
  • 2
1
2 3
11 12