Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I check the types of constructor arguments (and at other places too)?

Python discourages checking the types. But in many cases this may be useful:

  1. Checking constructor arguments. e.g. checking foe Boolean, string, dict etc. If I don't and set the object's members to the arguments it will cause problems later.

  2. Checking functions arguments.

  3. In properties. If someone sets a wrong value or different type, I should respond quickly.

like image 216
Xolve Avatar asked Mar 02 '09 11:03

Xolve


People also ask

How do you avoid too many parameters in a constructor?

Using a builder can solve the issue of having to many parameters in a constructor when all the fields are not mandatory. It also takes away the problems with having multiple constructors for different purposes. Note that a builder still should have a constructor with the mandatory fields that always needs to be set.

What are arguments in constructor?

It is used to prevent a specific constructor from being called implicitly when constructing an object. For example, without the explicit keyword, the following code is valid C++: Array a = 10; This will call the Array single-argument constructor with the integer argument of 10.

How many arguments can a constructor have in Java?

At the bottom, some of the classes can have up to 30 parameters, 28 of which are just being passed into the super constructor.

Can constructor take any number of arguments?

A constructor having a specific number of parameters(arguments) is called a parameterized constructor. The parameterized constructor is used to provide different values to the objects, you can also provide the same values.


2 Answers

The answer is almost always "no". The general idea in Python, Ruby, and some other languages us called "Duck Typing". You shouldn't care what something is, only how it works. In other words, "if all you want is something that quacks, you don't need to check that it's actually a duck."

In real life, the problem with putting in all those type checks is the inability to replace inputs with alternate implementations. You may check for dict, but I may want to pass something in which is not a dict, but implements the dict API.

Type checking only checks for one of many possible errors in code. For example, it doesn't include range checking (at least not in Python). A modern response to the assertion that there needs to be type checking is that it's more effective to develop unit tests which ensure that not only are the types correct, but also that the functionality is correct.

Another viewpoint is that you should treat your API users like consenting adults, and trust them to use the API correctly. Of course there are times when input checking is helpful, but that's less common than you think. One example is input from untrusted sources, like from the public web.

like image 131
Andrew Dalke Avatar answered Oct 13 '22 02:10

Andrew Dalke


The simple answer is No, use Polymorphism, Exceptions etc.

  1. In the case of constructor arguments being of the wrong type, an exception will be thrown when executing code that depend s on the parameter being of a particular type. If it is a weird, domain specific thing, raise your own Exception. Surround blocks of code which are likely to fail with try-except and handle errors. So it is better to use Exception handling. (Same goes for function arguments)

  2. In properties, the same argument applies. If you are validating the value received, use an assertion to check its range etc. If the value is of the wrong type, it will fail anyway. Then, handle AssertionError.

In Python, you treat programmers as intelligent beings!! Just document your code well (make things obvious), raise Exceptions where appropriate, write polymorphic code etc. Leave the Exception handling(where it is appropriate only)/errors in construction to the client code.

Warning
Leaving Exception handling to clients doesn't mean that you should chuck a lot of garbage errors at the unwitting user. If at all possible, handle exceptions that might occur due to bad construction or any other reason in your code itself. Your code should be robust. Where it is impossible for you to handle the error, politely inform the user/client code programmer!

Note
In general, bad arguments to a constructor isn't something I worry about too much.

like image 29
batbrat Avatar answered Oct 13 '22 02:10

batbrat