开发者

How to refactor function parameters for this logic: "param 2 is only needed if param 1 is true"?

开发者 https://www.devze.com 2023-04-01 12:30 出处:网络
I have to following functions: public enum class SetMinMaxMode { Auto, // min and max is set based on the channel data

I have to following functions:

public enum class SetMinMaxMode
{
    Auto, // min and max is set based on the channel data
    Manual
};

generic<typename T> where T : System::ValueType
void SetData(array<T>^ data)
{
    SetData(data, SetMinMaxMode::Auto, Double::MinValue, Double::MaxValue);
}

generic<typename T> where T : System::ValueType
void SetData(array<T>^ data, SetMinMaxMode minMaxMode, Double minVal开发者_开发知识库ue, Double maxValue)
{
    //
}   

The parameter minValue and maxValue is only used when parameter minMaxMode is SetMinMaxMode::Manual.

If someone writes function like this:

SetData(data, SetMinMaxMode::Auto, desiredMinValue, desiredMaxValue);  

Then he might not know that the last two parameters are not going to be used.

My questions is, is there any way to refactor so that someone will never need to supply unnecessary parameters?


You could write a function that doesn't take SetMinMaxMode at all.

SetDataAuto(data);

so you either call the one with min/max or you call the one with auto.

Your other function would be:

SetDataManual(data, min, max);
0

精彩评论

暂无评论...
验证码 换一张
取 消