Questions tagged [scope]
77 questions
43
votes
7 answers
Does it make sense to create blocks just to reduce a variable's scope?
I'm writing a program in Java where at one point I need to load a password for my keystore. Just for fun, I tried to keep my password in Java as short as possible by doing this:
//Some code
....
KeyManagerFactory keyManager =…

Lord Farquaad
- 569
- 5
- 10
41
votes
9 answers
Using compound statements ("{" ... "}" blocks) to enforce variable locality
Introduction
Many "C-like" programming languages use compound statements (code blocks specified with "{" and "}") to define a variables scope.
Here is a simple example.
for (int i = 0; i < 100; ++i) {
int value = function(i); // Here 'value' is…

wefwefa3
- 997
- 3
- 10
- 21
41
votes
9 answers
What did programmers do before variable scope, where everything is global?
So, I am having to deal with seemingly archiac language (called PowerOn) where I have a main method, a few datatypes to define variables with, and has the ability to have sub-procedures (essentially void methods) that does not return a type nor…

Chad Harrison
- 887
- 7
- 13
40
votes
6 answers
Should I place functions that are only used in one other function, within that function?
Specifically, I'm writing in JavaScript.
Let's say my primary function is Function A. If Function A makes several calls to Function B, but Function B is not used anywhere else, then should I just place Function B within Function A?
Is that good…

Gary
- 503
- 1
- 4
- 5
39
votes
4 answers
Why are brackets required for try-catch?
In various languages (Java at least, think also C#?) you can do things like
if( condition )
singleStatement;
while( condition )
singleStatement;
for( var; condition; increment )
singleStatement;
So when I have just one statement, I…

Svish
- 1,092
- 8
- 14
35
votes
8 answers
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
According to the accepted answer on "Rationale to prefer local variables over instance variables?", variables should live in the smallest scope possible.
Simplify the problem into my interpretation, it means we should refactor this kind of…

ocomfd
- 5,652
- 8
- 29
- 37
19
votes
4 answers
How to Determine # of Programmers needed for a project
How do you know how many programmers a particular project needs to be successful?
The company I work for fulfills orders for client companies. We have written an in-house warehouse management system that handles location based inventory…

kstevens715
- 293
- 1
- 2
- 5
16
votes
2 answers
Why is it good programming practice to limit scope?
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 looked into events and delegates in C#. I searched…

overki11
- 187
- 1
- 5
14
votes
3 answers
How do you safely refactor in a language with dynamic scope?
For those of you who have the good fortune not to work in a language with dynamic scope, let me give you a little refresher on how that works. Imagine a pseudo-language, called "RUBELLA", that behaves like this:
function foo() {
print(x); // not…

senshin
- 324
- 1
- 11
14
votes
5 answers
Should you refactor existing code that is not broken in a project focused on new features?
Given a small project that aims to add new functionality to application, the changes introduced touch some existing code, involving updating these in certain areas. During implementation, I've found some of these code which were updated have…

Carlos Jaime C. De Leon
- 653
- 5
- 13
13
votes
5 answers
Is Java package level scope useful?
I understand the idea of package scope, and at times have even thought I wanted it. However, every time I set down with a serious intent to try using it I discovered it did not fit the needs I thought it would serve.
My main issue always seems to…

dsollen
- 1,123
- 1
- 12
- 28
13
votes
4 answers
Is the usage of internal scope blocks within a function bad style?
There are some (quite rare) cases where there is a risk of:
reusing a variable which is not intended to be reused (see example 1),
or using a variable instead of another, semantically close (see example 2).
Example 1:
var data =…

Arseni Mourzenko
- 134,780
- 31
- 343
- 513
13
votes
9 answers
Isn't class scope purely for organization?
Isn't scope just a way to organize classes, preventing outside code from accessing certain things you don't want accessed?
More specifically, is there any functional gain to having public, protected, or private-scoped methods? Is there any advantage…

Qix - MONICA WAS MISTREATED
- 1,896
- 16
- 32
10
votes
7 answers
Is global state really always bad?
I have a question regarding my general understanding of global state in software engineering.
When I write an app I like to decompose it into little, manageable components and functions, that are ideally self-contained, have their own local state…

Angelica
- 109
- 5
10
votes
2 answers
Should I declare variables at the top of the function for reasons other than the scope rules?
In JavaScript, one should declare all variables at the beginning of the function to mitigate the risk of mistakes related to the fact that the scope of variables is a function. The following code illustrates this non-intuitive scope by actually…

Arseni Mourzenko
- 134,780
- 31
- 343
- 513