Questions tagged [argument-validation]
11 questions
19
votes
8 answers
What is the best practice in case one argument is null?
when validating methods' input, I used to check if the argument is null, and if so I throw an ArgumentNullException. I do this for each and every argument in the list so I end up with code like this:
public User CreateUser(string userName, string…

Ahmed
- 11,063
- 16
- 55
- 67
18
votes
3 answers
C#: Argument validation: null/empty strings
I don't know how many countless times I've had to write code to validate string arguments:
public RoomName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Cannot be empty", "name");
}
}
Is there…

core
- 32,451
- 45
- 138
- 193
6
votes
3 answers
C#: Best practice for validating "this" argument in extension methods
Let's say I have an extension method
public static T TakeRandom(this IEnumerable e)
{
...
To validate the argument e, should I:
A) if (e == null) throw new NullReferenceException()
B) if (e == null) throw new ArgumentNullException("e")
C)…

core
- 32,451
- 45
- 138
- 193
5
votes
5 answers
How to avoid argument validation
Validating Primitive Arguments and "Complex Data"
Validating Arguments
When writing a method, arguments should be validated first before any operations are performed. For example, let's say we've got a class representing people:
public class…

JamesBrownIsDead
- 4,573
- 3
- 20
- 10
3
votes
0 answers
Is there a good way to validate that all inputs to a function have compatible sizes when using a (Repeating) arguments block?
Setup
Let's say I have some function that a number of arrays as an input and does something with them that requires the arrays to have compatible sizes (i.e., to have the same sizes after implicit singleton dimension expansion). I have written a…

Matthew Dvorsky
- 31
- 1
2
votes
1 answer
Can I declare a constructor that accepts Name-Value arguments to define immutable properties?
Since MATLAB R2019b it has been possible to declare name-value arguments from class properties which has created an elegant way to define a class that can be constructed with a declaration of its properties without repeating each defined property in…

Will
- 1,835
- 10
- 21
2
votes
1 answer
Does cross-argument function argument validation not work with (Repeating) arguments?
Summary
When using function argument validation that depends on multiple
arguments in a (Repeating) arguments block, the current argument is passed
to the validation function normally while other arguments are passed as
partially-populated
cell…

Brian61354270
- 8,690
- 4
- 21
- 43
2
votes
1 answer
Argument dependency in argparse
I have test scenario where I need to take action based on arguments passed to python script. The requirement is as follows:
test.py -a a1 -b b1 [[[-c c1] [-d d1]] | [-e e1 -f f1]]
The user has to pass mandatory arguments '-a' and '-b'.
The…

Nilesh Bhave
- 301
- 1
- 12
2
votes
3 answers
Contract.Requires and the Decorator Pattern. How to avoid overchecking conditions?
I currently have a command handling interface that is implemented by a few different classes for different command types. I'm using the Decorator Pattern in conjunction with an IoC container (Unity in my case) to add cross cutting concerns to those…

julealgon
- 7,072
- 3
- 32
- 77
2
votes
2 answers
Argument Checking Library - Arguments for Implicit/Explicit Null Checks
I am currently writing a small argument checking library for Java. Checks are written in a fluent interface way like this:
Check.that(name).matches("hello .*!").hasLenghtBetween(0,…

rolve
- 10,083
- 4
- 55
- 75
0
votes
3 answers
How to respond based on arguments in java constructor
I have a MVC type architecture for new user registration to my app.
The user submits the registration form to the controller servlet. This servlet extracts the user parameters and passes them to a class constructor, which represents (and returns) a…

me_digvijay
- 5,374
- 9
- 46
- 83