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#10648

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

Conversation

@JeffB42
Copy link

@JeffB42JeffB42 commentedAug 16, 2020
edited
Loading

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other openPull Requests for the same update/change?

Changes to Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?

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...

@harryboo
Copy link

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

@JeffB42
Copy link
Author

JeffB42 commentedSep 11, 2020
edited
Loading

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

@per1234
Copy link
Collaborator

Moved toarduino/arduino-examples#20

@per1234per1234 added Component: DocumentationRelated to Arduino's documentation content feature requestA request to make an enhancement (not a bug fix) labelsSep 3, 2022
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

Component: DocumentationRelated to Arduino's documentation contentfeature requestA request to make an enhancement (not a bug fix)

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@JeffB42@harryboo@per1234

[8]ページ先頭

©2009-2025 Movatter.jp