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
7
votes
5 answers

Scala avoid using null

I hava a project on github that is analysed by codacy . The analysis suggest to "Avoid using null" for the following line of code: def doSomethingWithPath(path:Path) = { require(path != null, "Path should not be null") //<-to avoid …
raisercostin
  • 8,777
  • 5
  • 67
  • 76
7
votes
7 answers

Preconditions library to throw IllegalArgumentException for notNull check

Do you know some nice alternative to Apache Commons Validate or Guava Preconditions that would throw IllegalArgumentException instead of NullPointerException when checking if object is not null (except Spring Assert)? I'm aware that Javadocs…
makasprzak
  • 5,082
  • 3
  • 30
  • 49
7
votes
5 answers

How do I insert a precondition in a java class method or constructor?

This is for a java class I'm taking. The book mentions preconditions and postconditions but doesn't give any examples how to code them. It goes on to talk about asserts, I have that down, but the assignment I'm doing specifically states to insert…
Jay Rohrssen
  • 243
  • 1
  • 2
  • 9
6
votes
3 answers

What are preconditions and postconditions?

I'm learning how to program but one thing I can't quite get my head around is preconditions and postconditions. Is an if statement before calling a function considered a precondition, or is there a separate more efficient way of doing this in most…
6
votes
1 answer

Is grouping preconditions into a method acceptable to stay DRY?

When writing preconditions for different functions with similar parameters, I want to group assertions or exceptions into a static method, rather than writing them out explicitly. For example, instead of GetFooForUser(User user) { assert(null !=…
Jens Ehrich
  • 573
  • 1
  • 6
  • 19
6
votes
2 answers

Validate function preconditions in android

Usually when writing a public method I do some error checking e.g. public SomeResult processSomething (int i, List items) { if( i < 0 ) { throw new IllegalArgumentException(); } if(items == null) { throw new…
Jim
  • 18,826
  • 34
  • 135
  • 254
6
votes
5 answers

Custom exception with Guava Preconditions

It's very simple question, I often use com.google.common.base.Preconditions in my projects to validate arguments and parameters, for example: Preconditions.checkNotNull(parameter, "message"); Preconditions.checkArgument(parameter > 0,…
maxi
  • 369
  • 5
  • 14
5
votes
5 answers

API Design - Mixing in pre-condition checks for index out of bounds?

I am designing an API that does something like this: // Drop $howMany items from after $from def dropElements[T](array: Array[T], from: Int, howMany: Int) The expected behaviour is that howMany should be non-negative and if howMany is zero, it…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
5
votes
1 answer

How to fix 412 (Precondition Failed) Error HTML5 Video Tag

I am using HTML5 video tag inside FlexSlider. Sometimes video stops working. After lot of search I got this error. GET http://studiobooth.local/app/videos/0062mParticle12151601.mp4 412 (Precondition Failed) Here is my HTML5 Video tag code:
Tanmay Vats
  • 409
  • 1
  • 5
  • 17
4
votes
3 answers

When to add a precondition and when to (only) throw an exception?

I am learning about preconditions and when to use them. I have been told that the precondition @pre fileName must be the name of a valid file does not suit in the following code: /** Creates a new FileReader, given the name of file to read…
Datoraki
  • 1,223
  • 13
  • 26
4
votes
2 answers

Are there any plans to add "expects" to std::optional?

This topic is controversial, but I'm in the camp which believes that preconditions and class invariants should be guarded by asserts which terminate the program if the contract of the corresponding SW component is violated - as long as the runtime…
B0rk4
  • 241
  • 1
  • 9
4
votes
3 answers

Pre and Post Condition from Stroustrup's book

In chapter 5.10.1 of Programming: Principles and Practice using C++, there is a "Try this" exercise for debugging for bad input of an area. The pre-conditions are if the the inputs for length and width are 0 or negative while the post-condition is…
mcbalsa
  • 51
  • 5
4
votes
3 answers

Is there a performance impact when using Guava.Preconditions with concatenated strings?

In our code, we often check arguments with Preconditions: Preconditions.checkArgument(expression, "1" + var + "3"); But sometimes, this code is called very often. Could this have a notable negative impact on performance? Should we switch…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
4
votes
2 answers

Why Does Checkers in Google Precondition Library take object in stead of a String

Why does check* methods in Google Precondition library take an Object instead of a String? I can see that the object is called String.valueOf() on. I think this design was due to not making any assumption on behalf of the client. But I cannot think…
Nilanjan Basu
  • 828
  • 9
  • 20
4
votes
1 answer

Is it possible to use Prismatic schema.core/maybe in a Clojure function precondition?

I'm trying to use Prismatic schema.core/maybe in a precondition for a function taking an optional argument opts, but it seems to always throw an AssertionError when I call the function with no opts: (require '[schema.core :as schema]) (defn foo [&…
Josh Glover
  • 25,142
  • 27
  • 92
  • 129
1
2
3
11 12