Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Say I have something like this:

public class BaseClass
{
    public BaseClass(string someString)
    {
        if(someString == null)
            throw new ArgumentException();
    }
}

public class ChildClass : BaseClass
{
    public ChildClass(string someString)
        : base(someString)
    {
        // Should I do this??
        if(someString == null)
            throw new ArgumentException();
    }
}

Also, what if I'm inheriting a class that I do not have the source for. Should I recheck constructor arguments?

share|improve this question
    
Not a fan of throwing these types of exception within the ctor; Eric has a great write up on it. –  Aaron McIver Jun 18 '13 at 22:37
    
The article doesn't really cover exceptions in the constructor. I agree completely with avoiding "vexing" exceptions, but there are certainly instances where an exception in the constructor makes sense. For instance, imagine the string in this constructor is a database connection string. –  ConditionRacer Jun 19 '13 at 13:23
1  
@AaronMcIver It depends on the purpose and design approach you're taking. I like to follow a rule that after construction an object is useful, so the constructor(s) require everything that would make the object in a valid usable state (a second stage open call is sometimes meritted but the constructor should still require the info necessary for the open call to be made). That said, object validation should be done at construction in those instances, and only by exceptions can you stop a new call from getting an object (which would be invalid and unusable if improper arguments are given) –  Jimmy Hoffa Jun 19 '13 at 13:53
1  
@AaronMcIver though, again it depends on the purpose. DTOs should never do this in the general case, this is usually only meritted when the object being constructed will work with some form of resource. Imagine for instance a class which needs a repository being given a null for the IAccountRepository in it's constructor; the resulting object will be useless as any call would simply dereference the repository member it relies on, so no point in allowing construction to complete without a valid repository since the resultant object would be unusable. –  Jimmy Hoffa Jun 19 '13 at 13:56

1 Answer 1

up vote 7 down vote accepted

No. If your base class validates something you don't have to. As for your second question: Yes and No. Yes if you're going to use the argument, no if only the base class uses the argument.

It's the base classes job to be sure that value is good enough for its purpose, you can't however expect the base class to ensure the value is good enough for your purpose. For all you know the base class is completely happy with a null value and can function fine in that scenario when you can't.

Also when null checking your strings, you usually want string.IsNullOrWhitespace(someString) or string.IsNullOrEmpty(someString) depending on whether or not " " is valid, or "" is valid, or neither.

share|improve this answer
    
The string.IsNull, etc.. is language specific and merely syntactic sugar. Not sure that it adds anything to what the OP is questioning. –  Aaron McIver Jun 18 '13 at 22:41
    
@AaronMcIver I could be mistaken, but in his particular question he's referring to C#, as for adding to the OP's question sure, it may not, but if it helps him as an aside while the rest of my answer actually answers his question (and the question for future readers) all the better, right? –  Jimmy Hoffa Jun 18 '13 at 22:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.