Timeline for Are global variables evil in Arduino?
Current License: CC BY-SA 3.0
12 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 18, 2017 at 9:49 | history | edited | Arjen | CC BY-SA 3.0 |
deleted 226 characters in body
|
| Jul 13, 2017 at 13:38 | comment | added | Arjen | @Graham I'm curious to find out what kind of discipline you're talking about? I think I have an idea what you're talking about, but discipline isn't going to create 'well-encapsulated code'. The accessor/mutator pattern will sometimes come with a slight performance cost, depending on how you setup your code, agreed. However, lots of expensive operations may be optimized out by a compiler. | |
| Jul 13, 2017 at 11:24 | comment | added | Graham | As a long-time embedded coder, I fairly regularly have to train people out of the cast-iron rule of "Global Variables Bad". Passing arguments always makes code slower. On a PC this is pretty much immaterial, but in the embedded world this is still a very big deal. The accessor/mutator pattern is a way to guarantee sound encapsulation at the cost of processing time. It's perfectly possible to write well-encapsulated code with global variables - but you do have to apply discipline to how you design it, and specify clearly how other people should use it. | |
| Jul 12, 2017 at 15:43 | history | edited | Arjen | CC BY-SA 3.0 |
deleted 219 characters in body
|
| Jul 12, 2017 at 15:37 | comment | added | Arjen | @patstew I didn't think of it that way. I think you're right. I'll modify my post. It will make it more easy to understand also. | |
| Jul 12, 2017 at 15:32 | comment | added | patstew |
Well, it's still global state that can be accessed anywhere in the program, you just access it through Program::instance().setup() instead of globalProgram.setup(). Putting related global variables into one class/struct/namespace can be beneficial, especially if they're only needed by a couple of related functions, but the singleton pattern doesn't add anything. In other words static Program p; has global storage and static Program& instance() has global access, which amounts to the same as simply Program globalProgram;.
|
|
| Jul 12, 2017 at 15:28 | comment | added | Arjen | @per1234 Thanks! I'm definitely no Arduino expert, but I suppose my first suggestion could work also then. | |
| Jul 12, 2017 at 15:23 | comment | added | Arjen | @patstew Do you mind explaining me how you feel it has the same downsides? In my opinion it doesn't since you're able to use data encapsulation to your benefit. | |
| Jul 12, 2017 at 14:05 | comment | added | patstew | A singleton is effectively a global, just dressed up in an extra confusing way. It has the same downsides. | |
| Jul 12, 2017 at 14:05 | comment | added | per1234 | You can define your own main() in the sketch if you prefer. This is what the stock one looks like: github.com/arduino/Arduino/blob/1.8.3/hardware/arduino/avr/… | |
| Jul 12, 2017 at 13:17 | review | First posts | |||
| Jul 12, 2017 at 13:26 | |||||
| Jul 12, 2017 at 13:14 | history | answered | Arjen | CC BY-SA 3.0 |