0

I have written a small API to allow users to control a power supply. The PowerController object can switch electrical ports on or off:

PowerController.SwitchOn(1)
PowerController.SwitchOff(3)

The PowerController can toggle the power on four different ports by specifying an integer between 1 and 4. The user can also read back the status of all four ports using:

PowerController.GetPortStatus()

This function returns a Boolean array with a true/false value for each of the 4 ports in it. The users of my API do not like the fact that they must specify a 1 based index to turn ports on or off, but must use a 0 based index to check the status of a given port. What would be the best way of returning the port status so that the user can use the same 1 based index number that they use when turning a port on or off?

3
  • Dont return a boolean array from your method. Defined an class/object to represent the state of your PowerController/Ports Commented Jan 13, 2016 at 12:07
  • 3
    continue using the 0-based index, but subtract or add 1 when displaying to users. That or use enums, which I think makes more sense. Commented Jan 13, 2016 at 12:07
  • There is no such thing as Option Base in C# as it exists in VB. The only way is to decrement/increment the array-index appropriately by 1. Commented Jan 13, 2016 at 12:08

4 Answers 4

1

You could return a Dictionary<int, bool> where the Key is the 1-based index of the port and a Value is the status of the port. This way, the users can use 1-based indexing, like this:

Dictionary<int, bool> portsStatus = PowerController.GetPortStatus();

bool statusOfPort1 = portsStatus[1];
Sign up to request clarification or add additional context in comments.

Comments

0

If I'm grasp your idea, when running this method

PowerController.GetPortStatus()

just return an array, where the index of the array is PORT and Value (0, 1)
is a flag:1- ON, 0 - OFF

example: return new int{-1, 0, 0, 0, 1} (only port 4 is active)

Comments

0

I would provide an alternative to PowerController.GetPortStatus(), say additional static function:

PowerController.IsPortOn(portNumber); // This number is 1-based

This way your users will have a way to access this data in 1-based manner and this will also leave already-written code intact, thus providing backward-compatibility.

Comments

0

You must be consistent with your users ; you can't ask them to use 0-based here and 1-based there.

Whichever you chose doesn't matter, because you should have a class and/or and enum for your different states.

Working with classes and enums is better than boolean and indexes, its more readable, and it's also much more fun.

Or, if you really wanna work with index, decide if it's 0 or 1 based. According to their choices, add/substract 1 to every index so it fits your system.

They want a 1-based index but you have an array of booleans (which is not super-good, I insist), you can let them use their 1-based index, and just substract 1 every time you use it, so it's a 0-based index under the hood. :)

Many solutions work, the last one is very very easy. But I suggest some deeper refactoring like I (and probably others) mentioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.