Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added duration variable to Blink.ino#20

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
per1234 wants to merge1 commit intomainfrompr_10648
Closed

Conversation

@per1234
Copy link
Contributor

Moved fromarduino/Arduino#10648 by@JeffB42

Added duration variable to the Blink.ino sketch. This change has the following benefits:

  1. Makes it easier to change the delay time and aids in troubleshooting
  2. Demonstrates good programming habits to beginner programmers by not using "magic numbers" or redeclaring the same value multiple times
  3. Can be used in a beginner "how to program" tutorial - open Blink,ino, then walk the reader/student through changing the value of duration, compile and run, then change the value again, compile and run, and notice the change, etc...

@per1234
Copy link
ContributorAuthor

From @harryboo on 2020-09-09

Please declare the variable outside of the function 'loop'.

@per1234
Copy link
ContributorAuthor

From @JeffB42 on 2020-09-10

Please declare the variable outside of the function 'loop'.

This is a coding style preference, and I'd prefer to keep it as it is. Is there a style guide that's been published by the Arduino team? That's not meant to be a snarky response, or defensive - I just would genuinely like to know if there is a style guide that's been published by the Arduino team, and if there is, if it states that variables be declared globally and at the top of the file.

This is a coding style preference, and I prefer this convention for the following reasons:

  1. The variable/const is close to where it is used, and you don't need to go hunting around to find where it's declared, what type it is, and what it's value is.
  2. Keeping the variable within the function doesn't add a global variable and doesn't pollute the global scope.
  3. This convention lends itself well for refactoring. Since The variable is only used in the one location (and that's the sole intention), the contents are self-contained and can easily be refactored into a new function (whether in the same file, or in a different file).

I've worked at several companies in my career, and all companies (or at least the recent ones that I remember) used this coding style. I did some googling, and found these discussions:

In Java, should variables be declared at the top of a function, or as they're needed?
https://stackoverflow.com/questions/1411463/in-java-should-variables-be-declared-at-the-top-of-a-function-or-as-theyre-ne

Declaring variables inside or outside of a loop
https://stackoverflow.com/questions/8803674/declaring-variables-inside-or-outside-of-a-loop

Why declare variables close to where they are used?
https://softwareengineering.stackexchange.com/questions/113262/why-declare-variables-close-to-where-they-are-used

@ubidefeo
Copy link
Contributor

@per1234
I strongly believe that this example should not contain any variables, since being the "hello world" of microcontrollers and their languages it should allow getting started without extra confusing terminology .

Setup: enable the LED pin to be an output
loop: turn on, wait 1000 ms, turn off, wait 1000 ms

@per1234per1234 added type: enhancementProposed improvement conclusion: declinedWill not be worked on labelsSep 26, 2020
@cmagliecmaglie reopened thisMay 7, 2021
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign ourContributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let usrecheck it.

@cmagliecmaglie closed thisMay 7, 2021
@per1234per1234 added wontfix conclusion: declinedWill not be worked on and removed conclusion: declinedWill not be worked on wontfix labelsOct 20, 2021
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

conclusion: declinedWill not be worked ontype: enhancementProposed improvement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@per1234@ubidefeo@CLAassistant@cmaglie@JeffB42

[8]ページ先頭

©2009-2025 Movatter.jp