Author Topic: Global variables - Evil or not  (Read 7614 times)

0 Members and 1 Guest are viewing this topic.

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3469
  • Country: it
Re: Global variables - Evil or not
« Reply #50 on: October 26, 2022, 10:09:15 am »
Still, he makes good points on how to write robust code: limit the globals, write self-contained modules with clear interfaces, yadda yadda.
Nothing really revolutionary, in my view.
True, nothing groundbreaking but it's worth being repeated to death. The importance of writing clear, understandable, clean code is lost on the majority of programmers. Limiting globals to enforce tidiness and discourage laziness is ultimately good.

Quote
Just a sample: in one of my projects, about 200 kLOC, I have a half dozen of globals:
  • They provide state to whole program.
  • They can be read everywhere.
  • They are written in exactly two points: user input, or loading of a saved preset.
same situation.

Quote
Given the above, I did not bother with accessors. Should the need arise of more writers, I might consider it.

bingo. Problem is when more than one person is touching the code.
 
The following users thanked this post: newbrain

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4077
  • Country: gb
Re: Global variables - Evil or not
« Reply #51 on: October 26, 2022, 10:11:07 am »
Gannsle's article goes a bit for hyperbole, and for some counterfactual information (Ariane 5 was, in fact, lost because ranges were checked!).

Never check for any error condition that you don't know how to handle better than just ignoring it!

Writing if (unexpected) trigger_self_destruct() is often not an improvement.

There is worse than that. 

A-holes who put hard runtime asserts into their library code.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21736
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Global variables - Evil or not
« Reply #52 on: October 26, 2022, 10:34:22 am »
Somewhat tangential, but something to keep in mind whenever you see clouds of holy wars gathering...



tl;dr viral "problems" go viral, in part, specifically because, the experts that know those problems are shite, and avoid them.  Which voluntarily excludes them from the conversation.  Then there's no authoritative voice to guide conversation, and it ends up dividing into camps that are mutually "obviously right" in their answers.  Which, while they might be "right" more or less, the truer answer is, "it's a stupid question" -- it should be reframed to one that makes sense.  But then the answer will be obvious and uninteresting to talk about....

Put another way: an aspect of critical thought is not just to 1. recognize experts on the topic, but 2. recognize their absence when it happens -- that is, even on topics that they are likely to have seen, and still have not participated in.  Consider if there might be a reason for that.  It could just be accidental of course, but it might be intentional.

So, keep that in mind with respect to "controversial" topics like this.  Fortunately, this thread, I think, has drawn a fair diversity of users here, but that might not be the case everywhere, especially where the quality of discourse (in the thread, or more broadly) has been poor, further shooing off experts.

More directly: this topic, as most basically stated in the title, easily leads to divisive thinking.  It is an issue with some nuance, but one must look deeper to appreciate those nuances, and the question can be phrased better (though in this case it may take knowledge of the situation and possible alternatives to know how better to phrase it; that happens sometimes...often?..).

Which is kind of a side issue these days; ironically, despite the diversity and scale of the internet, it's increasingly hard to find resources on questions like this, other than on Stack for example, and as it's not a conversation platform, there may be less incentive for experts to contribute to mostly-correctly answered questions.  Or they assume (erroneously) that a reader may be able to find / anticipate related issues that haven't been mentioned yet.  Or that answers change over time (especially for software, as languages, apps and OSs get upgraded) but nothing brings topics to the front for further review until commented upon

Not that the SNR is any better at places like here.  For question-answers, I'd rather skip to the answers rather than sift through ten pages of fluff (or a hundred..).  But on those topics where conversation is necessary, yeah.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11694
  • Country: my
  • reassessing directives...
Re: Global variables - Evil or not
« Reply #53 on: October 26, 2022, 11:20:48 am »
Which, while they might be "right" more or less, the truer answer is, "it's a stupid question" it should be reframed to one that makes sense.  But then the answer will be obvious and uninteresting to talk about....
ditto! when i see this kind of riddle i will just skip it. when other FB newcomers will like to answer to show how clever high IQ they are. 9 = 90 is just plain stupid written by moron, period. it will be more acceptable if 9 --> 90 (read mapped). is like in the other thread discussing about a scope, someone came and said DS1054Z is a "sampling oscilloscope", it came down to terminology differences. but i dont think its relevant to this thread. this topic is about "why should i care about something that is none of my business?", my context is this, but the problem is not in my context, but i would like to ask anyway. so 2 schools came, imho both are right within their own context. because once upon a time we came from school #1 so we understand why they say its not a problem.
« Last Edit: October 26, 2022, 11:25:22 am by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6321
  • Country: fi
    • My home page and email address
