112
votes
Accepted
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
No. There are several reasons why:
Variables with meaningful names can make code easier to comprehend.
Breaking up complex formulas into smaller steps can make the code easier to read.
Caching.
...
36
votes
Accepted
Does it make sense to create blocks just to reduce a variable's scope?
First, speaking to the underlying mechanics:
In C++ scope == lifetime b/c destructors are invoked on the exit from the scope. Further, an important distinction in C/C++ we can declare local objects. ...
27
votes
Does it make sense to create blocks just to reduce a variable's scope?
In my opinion it would be more clear to pull the block out into its own method. If you left this block in, I would hope to see a comment clarifying why you're putting the block there in the first ...
26
votes
Accepted
Why is scope a good thing?
Engineering is, abstractly, managing complexity.
Software Engineering is, abstractly, managing complexity in software!
Scope is a tool to help manage complexity, like any tool it can be used or ...
17
votes
Does it make sense to create blocks just to reduce a variable's scope?
They can be useful in Rust, with it's strict borrowing rules. And generally, in functional languages, where that block returns a value. But in languages like Java? While the idea seems elegant, in ...
16
votes
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
Agree, variables which are not necessary and does not improve the readability of the code should be avoided. The more variables which are in scope at a given point in code, the more complex that code ...
11
votes
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
In addition to the other answers, I'd like to point something else out. The benefit to keeping a variable's scope small isn't just reducing how much code syntactically has access to the variable, but ...
11
votes
Why is scope a good thing?
Minimizing your use of global variables means that you don't have to think about how these variables are interacting with your functions.
How many different places in your code are writing to that ...
11
votes
Is global state really always bad?
I think you are conflating two (surely related) things here: global state, and global variables which are accessible from everywhere in a huge application. An application can have global state without ...
9
votes
Is `this` in JavaScript an example of dynamic scoping?
No, this is lexically scoped to a function, just like a function parameter. It basically works like an implicit parameter.
Dynamic scoping means that if a variable is defined in a function, it is ...
7
votes
Accepted
Is `this` in JavaScript an example of dynamic scoping?
Dynamic scope seems to imply, and for good reason, that there's a model whereby scope can be determined dynamically at runtime, rather than statically at author-time. That is in fact the case.
Let's ...
Community wiki
6
votes
Does it make sense to create blocks just to reduce a variable's scope?
General case:
Is there a case where using blocks only to reduce variable scope makes sense?
It is generally considered good practice to keep methods short. Reducing the scope of some variables can ...
6
votes
Is global state really always bad?
Sometimes it's about what you make me read.
When I'm debugging something that relies on a mutable global variable (sorry, depends on) you force me to go read anything that writes to it to understand ...
5
votes
Exponential Growth of Scope of C++ Projects
Does anyone with experience of C++ and other performance-focused/compiled languages agree C++ makes things complicated and maybe recommend alternatives?
I've been programming in C++ professionally for ...
5
votes
Why is scope a good thing?
You mention you're a newbie coder, so I'm guessing you're working on fairly small programs right now - small enough that you can keep most of it in your head at once. But a typical mobile app is ...
4
votes
Is global state really always bad?
When you make something global, you force there to only be one of it.
You say that user authentication status should be global. Then what happens later, when your customers want to be able to log in ...
3
votes
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
Whats somewhat missing as reason for NO is debugging / readabillity.
Code should be optimized for that, and clear and concise names help a lot
e.g. imagine a 3 way if
if (frobnicate(x) && (...
3
votes
Why is scope a good thing?
There are a couple of schools of programming methodology, but nearly everyone agrees that functions should have well defined and narrow responsibility. For one, it makes testing much easier, for ...
3
votes
Accepted
Python Class vs File level variables
This is a mostly stylistic choice. In the end, it doesn't matter. There are some finer differences depending on how they are used.
If the constants are only used within the module, using module-level ...
3
votes
Define "constants" at the global or function scope?
You’re well on your way to making a global.
The principle of data hiding tells us to limit exposure of details. Constants are details. The wider the scope the wider the exposure. The correct scope for ...
3
votes
Is global state really always bad?
There's a lot of airy claims like "they can't be reasoned about".
Virtually any application will in fact have a few globals, they just get dressed up as singletons, as context objects that ...
3
votes
How to propagate lexical scope in a compiler, specifically around step-by-step functions?
But I'm wondering typically how you are supposed to handle variables that are defined in random places in a linear sequence.Does this mean that every time a variable is introduced, we fork the scope ...
2
votes
Does it make sense to create blocks just to reduce a variable's scope?
Is there a case where using blocks only to reduce variable scope makes sense?
I think that motivation is sufficient reason to introduce a block, yes.
As Casey noted, the block is a "code smell" that ...
2
votes
Why is scope a good thing?
Beyond the observation that global state is bad, what you're looking for, if you want to share variables between several methods, is the notion of the class and object instantiation!
A class lets ...
2
votes
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
Consider functions (methods). There it is neither splitting the code in the smallest possible subtask, neither the largest singleton piece of code.
It is a shifting limit, with delimiting logical ...
2
votes
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
I would say no, because you should read "smallest scope possible" as "among existing scopes or ones that are reasonable to add". Otherwise it would imply that you should create artificial scopes (e.g. ...
2
votes
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
This is somewhat language-dependent, but I would say that one of the less obvious benefits of functional programming is that it encourages the programmer and reader of code to not need these. Consider:...
2
votes
Accepted
Should product scopes and/or project scopes be considered for small internal development work
Paperwork like this is almost certainly either an attempt at delegating accountability, or an attempt at delegating responsibility, or both. Ask yourself why people are trying to pass off this ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
scope × 77variables × 14
c++ × 8
java × 7
javascript × 7
programming-languages × 7
design × 5
c# × 5
object-oriented × 5
programming-practices × 5
c × 5
coding-style × 4
refactoring × 3
compiler × 3
encapsulation × 3
design-patterns × 2
python × 2
testing × 2
functional-programming × 2
naming × 2
class-design × 2
functions × 2
single-responsibility × 2
business-logic × 2
language-features × 2