Linked Questions

8
votes
7answers
929 views

Why is scope a good thing? [duplicate]

I'm a newbie coder. I find it troublesome to declare a variable in 1 function and not be able to access it in other functions. I have to make many of my variables global just to get my code to work. ...
16
votes
2answers
4k views

Why is it good programming practice to limit scope? [duplicate]

I'm relatively new to programming (July 2015), and I've always wondered why it's good programming practice to hide variables as much as possible. I've run into this question mainly recently when I ...
4
votes
9answers
4k views

Confusion of scope of a variable: Is Global Variable a solution? [duplicate]

I am newly learning C Programming. When we have a C Program with lot of functions, parameter passing, calling and all - It becomes a great problem as to which variable must be declared where. My ...
5
votes
4answers
9k views

Why use a local variable over a global variable? [duplicate]

Im a beginner using visual studio 2013, and I know what they are, but I'm puzzled, why would one use a local variable? I would always just use a global variable.
1
vote
1answer
10k views

Python: Faster to use global variable or pass as arguments to a function? [duplicate]

Hey so i was wondering which is the more efficient way, or better practice to do in this situation. 1, def function(): global number number += 2 Or 2, def function(number): return ...
3
votes
2answers
1k views

Passing central objects around or having global instances? [duplicate]

Having a larger WinForms application with several classes I currently pass references to several "central" objects around to function calls. This leads to more method parameters. Example: public ...
1
vote
2answers
5k views

Should I use extern global variables or static global variables with “get” and “set” functions in C [duplicate]

I am working on one of my first projects in C (microcontroller code) which is split up into multiple C files. I noticed some inconsistency in my coding where I had some variables declared as extern ...
0
votes
1answer
478 views

Singleton: Why is a global point of access bad? [duplicate]

Most people here and on StackOverflow agree that Singletons are evil. The main explanation I've come across against the use of Singletons, is they fact that they provide a global point of access to ...
253
votes
22answers
16k views

How are globals any different from a database?

I just ran across this old question asking what's so evil about global state, and the top-voted, accepted answer asserts that you can't trust any code that works with global variables, because some ...
20
votes
5answers
5k views

What are the benefits of dependency injection in cases where almost everyone needs access to a common data structure?

There are plenty of reasons why globals are evil in OOP. If the number or size of the objects needing sharing is too large to be efficiently passed around in function parameters, usually everyone ...
39
votes
6answers
7k views

Why is “tight coupling between functions and data” bad?

I found this quote in "The Joy of Clojure" on p. 32, but someone said the same thing to me over dinner last week and I've heard it other places as well: [A] downside to object-oriented programming ...
39
votes
4answers
54k views

Why prefer non-static inner classes over static ones?

This question is about whether to make a nested class in Java to be a static nested class or an inner nested class. I searched around here and on Stack Overflow, but couldn't really find any questions ...
13
votes
7answers
14k views

What are the roles of singletons, abstract classes and interfaces?

I am studying OOP in C++ and, even though I am aware of the definitions of these 3 concepts, I cannot really realize when or how to use it. Let's use this class for the example: class Person{ ...
18
votes
2answers
1k views

Is it a good idea to define one big private function in a class to maintain valid state, that is, to update the object's data members?

Although in the code below a simple single item purchase in an e-commerce site is used, my general question is about updating all data members to keep an object's data in valid state at all times. I ...
2
votes
5answers
5k views

why are globals bad in javascript [duplicate]

I've been writing web applications for a while now , and everyone knows the one golden rule not to assign variables to the window object , don't ever assign a variable without using var. My ...
5
votes
3answers
2k views

How can I effectively manage long-term feature development and short-term bug fixing at the same time?

We have a huge C, and C++ code base. There is a plan to add a new feature into the existing system, but, this should happen only after 3 to 4 months. So, we are looking for best possible options. ...
3
votes
2answers
305 views

Place variables frequently used by the same function on the heap?

Say that I have the following situation: void myFunc() { int x; //Do something with x } "x" is placed on the stack which is no doubt fast. Now, "myFunc" is called very frequently, lets say ...
6
votes
5answers
931 views

Are “open-closed principle” and “less coupling” rationales to use global state?

Consider I'm writing a mobile app with user-login feature, using a framework which can be simplified like that: class UserData{ static token=""; static name=""; static balance=0; } class ...
5
votes
5answers
3k views

Proper OO design for State and Command

I'm working (a bit) on a (turn-based strategy) game. There are two classes relevant for the question: State: This is an immutable class, which exposes all its fields (either via getters or another ...
5
votes
3answers
1k views

Are we overlooking bad effects of global state in this design?

I’ve read the answers to Why is Global State so Evil?, and I think the negative consequences do not apply in this situation. However, that’s what everyone says just before they get hit by a falling ...
4
votes
1answer
3k views

How to avoid global state without large function signatures?

So, there are some good answers to this question, namely: Why is Global State so Evil? I have a specific question about the alternatives Mikera proposed as the answer with the second most up-votes. ...
2
votes
1answer
7k views

Memory usage of global versus local variables

I am a beginner in C coding. I'm writing a C code where a variable is required to be used by different functions. I want to ask whether to use a global variable and manipulate it through functions or ...
0
votes
2answers
316 views

Is the global state believed to be evil because of its nature or mostly due to its usual, no-rules usage?

I don't dislike global state, but that could be due to the lack of experience. I was thinking about what the usual implementation of global state is: A big variable where data flows in a non-...
2
votes
3answers
2k views

Using static to create an immutable object

I was reading on this software engineering page about the use of static methods. However, I'm confused, and maybe it's about the context in which it talks about static. The idea is, that static is ...
1
vote
1answer
2k views

Is it a good idea to use my own callback mechanism instead of standard implementation like startActivityForResult?

I am trying to create a simple app which has 2 activities, which enter text on one activity and then displays it on the title bar if another activity: EditTextActivity: MainActivity: I know the ...
4
votes
1answer
1k views

Why shouldn't a static class have an internal state?

While working on a project, I decided to create a database class to manage my DB connection. I started looking for the best practice to do that, which is usually either a static class or a singleton ...
1
vote
2answers
346 views

Is it a good idea having default static variables for new instances?

OK let's say I have something like this: public class MyObject { public static int DefaultValue = 9 private int _value = DefaultValue; public int Value { get { return _value; } set { ...
1
vote
1answer
2k views

Why singleton is an anti-pattern? [duplicate]

Many articles in Internet say that singleton is an anti-pattern, because it makes debugging more difficult. However I don't understand why debugging a program with a singleton object is difficult. ...
-2
votes
1answer
1k views

Global variable or parameter?

Is it correct that homeitem should be declared inside the OnLoad method and overgiven to all the render methods instead of just declaring it as a global variable and accessing it by all the render ...
2
votes
1answer
911 views

Preserving data in js with multipage application

One benefit of single page applications like Angular.js, is that you can store data in a global variable and access in multiple routes. With multiple page applications, the data is lost during ...

15 30 50 per page