Hot answers tagged

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. ...
Robert Harvey's user avatar
42 votes
Accepted

Why is it good programming practice to limit scope?

Because the more things you have to deal with in any task the harder it becomes. For example, try patting your head. Then try patting your head and counting backwards from 1000. Then try patting ...
Tom Squires's user avatar
  • 17.7k
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. ...
Erik Eidt's user avatar
  • 33.7k
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 ...
Casey Langford's user avatar
25 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 ...
esoterik's user avatar
  • 3,899
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 ...
Idan Arye's user avatar
  • 12.1k
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 ...
JacquesB's user avatar
  • 58.7k
11 votes
Accepted

Getting a reference out of a try block

The normal way is to move the "so something with x" line(s) also within the try block: std::map<char, int> mymap {...}; try { int& x = mymap.at('a'); // do something with x } catch (...
Bart van Ingen Schenau's user avatar
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 ...
Robert Harvey's user avatar
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 ...
YawarRaza7349's user avatar
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 ...
Doc Brown's user avatar
  • 206k
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 ...
JacquesB's user avatar
  • 58.7k
8 votes
Accepted

Block Scoped and Function Scoped Languages

In theory, function scope should actually be faster - variables are typically created in a stack frame, and function scope would only create that stack frame once, while block scope would need to ...
tofro's user avatar
  • 901
7 votes
Accepted

Can dynamic typing and dynamic scoping lead to each other?

Dynamic scoping and dynamic typing have two things in common: they have something to do with variables, and they both contain the word “dynamic”. What does “dynamic” mean here? Something is dynamic ...
amon's user avatar
  • 134k
7 votes
Accepted

What is the use case for shadowing variables?

The issue with the way you describe it (I don't know coffeescript myself) is that it makes it too easy to break existing code without realizing it. Let's say you have this code: bar = 5 // 100 lines ...
Gort the Robot's user avatar
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 ...
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 ...
Bruno's user avatar
  • 344
5 votes

Doesn't returning a Collection as Unmodifiable / Immutable unnecessarily break method scope?

Returning immutable data is an example of "the pit of success". In order for some other part of the system to modify it, it must explicitly convert it to a mutable data set and modify it. This reduces ...
David Arno's user avatar
  • 39.2k
5 votes

Where to declare a variable and define a function in Javascript?

Javascript variables only have 2 scopes, global and local. A variable declared inside a function (inside an IF or not) is accessible to the whole function. Personally I declare variables toward the ...
pieperu's user avatar
  • 181
5 votes

Why is it good programming practice to limit scope?

The simpler your application is, the less likely it will be to break. Adding complexity means adding places where errors can occur. Usually those errors will be in your code, but occasionally you ...
Adam Zuckerman's user avatar
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 ...
user1118321's user avatar
  • 4,993
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 ...
Errorsatz's user avatar
  • 776
5 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 ...
candied_orange's user avatar
4 votes

Language Design: skipping occurrences of an identifier instead of accessing enclosing scope

As I understand it, you're suggesting a construct: @ prefixed to an identifier, e.g. @foo, which goes to the next (2nd) visible foo starting from the current scope going to outer scopes. Thus, ...
Erik Eidt's user avatar
  • 33.7k
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 ...
user253751's user avatar
  • 4,873
3 votes

Language Design: skipping occurrences of an identifier instead of accessing enclosing scope

Think how easy it is to make an mistake. For example if someone writes @@foo = @foo instead of @foo = @@foo Now how long do you think it would take your novice programmers to spot this error. ...
Batavia's user avatar
  • 460
3 votes

How does static scoping apply to recursive functions?

Scoping tells us which i is being referred to when a line of code does so. However, in your example, there is only one i, and it is in the global scope. Since there is no local i declaration (...
Erik Eidt's user avatar
  • 33.7k
3 votes

Relation between static and dynamic bindings and scopings?

The quoted definitions of "entity" and "object" is ambiguous, we need to be more precise. A name is associated with a storage location which in turn contains a value. The value is the data. (I find it ...
JacquesB's user avatar
  • 58.7k
3 votes
Accepted

Where to declare a variable and define a function in Javascript?

My question is, where do I declare the baz variable? So the generic answer to this question is "wherever you want", but of course that's not helpful and has pitfalls, so I'm going to tell you where I ...
zzzzBov's user avatar
  • 5,814
3 votes

Should I unit test the consuming class or the class running the logic?

You should test at both the VatCalculator and at the ReceiptGenerator level. In the unit-tests for the VatCalculator class, you verify that the right adjustment to the given price is made based on ...
Bart van Ingen Schenau's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible