A class with parameters that have not been set

I am creating a class in C # that will eventually become part of a library that other users can use. The user of this class must set some properties and then use the public method to get the results. What if the user calls the method without setting all the properties? Throw an exception and expect the user to catch it?

thanks

+4
source share
8 answers

Although I do not agree with your design of setting properties, and then called by a method, the best option is to throw an exception from its library. Exceptions are thrown to indicate that something unusual has happened - you can optionally pass a message with an exception that mentions the purpose of the properties before calling this method.

+1
source

It might be better to create a constructor that accepts parameters, so you create an instance of an object that is in a healthy state.

+12
source

Along with all the other options that have been mentioned, you can set default values. Then the user must set them to something else if they do not want the default behavior. But in fact, without knowing more about what your code does, it is hard to say which is the best option.

+4
source

If you cannot (or should not) run your object with a known good state, I would suggest throwing some sort of exception to the caller.

+1
source

If the properties must be necessarily set, then make the necessary constructor parameters and hide the constructor without parameters without parameters.

If not all properties need to be set, you will need to rely on registrations / exceptions to control behavior.

+1
source

Good design makes it difficult to do the wrong thing. Perhaps in this case, the API should take all the inputs as parameters for the method, which is likely to be static then.

0
source

Even if there are 10 properties, as you say, if they are required, and it makes no sense to give them default values, I would say by putting them all in the constructor.

If some of them may have reasonable default values, you can leave them outside the constructor and just set them to default values.

If none of the above is viable, for example, an object may need to be built before the values โ€‹โ€‹for these properties are known, then I will see if it makes sense that the required properties can be passed as parameters to the method (for example , it either does not have many parameters, or cannot be called many times, or if it really responds to its own internal state.)

A ban on all of the above, an exception should be thrown. You also mention "Throw an exception and expect the user to catch it?" The purpose of throwing an exception should not be that you expect the caller to catch him, you should expect them to be sure that the property is set. Catching this should be the last resort when they cannot intelligently verify that the property is set, and they donโ€™t care if your method fails as a result.

0
source

Can you use .NET 4? In this case, the consumer can use named parameters if you want to keep it understandable. So, with properties you would:

new Class { Prop1 = 123, Prop2 = 123, ... }; 

And with named parameters:

 new Class( param1: 123, param2: 123, ... ); 

Also, if you use reference types, you will need to check for null values โ€‹โ€‹anyway. The difference is that with constructor parameters you check once once, while with properties you will have to check every time your method is called.

0
source

Source: https://habr.com/ru/post/1308356/


All Articles