Skip to content

default value handling error #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zencuke opened this issue Jul 23, 2018 · 2 comments
Closed

default value handling error #5

zencuke opened this issue Jul 23, 2018 · 2 comments

Comments

@zencuke
Copy link

zencuke commented Jul 23, 2018

All through begin() there are a series of case statements. In most of then a comment suggests that the author thinks the default switch case somehow relates to the application default value. In this code the default case covers user errors, when the user sets the switch variable to an unsupported value. That is not user friendly behavior. The code should either throw an error so the user can see and fix the problem or attempt to set something close to the user requested value.

For example accelSampleRate. Accepted values are 13, 26, 52 etc; power of 2 multiples of 13. If the user asks for 14, 21 or any other value not in the set they will silently get 104 Hz because that is the default case value for the switch, which of course makes no sense. That may be a reasonable value if the user doesn't specify a value which may be what the coder thought would happen but that is controlled by the initial (default) value of settings.accelSampleRate which happens to be 416. So it didn't even get the default value right. 104 is just a random meaningless value.

The user friendly options when the user asks for an unsupported value would be either:

  1. Throw an error, print a message, do something so the user can see and fix the error.
  2. Pick the supported value closest to what the user asked for.

I would prefer 1) but 2) is more beginner friendly.

Masking the error by moving forward with a randomly selected value (randomly selected by the coder) is not user friendly. At the very least it should be consistent and default to the actual default value the user would get by not specifying any value. Now we have 3 user cases, the user doesn't specify and gets the default of 416, the user asks for a supported value and gets it, or the user makes a typo, asks for an unsupported value, and gets 104.

@zencuke
Copy link
Author

zencuke commented Jul 23, 2018

I might have coded this one as "switch(((settings.accelSampleRate/13)+1)*13)" which sort of uses the floor value.

oclyke added a commit that referenced this issue Mar 13, 2019
This completes the handling of issue #5, started with commits 32aca21 and 159af65.

Instead of returning the actual values through the pointer I changed to returning the values that the user wanted through the pointer. Then the actual .settings member of the object is modified to reflect the current settings.

There is now an example to illustrate how to check if the settings have been chnged by the 'begin' method.
@oclyke
Copy link
Contributor

oclyke commented Mar 13, 2019

Hi @zencuke. Thanks for the issue and the suggestion, definitely helped to improve this code! I leaned toward option 1 where we allow the user to see if anything has changed. There is now an optional argument to the 'begin()' function that expects a pointer to a SensorSettings structure. That pointer will be filled out to match the '.settings' member of the IMU object when you called begin(), and then the .settings will be changed to reflect what was actually used in the default case. You can then compare the .settings fields with the fields in the pointed to structure as shown in an example.

If there's anything more you'd like say about this please feel free to re-open this issue.

Edit: commit 88488f9 addressed this issue

@oclyke oclyke closed this as completed Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants