Does not Developers get confused if there are too many global variables?
-
It has been noticed that some developers heavily use Global variables, may be without any discomfort. Is it like they get accustomed to it and hence use it? Dont they get confused by over usage?
Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!
Depends what you mean by 'too many'. Not everything has to be object-oriented, there is nothing wrong in using non-OO code for a simple script of a few hundred lines, or indeed using semi-OO as might be used in classic C++ for a a few thousand lines. It is not wise to insist on dogma if it means using a bazooka to swat a fly. Indeed, scripts were intended to be kept simple, it is only fairly recent demands to increase the complexity of JS that has resulted in attempts to make it OO. Personally, I think that if code gets this complex then it's better to use server-side C# or something similar, though people are doing some heavy work with JS these days and doing it well. That's my opinion as a humble amateur. One thing I have learnt over the years - for every coding dogma there is at least one exception, almost without exception.
-
Depends what you mean by 'too many'. Not everything has to be object-oriented, there is nothing wrong in using non-OO code for a simple script of a few hundred lines, or indeed using semi-OO as might be used in classic C++ for a a few thousand lines. It is not wise to insist on dogma if it means using a bazooka to swat a fly. Indeed, scripts were intended to be kept simple, it is only fairly recent demands to increase the complexity of JS that has resulted in attempts to make it OO. Personally, I think that if code gets this complex then it's better to use server-side C# or something similar, though people are doing some heavy work with JS these days and doing it well. That's my opinion as a humble amateur. One thing I have learnt over the years - for every coding dogma there is at least one exception, almost without exception.
Thanks
-
I have a project that uses quite a few public statics to hold configuration information. IE. different customers have different features and expect slightly different behaviour from the application. I think this is fairly reasonable as they are set only at the time the application starts up and never change. I'd be interested to know how other people would tackle this.
Most of the times I prefer storing name/key pairs in config file rather than using lot of static fields.
-
If your program uses global variables, they tend to grow over time, given that every new person that comes next will introduce their own (often redundant) global variables, not for laziness, but because they don't know whether using and existing one will have any side effects anywhere else in the code base, so please don't use them or at least keep it at minimum.
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...
Thanks a lot. You have raised a very unique reason to avoid using Global variables as much as possible.
-
I see people use global variables (and class statics, and sometimes singletons) when they don't want to think too hard about how, and where, it will need to be accessed. Confusion? Yeah. Discomfort? Those who use them a lot, get used to keeping track of their scope of use manually.. and once you get into that style of coding, more aren't necessarily much worse. Conveying that scope of use information to the next programmer is difficult, and very quickly the code ends up on a toboggan ride to a very warm place. Unless they're used to hold immutable data that's set up very early in the program, they're usually a maintenance nightmare.
We can program with only 1's, but if all you've got are zeros, you've got nothing.
Great expalanation. Thank you.
-
It's simple really: variables should be visible only where they need to be, which means the scope should be as limited as possible. There are lots of reasons for this, including the fact that some of the worst bugs in the world are caused by using variables with too wide of a scope. I once saw a daily import of very important financial data blow up due to such a bug. The guy who wrote the import routine in T-SQL read the identity of the last inserted record of a certain table using @@IDENTITY. This was important because if it got the wrong identity back foreign keys would get all screwed up during the import. Anyway, guess what happened when another developer innocently added a trigger to the table for bulk imports? Yeah. @@IDENTITY is essentially global in scope, so adding an insert trigger to the table--which should have been fine--blew it the hell up. That ended up costing a good chunk of real money for the company. It was also a hard one to debug, because the trigger was the only new code and everyone looked over it and it looked fine, it took a while to figure out what was happening with the import routine which had been in production for years. And all because @@IDENTITY was used instead of SCOPE_IDENTITY. There are very few cases where global variables are a good idea. They make good time bombs though.
Agreed. Thank you.
-
Not that I am particularly defending the use of var, but I quite like it in the case of
var foo = new SomeReallyQuiteSensibleButLongName();
especially if there are a few of them (cuz they line up and look nice) You can see what the type is easily, so no harm done. When it is
var foo = TheResultOfSomeFunctionWhoseNAmeDoesntReflectItsReturnType();
it annoys me as I have to use intellisense to ind out what type it is. Especially annoying with something like
var selectedCustomer = GetCustomerFromList();
which, at first glance, I would guess at selectedCustomer being some kind of Customer object. Imagine my surprise when I find out it is a boolean! Yep - the function returns a boolean that determines whether the customer was selected from the list. Now this comes down to good naming - and I have yet to meet a developer that names consistently well (myself included)
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Thanks for good explanation.