Skip to content
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

Button library improvements #23

Merged
merged 10 commits into from
Feb 17, 2021
Merged

Button library improvements #23

merged 10 commits into from
Feb 17, 2021

Conversation

eojreyem
Copy link
Contributor

Readability

I had a hard time following how this worked so...

  • variables were renamed
  • logic was simplified
  • comments were added

update() renamed to listener()

This may be pedantic (but most of programming is?) and I know we already changed it once.
We are not "updating" the button, we are adding a "listener" to a loop in our sketch.
If this is the only thing you're hung up on, I can revert this change.

Removed setup() in favor of doing button setup in the constructor

Previously this was an empty constructor and one would run setup(pin,callback,etc) in the arduino sketch setup function.
I think there are advantages to getting rid of the setup function.

  • declare and define the button right at the top of your sketch.
  • declutter the sketch's setup function
  • modify fewer lines of code to add/remove buttons from a sketch.

An example sketch

Examples>Button>Button.ino
I put a button example sketch in the example folder with my current understanding of best practices.
I updated the readMe file accordingly.
Since Arduino doesn't allow for relative library paths in the include statements, this must be modified to compile. :(
note: pass in a pointer to a function rather than putting a bunch of code in the button constructor (formerly in the sketch's setup function)!

Private variables

All variables were public, which I believe enables some bad coding practices.
If you want to know the state of the button use the function getState(). If you can see a reason to access the other variables involved please let me know and give an example.

@eojreyem eojreyem marked this pull request as ready for review January 20, 2021 20:22
@eojreyem eojreyem added documentation Improvements or additions to documentation enhancement New feature or request labels Jan 20, 2021
@eojreyem eojreyem self-assigned this Jan 20, 2021
@twsdbailey
Copy link
Contributor

I think this looks good, Joe. Pretty easy to follow, and seems easy to implement.

@eojreyem
Copy link
Contributor Author

@sanine-a I'm guessing you are not on board with all of these decisions (private vars, constructor vs setup). Next time we're both in the shop we can discuss this. Not sure if I made my case well above, and you may want to make the case for doing it a different way.

Merging this is not pressing. I'm happy to take the time to discuss changes as this should be something we all agree is useful.

Copy link
Contributor

@brandonwkipp brandonwkipp left a comment

Choose a reason for hiding this comment

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

Hey @eojreyem

This looks like a good improvement over what we had before. Good job on that front! It makes more sense to move all those variables over to private scope and to have the setup function abstracted away like this.

I do have some questions about code styling. The curly bracket styling got changed, which is fine and I feel like I've seen y'all use it regularly. I think it would be good to commit to uniform styling to help make code cleaner and easier to maintain. Would all of you agree this style of curly brackets is how we collectively want to standardize our code in the future:

void func()
{
  // logic
}

The other thing is listener name change. I think that name captures what it is doing more, but if that's the case, we should make a ticket to explicitly rename all of the libraries' update functions to listener.

@twsdbailey
Copy link
Contributor

twsdbailey commented Jan 21, 2021 via email

@tnordberg
Copy link
Member

tnordberg commented Jan 21, 2021

Nice work @eojreyem. I'm curious, is listener() something you've seen somewhere out in the wild?

I think it might be a little misleading to use a noun as the method name. I understand by adding that line of code inside the main loop you are in essence creating a listener, but the function should be named to describe what it does each time it's called (e.g. listener() -> listen()) without assumption of how it will be used.

Also, I think what's nice about update is it signals which method is meant to be called every tick inside the main sketch loop, and I've seen update used commonly in functional programming. I'm fine switching from update to listen, check, loop, or tick... but whatever we land on should become our standard across all the libraries. Curious to hear what @sanine-a thinks too.

@bryankennedy
Copy link
Member

As you reach agreements here please add any notes to our Code Style Guide article.

Please build off of the guides in the community. E.g., https://www.arduino.cc/en/Reference/APIStyleGuide

@eojreyem
Copy link
Contributor Author

Thank you guys for the feedback!

@brandonwkipp {} - That's something that hasn't been on our radar at all down here in the shop. I'm really enjoying using VSCode and using the C/C++ extension you found. I've grown so use to it formatting my code when I save that I now rely on ctrl+S to format rather than typing the indents myself.
I don't know what tools others are using if any. I can talk to Kate and Dave about this.

@tnordberg and @brandonwkipp I think the convention that has informal agreement down in the shop was 'update'. Given Trygve's framing, I guess it isn't as inaccurate as I though. I'm going to revert that back to update.

Copy link
Contributor

@twsdbailey twsdbailey left a comment

Choose a reason for hiding this comment

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

Let's give it a go!

@eojreyem
Copy link
Contributor Author

I'd like to merge this week. @sanine-a do you have any feedback?

@twsdbailey
Copy link
Contributor

twsdbailey commented Feb 17, 2021 via email

@sanine-a
Copy link
Member

My only feedback is that I agree with Brandon that we should standardize our curly bracket style. I'm partial to the brackets being on different lines because I think it helps serve to break up function declarations and their code blocks, and it makes formatting constructor initialization lists a little nicer, but those are purely aesthetic considerations and I'd be perfectly fine if we standardized in the other direction. (I might open an issue on this subject...) This looks great -- I vote merge it!

@twsdbailey
Copy link
Contributor

twsdbailey commented Feb 17, 2021 via email

@bryankennedy
Copy link
Member

@sanine-a added a new issue to discuss the code style items more broadly here: #24

Just linking up in case anyone missed that.

@twsdbailey
Copy link
Contributor

I argue for keeping it on the same line. In most Arduino sample code I've seen, it is on the same line. If we use 3rd party libraries, as we often do, they typically use that formatting.

@eojreyem eojreyem merged commit 969ce34 into master Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

6 participants