My point is that it is a choice, whether to enforce strict declarations or not or to require strict typing or not. Arguments can be made for either. Different languages make different choices.
With regards to the Mars Climate Orbiter, my understanding was that that was an error in the assumption of units, not type. Presumably (I don’t know the code specifics) it was always a variable of some kind of float. The issue was whether it represented a metric or imperial value. I assume that whatever language they were using did not have a metric
and imperial
type or a newton-seconds
or pound-seconds
type. They just had some kind of float and a variable name that didn’t make clear that it was metric. You might call that an issue of variable naming but more likely it is an issue of documentation or leadership. Or you can blame it on the US’s intransigent instance on not joining the rest of the world in adopting the metric system even though it is widely used even by American engineers. I don’t think any programming language could have prevented that.
But, if one is a ‘one man team’ and selects global’s judiciously, I just wondered is that still ‘bad’–
I still think it is a bad idea. Unless the program is small, it is easy to loose track of globals. And in terms of maintainability, anyone that has had to come back and examine their own code after 6 months knows, it is easy to forget what you assumed would be obvious when you wrote it.
I still say that global variables should be used sparingly. Sure, constants that will be used everywhere might make sense. If you are writing a math program and have a constant like PHI
, there is little chance that you will reuse that name somewhere else for some other value. Constants are perhaps slightly less dangerous, but variables wreak havoc.
I always work on the assumption that someone is going to have to look at my code and understand that, whether or not that is a fellow coder in a week, or me in a year.
I do not hold that every variable must be local and passed. I occasionally use variables that are available to “this” scope and it’s sub-scopes, essentially a “local” global variable, even if just global to a certain module. I do this if passing it around is going to cause more confusion than is saves.
On some of the small apps I did for FCC, I used a few global variables. Often I’ll put them in a global object called GLOBAL
so there is no confusion.
I think global variables are a bad habit, one that should be resisted. It’s not uncommon for beginners to use them and that is fine, but eventually the the size of their project will increase to where that gets increasingly dangerous. Sometimes they hold onto globals because they haven’t learned how to plan their app. They aren’t thinking out what info is needed where.
The “one man team” is less and less common. Apps are now so big that they require a team. We should be using best practices whenever possible, even if we feel they aren’t needed. Firstly, they are often needed even when we don’t realize, and secondly, if we use those best practices they eventually become instinctual - they become the way you think.
I too like to ask the question “why”. Sometimes the rules change. When I started programming a function always had to have one return. Now functions often have a “fail fast” return at the top and a few returns at the bottom. Redux reducers have a return on each case and that is considered good practice. But I understand the logic of the old rule - that it can be confusing to understand a function if it has multiple entries and exits - so I also understand that now we “break” the rule as long as it is clear.
Just about every rule (or principle if you like) has some boundary case where it seems like it would be better to break it. But that doesn’t mean that the rule is not a good idea. Learning to code without globals will save many, many more headaches than it causes.
So, I’m not saying globals are always bad. I’m saying that IMHO, in general they are a bad idea. If we’re starting to use one, we should ask ourselves, Who really needs this variable? Is there a better way to do this? Maybe there are a few cases where that value is truly needed everywhere and passing it around would make too much of a mess. So if one must do something “dangerous”, they at least should know that it is a little sketchy and should do it sparingly and should accordingly document it well. Pounding that rule into people’s heads makes sure that they don’t do it willy-nilly.