Re: Global variables - Evil or not
« Reply #54 on: October 26, 2022, 12:17:50 pm »
the truer answer is, "it's a stupid question" -- it should be reframed to one that makes sense.
I prefer the term "wrong question", but only because there are lots of people who are afraid of asking questions because they fear being ridiculed or pointed out as stupid.

Reason being, it is not stupid, per se, just wrong.  Many times those asking the wrong question have to be explained how the question is wrong.  Typical reasons in my experience are loaded questions (that cannot be answered) and incorrect premises or assumptions leading to the incorrect question being posed, and especially that in the logic chain of the current overall problem a previous question before this was answered incorrectly, making this entire question irrelevant and useless.

Stupid is when after having been pointed that out, the person just repeats the same question.  Then ridicule is appropriate; but not as the initial reaction.

In physics, various terms are used from "nonsensical" (as in "irrational", "meaningless" or "without practical meaning") to "ill-posed" (extending the well-posed problem concept by Hadamard), because mathematics is only applied to describe the behaviour of systems, and if the initial problem/question is posed wrong, then the results can be horribly wrong.  In particular, in most physics simulations, the very first thing a physicist does when they obtain a set of results, is to check if it makes any sense.

I wish English had a term with suitable connotations here, like "missensical" or "mis-asked", which would immediately tell the asker that one or more of the premises or axioms related to the question are incorrect/unsuitable/unphysical, making the question itself irrelevant, necessitating a step back in the chain of logic for the underlying problem at hand –– but without making the asker feel stupid.  We all make mistakes, and everyone should always have a go at fixing their question at least once.  Missing important details is a very easy error to make.  Perhaps "erroneous question"?  Or is that too pretentious?  :-//
 
The following users thanked this post: MK14

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6271
  • Country: ro
Re: Global variables - Evil or not
« Reply #55 on: October 26, 2022, 12:19:25 pm »
Interesting points on the topic of hiding the fact variables are global.  This generates a LOT of discussion and arguments in Enterprise space.


Doh, but I bet it does, especially on Enterprise!



 ;D

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4460
  • Country: dk
Re: Global variables - Evil or not
« Reply #56 on: October 26, 2022, 01:18:14 pm »
Gannsle's article goes a bit for hyperbole, and for some counterfactual information (Ariane 5 was, in fact, lost because ranges were checked!).

Never check for any error condition that you don't know how to handle better than just ignoring it!

Writing if (unexpected) trigger_self_destruct() is often not an improvement.

https://en.wikipedia.org/wiki/Battleshort :)
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11694
  • Country: my
  • reassessing directives...
Re: Global variables - Evil or not
« Reply #57 on: October 26, 2022, 02:04:32 pm »
the truer answer is, "it's a stupid question" -- it should be reframed to one that makes sense.
I prefer the term "wrong question", but only because there are lots of people who are afraid of asking questions because they fear being ridiculed or pointed out as stupid.
there are stupid content creators only for hunting "likes/views", and there are stupid followers, who react to the first's video... the video creator above is the later. they dont ask question, they try to establish their point of view through wrong notations, leading to misconception and harm esp to kids just started going to school.

Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 
The following users thanked this post: pcprogrammer

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3778
  • Country: nl
Re: Global variables - Evil or not
« Reply #58 on: October 26, 2022, 02:09:14 pm »


Nothing wrong with that. Just fill in the missing bits in your mind. The answer is 12. :-DD

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3778
  • Country: nl
Re: Global variables - Evil or not
« Reply #59 on: October 26, 2022, 02:19:43 pm »

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4577
  • Country: gb
Re: Global variables - Evil or not
« Reply #60 on: October 26, 2022, 02:26:57 pm »
I wish English had a term with suitable connotations here, like "missensical" or "mis-asked", which would immediately tell the asker that one or more of the premises or axioms related to the question are incorrect/unsuitable/unphysical, making the question itself irrelevant, necessitating a step back in the chain of logic for the underlying problem at hand –– but without making the asker feel stupid.

I'd suggest, a suitable English term might be ...
Ambiguous or Ambiguous question.

E.g. What is 1 + ! = ?
The answer could be 2, because I typed '!' instead of '1', or it could be an entirely different question.  Hence it is AMBIGUOUS, because you can't tell readily/easily from the question, what the right answer is (without extra information, knowledge or help, from the question source).

I.e. The question is (possibly) malformed (e.g. a typing mistake, or issue with the fonts, so it was suppose to be a '1', but came out as a '!', somehow).
 
The following users thanked this post: PlainName, pcprogrammer

Offline timenutgoblin

  • Regular Contributor
  • *
  • Posts: 189
  • Country: au
Re: Global variables - Evil or not
« Reply #61 on: October 26, 2022, 02:32:02 pm »
Then there's no authoritative voice to guide conversation, and it ends up dividing into camps that are mutually "obviously right" in their answers.  Which, while they might be "right" more or less, the truer answer is, "it's a stupid question" -- it should be reframed to one that makes sense.  But then the answer will be obvious and uninteresting to talk about....


Which, while they might be "right" more or less, the truer answer is, "it's a stupid question" it should be reframed to one that makes sense.  But then the answer will be obvious and uninteresting to talk about....
ditto! when i see this kind of riddle i will just skip it. when other FB newcomers will like to answer to show how clever high IQ they are. 9 = 90 is just plain stupid written by moron, period. it will be more acceptable if 9 --> 90 (read mapped).


Nothing wrong with that. Just fill in the missing bits in your mind. The answer is 12. :-DD
 
The following users thanked this post: pcprogrammer

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3778
  • Country: nl
Re: Global variables - Evil or not
« Reply #62 on: October 26, 2022, 02:32:46 pm »
E.g. What is 1 + ! = ?

And then the answer would be 11  |O  :-DD (See the video Mechatrommer posted)

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6321
  • Country: fi
    • My home page and email address
Re: Global variables - Evil or not
« Reply #63 on: October 26, 2022, 04:12:55 pm »
the truer answer is, "it's a stupid question" -- it should be reframed to one that makes sense.
I prefer the term "wrong question", but only because there are lots of people who are afraid of asking questions because they fear being ridiculed or pointed out as stupid.
there are stupid content creators only for hunting "likes/views", and there are stupid followers, who react to the first's video... the video creator above is the later. they dont ask question, they try to establish their point of view through wrong notations, leading to misconception and harm esp to kids just started going to school.
I consider those more like "lies and fallacies masquerading as questions", myself.

I don't like even those called "stupid questions", because as I said, it makes those who just don't know the difference yet afraid of asking questions –– and that means they never learn to ask good questions.  And the world becomes less, if we do not ask good questions.

I'd suggest, a suitable English term might be ...
Ambiguous or Ambiguous question.
Hmm... ambiguous: open to multiple interpretations.  That's not it, because the point is that the question itself is incorrect, not that it can be interpreted incorrectly.  In face-to-face, I can often infer the correct interpretation of ambiguous questions –– like "When you were carrying that TV yesterday, did you notice the friendly squirrel at front?  Did you bring it inside okay?" typically "it" referring to the TV, and not to the squirrel.  In written text, it is similarly common for the exact interpretation to be inferrable from the surrounding context, often subsequent text, leaving the interpretation in a Schrödinger state for a paragraph or two.

To be clear, I'm referring to questions like "Why isn't there any solutions to x*x - 2.0 = 0?", when the asker is really asking why their C code using the float type (float x = sqrtf(2.0f); float y = x*x - 2; if (y == 0) printf("Okay!\n"); else printf("Fail!\n");) fails.  I indeed do occasionally answer this kind of questions, often with a snarky initial bite to get the OP out of the passive funk (indicated by the, uh, "inane" question they posed?), followed with actual useful help.

And also questions like "I want to fix this engine using these two q-tips and a crow feather", or their programming or engineering or electronics equivalent.  These are super-constrained so that no realistic solution is possible, unless you start by "First, trade one of the q-tips for at least two rubber strings no less than four inches in diameter", and describe the next thousand or so steps in detail.

(And yes, I kinda like "inane question" even better than "wrong question", because its dictionary definition fits like a finger in a nostril.  It's just that I don't see it used in everyday speech, so I think it might be perceived a bit snotty.)
 
The following users thanked this post: MK14

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4577
  • Country: gb
Re: Global variables - Evil or not
« Reply #64 on: October 26, 2022, 04:51:37 pm »
E.g. What is 1 + ! = ?

And then the answer would be 11  |O  :-DD (See the video Mechatrommer posted)

One solution (to that video you mentioned), would be to say to the, possibly problematic pupil, towards the beginning of the video.  You have won a big prize, with 22 nice sweets in it.  The only catch, is you have to give just 2 sweets, to another pupil, and another two sweets, to a different pupil.
How many sweets, do I remove from the prize bag, for the other pupils?
4 or 22 sweets?
« Last Edit: October 26, 2022, 04:53:11 pm by MK14 »
 
The following users thanked this post: pcprogrammer

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4577
  • Country: gb
Re: Global variables - Evil or not
« Reply #65 on: October 26, 2022, 05:11:44 pm »
I'd suggest, a suitable English term might be ...
Ambiguous or Ambiguous question.
Hmm... ambiguous: open to multiple interpretations.  That's not it, because the point is that the question itself is incorrect, not that it can be interpreted incorrectly.  In face-to-face, I can often infer the correct interpretation of ambiguous questions –– like "When you were carrying that TV yesterday, did you notice the friendly squirrel at front?  Did you bring it inside okay?" typically "it" referring to the TV, and not to the squirrel.  In written text, it is similarly common for the exact interpretation to be inferrable from the surrounding context, often subsequent text, leaving the interpretation in a Schrödinger state for a paragraph or two.

To be clear, I'm referring to questions like "Why isn't there any solutions to x*x - 2.0 = 0?", when the asker is really asking why their C code using the float type (float x = sqrtf(2.0f); float y = x*x - 2; if (y == 0) printf("Okay!\n"); else printf("Fail!\n");) fails.  I indeed do occasionally answer this kind of questions, often with a snarky initial bite to get the OP out of the passive funk (indicated by the, uh, "inane" question they posed?), followed with actual useful help.

And also questions like "I want to fix this engine using these two q-tips and a crow feather", or their programming or engineering or electronics equivalent.  These are super-constrained so that no realistic solution is possible, unless you start by "First, trade one of the q-tips for at least two rubber strings no less than four inches in diameter", and describe the next thousand or so steps in detail.

(And yes, I kinda like "inane question" even better than "wrong question", because its dictionary definition fits like a finger in a nostril.  It's just that I don't see it used in everyday speech, so I think it might be perceived a bit snotty.)

But you don't know if the question is incorrect or not (with absolute 100% certainty), that in itself, maybe a considerably harder question/problem to solve.  Hence the term ambiguous.

An expert on C, on this forum, may take one look at it, and say something like, "Since version xyz.123...... of the C standard, that (hypothetical, no connection to the C code you just mentioned) expression is always defined as ...".

Or later in the job interview, they might say, that the question was purposely created to have no definitive exact answers.  In order to make sure the possible future employee, is confident enough to simply say "They don't know the answer", or the question is ambiguous or badly formed (incorrect question).

It might be that (the reader of the question), missed some additional information/clues.  Which made the question answerable.  E.g. A schematic, which says all resistors are 1%, 0.25watt metal film, unless stated otherwise.

In other words, MAYBE the question is faulty, but it could also be that the reader (of the question), is simply mistaken or lacking experience, which would make the question unambiguous to them.  So simply saying the question is incorrect, is NOT that simple.

So I still think the term, ambiguous, is probably best.

Real Life Example (from memory, so might be wrong, also I can't 100% remember the original question, so I may have changed it a fair bit).
There was an online electronics test quiz, with (at the time it was written), perfectly valid questions.  But it said something like:
"How many transistors, might there be on a high complexity integrated circuit?".
A:..10,000
B:..1 million
C:..None of these answers
 
Nowadays, the correct answer is probably C, but at the time the question was created, answer B, or even A, could be correct.  So I actually gave the wrong (as regards current technology) answer, in the hope it was the correct answer for when the online quiz question was created.

EDIT: On reflection, you could always say "Incorrect Question", I can't really object.
« Last Edit: October 26, 2022, 05:45:54 pm by MK14 »
 

Offline AVI-crak

  • Regular Contributor
  • *
  • Posts: 125
  • Country: ru
    • Rtos
Re: Global variables - Evil or not
« Reply #66 on: October 26, 2022, 11:42:36 pm »
I have used global variables forever and have never had any issues. Am I missing something?
Hint, a global variable should be declared like this:
Code: [Select]
register volatile uint32_t  __very_important_5214     asm     ("r7");
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4051
  • Country: nz
Re: Global variables - Evil or not
« Reply #67 on: October 27, 2022, 12:01:06 am »
I have used global variables forever and have never had any issues. Am I missing something?
Hint, a global variable should be declared like this:
Code: [Select]
register volatile uint32_t  __very_important_5214     asm     ("r7");

I've done that a few times, but I think I used r9.

Works very nicely, even calling out to libraries that don't see this declaration (e.g. 3rd party) -- as long as you don't have any callback functions passed to them (including virtual functions in your own derived classes).
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14555
  • Country: fr
Re: Global variables - Evil or not
« Reply #68 on: October 27, 2022, 01:19:35 am »
It compiles, so it works.TM ;D
 
The following users thanked this post: paulca

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6321
  • Country: fi
    • My home page and email address
Re: Global variables - Evil or not
« Reply #69 on: October 27, 2022, 08:04:12 am »
It compiles, so it works.TM ;D
You monster! :rant:
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19636
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Global variables - Evil or not
« Reply #70 on: October 27, 2022, 09:01:56 am »
It compiles, so it works.TM ;D

The modern variant of that is "it passes the unit tests, so it works".

Regrettably I couldn't trademark that because it is common usage :(
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4460
  • Country: dk
Re: Global variables - Evil or not
« Reply #71 on: October 27, 2022, 09:20:19 am »
It compiles, so it works.TM ;D

The modern variant of that is "it passes the unit tests, so it works".

Regrettably I couldn't trademark that because it is common usage :(

so it works so it ships
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4077
  • Country: gb
Re: Global variables - Evil or not
« Reply #72 on: October 27, 2022, 09:56:54 am »
It compiles, so it works.TM ;D

The number of times in work you will here me say, "Just run it!  Stop checking it, run it and you'll find out if it works."  LOL
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4077
  • Country: gb
Re: Global variables - Evil or not
« Reply #73 on: October 27, 2022, 10:01:04 am »
On the debate.  I find this forum awesome because there are so many, very, very smart people in here.  From a wide range of backgrounds and views.

The original topic was just the cat thrown to the pigeons.

It went how I would have expected it to... if the OP wanted an actual answer, then I don't think this would be the place to ask.  I for one wasn't taking it seriously, just a water cooler heated debate we will all just walk away from back to our benchs and desks and mummble about for a while.

My own opinions in this were delibrately biased against complexity, however I'll be the first to introduce standards, conventions and best practices in a project in work.  IF they are necessary.

The only negative that drags it down is when the attacks turn from the ideas to the people who proposed them.  If you find yourself using "you" in the none 2nd sense and are indeed directing "you" to an individual and not the idea.  I would suggest you stop and consider it rude.  Ideas are stupid, it doesn't mean the person with that idea is stupid.  Consider the gold fish thought experiments.
« Last Edit: October 27, 2022, 10:08:34 am by paulca »
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11694
  • Country: my
  • reassessing directives...
Re: Global variables - Evil or not
« Reply #74 on: October 27, 2022, 10:53:35 am »
The number of times in work you will here me say, "Just run it!  Stop checking it, run it and you'll find out if it works."  LOL
almost everytime immediately you will know it works... what you are going to find out sooner or later is if it doesnt work... and worse when you already send the project to the moon. so i guess more precise word is... "run it and you'll find out if it doesnt work"... hint: semantic error... ::)
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf