arduino / arduino-examples

Arduino IDE bundled examples
Creative Commons Zero v1.0 Universal
90 stars 41 forks source link

Added duration variable to Blink.ino #20

Closed per1234 closed 3 years ago

per1234 commented 3 years ago

Moved from https://github.com/arduino/Arduino/pull/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 commented 3 years ago

From @harryboo on 2020-09-09

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

per1234 commented 3 years ago

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 commented 3 years ago

@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

CLAassistant commented 3 years ago

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