Skip to content

system/settings: general code cleaning #3110

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

Merged
merged 2 commits into from
Jun 26, 2025

Conversation

jeanthom
Copy link

@jeanthom jeanthom commented Jun 25, 2025

Summary

  • Remove duplicate checks, use switch/case whenever possible
  • Remove assertions just before if () condition checking the exact same thing
  • Replace if (condition) assert(0) with assert(condition)

Impact

No impact on users.

Testing

Ran the example "settings" app using sim:nsh NuttX 12.9.0 configuration.

Before:

NuttShell (NSH) NuttX-12.9.0
nsh> settings
Example of settings usage to file: /tmp/settings.bin and /tmp/settings.txt:--------------------------------------------------------------
No existing binary storage file found. Creating it.
No existing text storage file found. Creating it.
Retrieved settings value (v1) with value:default value
Trying to (re)create a setting that already exists (v1)
Retrieved setting type is: 4
Trying to change setting (v1) to integer type
Deliberate fail: settings change invalid: -13
Deliberate fail: non-existent setting requested. Error:-2
Trying to change setting (v1) from int to string: I'm a string
Creating a string settings value (s1):I'm a string
Retrieved string settings value (s1) with value:I'm a string
Changing setting to an IP value (s1) with value:192.168.100.1
Retrieved IP address settings value (s1) with value:0xc0a86401
syncing storages
exiting settings example app

After:

NuttShell (NSH) NuttX-12.9.0
nsh> settings
Example of settings usage to file: /tmp/settings.bin and /tmp/settings.txt:--------------------------------------------------------------
No existing binary storage file found. Creating it.
No existing text storage file found. Creating it.
Retrieved settings value (v1) with value:default value
Trying to (re)create a setting that already exists (v1)
Retrieved setting type is: 4
Trying to change setting (v1) to integer type
Deliberate fail: settings change invalid: -13
Deliberate fail: non-existent setting requested. Error:-2
Trying to change setting (v1) from int to string: I'm a string
Creating a string settings value (s1):I'm a string
Retrieved string settings value (s1) with value:I'm a string
Changing setting to an IP value (s1) with value:192.168.100.1
Retrieved IP address settings value (s1) with value:0xc0a86401
syncing storages
exiting settings example app

* Remove duplicate checks, use switch/case whenever possible
* Remove assertions just before if () condition checking the exact
  same thing
* Replace if (condition) assert(0) with assert(condition)

Signed-off-by: Jean THOMAS <jean@lambdaconcept.com>
Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jeanthom :-)

  • Please take a look at my notes in the code to make sure all will work as expected after change :-)
  • There is a slight behavioral change introduced for instance no asserts but error code return which seems okay :-)
  • How did you verify the change? Could you please provide testing logs from a real hardware before and after?

@jeanthom
Copy link
Author

Hi @cederom and thanks for your review. I applied the required suggestions to the code. Regarding the behavioral change, I think it is sensible: I was always told asserts should be used to verify internal state of a program whereas error code should be preferred to notify an issue with user input.

Regarding tests: ran the example "settings" app using sim:nsh NuttX configuration.

Before:

NuttShell (NSH) NuttX-12.9.0
nsh> settings
Example of settings usage to file: /tmp/settings.bin and /tmp/settings.txt:--------------------------------------------------------------
No existing binary storage file found. Creating it.
No existing text storage file found. Creating it.
Retrieved settings value (v1) with value:default value
Trying to (re)create a setting that already exists (v1)
Retrieved setting type is: 4
Trying to change setting (v1) to integer type
Deliberate fail: settings change invalid: -13
Deliberate fail: non-existent setting requested. Error:-2
Trying to change setting (v1) from int to string: I'm a string
Creating a string settings value (s1):I'm a string
Retrieved string settings value (s1) with value:I'm a string
Changing setting to an IP value (s1) with value:192.168.100.1
Retrieved IP address settings value (s1) with value:0xc0a86401
syncing storages
exiting settings example app

After:

NuttShell (NSH) NuttX-12.9.0
nsh> settings
Example of settings usage to file: /tmp/settings.bin and /tmp/settings.txt:--------------------------------------------------------------
No existing binary storage file found. Creating it.
No existing text storage file found. Creating it.
Retrieved settings value (v1) with value:default value
Trying to (re)create a setting that already exists (v1)
Retrieved setting type is: 4
Trying to change setting (v1) to integer type
Deliberate fail: settings change invalid: -13
Deliberate fail: non-existent setting requested. Error:-2
Trying to change setting (v1) from int to string: I'm a string
Creating a string settings value (s1):I'm a string
Retrieved string settings value (s1) with value:I'm a string
Changing setting to an IP value (s1) with value:192.168.100.1
Retrieved IP address settings value (s1) with value:0xc0a86401
syncing storages
exiting settings example app

@jeanthom jeanthom requested a review from cederom June 25, 2025 15:34
@acassis
Copy link
Contributor

acassis commented Jun 25, 2025

@TimJTi since you are the original author, please take a look too!

@TimJTi
Copy link
Contributor

TimJTi commented Jun 25, 2025

@TimJTi since you are the original author, please take a look too!

Actually - I didn't write it but did "port" it to NuttX and am using it regularly. Once it is reviewed by others and "ready to merge" give me the heads up and all merge the PR in to my code and make sure it works OK for me still. Or are we there now?

The original author was very keen I messed with as little as possible when I was making it "suitable for NuttX", and got me to "undo" things I'd changed (perhaps including better use of select/case rather than if/elseif/else etc!!! Can't please everybodt all the time LOL.

As far as assert vs error code: having both means that if NDEBUG is set, disabling asserts, there are still error messages, but the program doesn't "crash" as such.

I have seen this used in quite a few places and it seemed a good idea at the time, and is "benign"; but if its frowned upon here, so be it - although nobody complained during the reviews when I submitted the PR!!

Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jeanthom for all the updates and feedback, just one bracket inside case and we are ready to go :-)

@jeanthom jeanthom force-pushed the system-settings-cleanup branch from ca22f54 to bdcf141 Compare June 26, 2025 09:16
@jeanthom jeanthom requested review from cederom and TimJTi June 26, 2025 09:16
@TimJTi
Copy link
Contributor

TimJTi commented Jun 26, 2025

@acassis @jerpelea I have been asked to review this, which I would be delighted to do!

Can either of you - or anyone else - suggest an efficient way to do this with some tips, so I don't waste tons of time doing things the hard way?

Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jeanthom :-)

@cederom
Copy link
Contributor

cederom commented Jun 26, 2025

@TimJTi: @acassis @jerpelea I have been asked to review this, which I would be delighted to do!

Can either of you - or anyone else - suggest an efficient way to do this with some tips, so I don't waste tons of time doing things the hard way?

Tim you can clone the source repo switch to branch that constitutes this PR build and see if that works for you :-) If you have existing application that uses settings you may want to build that too and see if anything is broken and works as expected - all should work fine with your existing application. Thank you :-)

I have dedicated nuttx/pr/nuttx.git and nuttx/pr/nuttx-apps.git clones where I add remotes to be tested, here is an example:

  1. mkdir nuttx-pr - create pr test workbench.
  2. cd nuttx-pr - go to pr test workbench.
  3. git clone https://github.com/apache/nuttx.git nuttx - clone nuttx repo.
  4. git clone https://github.com/apache/nuttx-apps.git - clone nuttx-apps repo.
  5. cd nuttx-apps - go to cloned apps repo.
  6. git remote add lambdaconcept https://github.com/lambdaconcept/nuttx-apps.git - add remote where pr source resides.
  7. git fetch --all - get changes from all remotes.
  8. git checkout lambdaconcept/system-settings-cleanup - switch to branch that sources this PR.
  9. you are ready to test :-)

@TimJTi
Copy link
Contributor

TimJTi commented Jun 26, 2025

@TimJTi: @acassis @jerpelea I have been asked to review this, which I would be delighted to do!
Can either of you - or anyone else - suggest an efficient way to do this with some tips, so I don't waste tons of time doing things the hard way?

Tim you can clone the source repo switch to branch that constitutes this PR build and see if that works for you :-) If you have existing application that uses settings you may want to build that too and see if anything is broken and works as expected - all should work fine with your existing application. Thank you :-)

I have dedicated nuttx/pr/nuttx.git and nuttx/pr/nuttx-apps.git clones where I add remotes to be tested, here is an example:

  1. mkdir nuttx-pr - create pr test workbench.
  2. cd nuttx-pr - go to pr test workbench.
  3. git clone https://github.com/apache/nuttx.git nuttx - clone nuttx repo.
  4. git clone https://github.com/apache/nuttx-apps.git - clone nuttx-apps repo.
  5. cd nuttx-apps - go to cloned apps repo.
  6. git remote add lambdaconcept https://github.com/lambdaconcept/nuttx-apps.git - add remote where pr source resides.
  7. git fetch --all - get changes from all remotes.
  8. git checkout lambdaconcept/system-settings-cleanup - switch to branch that sources this PR.
  9. you are ready to test :-)

Thanks @cederom

And I see nicely "quoted" added/deleted lines that are then commented on (and in the past I have seen suggested changes that can be accepted here). Is that done manually from the git change log or is there a "trick" to that too?

@cederom
Copy link
Contributor

cederom commented Jun 26, 2025

@TimJTi: And I see nicely "quoted" added/deleted lines that are then commented on (and in the past I have seen suggested changes that can be accepted here). Is that done manually from the git change log or is there a "trick" to that too?

If you are talking about discussion here on GitHub regarding specific lines changed then this is part of the PR processing interface on GitHub website :-) You go to "files changed" top tab under PR, then when you point to a line number either on the - or + side you can mark specific line or lines range where comments can be added, these comments then show up as discussion here :-)

@acassis
Copy link
Contributor

acassis commented Jun 26, 2025

@TimJTi normally I do this way: https://acassis.wordpress.com/2022/02/24/how-to-test-a-pr-before-it-get-integrated-on-nuttx/

Copy link
Contributor

@TimJTi TimJTi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much neater than the original that I worked from! Nice work 👍

Nothing broken that I've found with brief testing in my project- I can raise a new issue or PR if I do find anything not right in the future but it looks to be good to go.

@acassis acassis merged commit c7b395e into apache:master Jun 26, 2025
39 checks passed
@acassis
Copy link
Contributor

acassis commented Jun 26, 2025

Thank you @jeanthom !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants