Questions tagged [scope]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
2answers
155 views

Is `this` in JavaScript an example of dynamic scoping?

Variables in JavaScript are lexically scoped. But, I wonder, is the this keyword, referring to the receiver of a method, an example of dynamic scoping. Or is this unrelated to the lexical/dynamic ...
-1
votes
1answer
196 views

if (a) {b();c();d();} else {c();} - prevent duplicate code c(); [duplicate]

I've got the following scenario: bool bWantToWait = getRandomBool(); if (bWantToWait){ std::future<bool> bSuccesfullWait = this->doStartWait(); } //lots of lines of code //even more ...
2
votes
3answers
55 views

Should product scopes and/or project scopes be considered for small internal development work

tl;dr: I work at a small company with a development team of 5-10 people, lately we have been asked to present "scope documents" for effectively all of our work before we carry out the actual work, ...
32
votes
8answers
7k views

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, ...
1
vote
1answer
66 views

Should I group all sound file definitions together? Or define some of them locally, if possible?

For example, suppose my application have some sound files, now I define all sound file references in a single class: public class SoundHelper{ public static Sound buttonSound=new Sound("(some url)...
8
votes
7answers
900 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. ...
0
votes
2answers
160 views

Exponential Growth of Scope of C++ Projects

I've been coding in C++ professionally for six odd years, and it's my go-to language for performance-critical projects. When working to a deadline, the scope expands to fill the time available. ...
1
vote
1answer
374 views

Python Class vs File level variables

I have the following class: import Radar class Domain(Radar): URL = 'https://mxtoolbox.com/DNSLookup.aspx' ADDRESS_KEYS = ('mailing_address', 'city_name', 'state_name', 'zip_code', ...
38
votes
7answers
7k views

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 .... ...
1
vote
1answer
67 views

How are global and local names disambiguated by the compiler in three-address code?

In the book Compilers: Principles, Techniques, and Tools (2nd Edition), Appendix A, the authors offer a implementation of a compiler front-end that generates three-address code for a very simple ...
3
votes
6answers
225 views

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

No idea how to write a good title for this question. I'm thinking about introducing an operator in a DSL that makes hidden identifiers (like variables) accessible. Think this.foo in Java or C# to ...
1
vote
2answers
578 views

C# Service scope concern

I am using dependency injection on a project to inject services using Unity dependency injection private async Task<T> GetResult<T>(HttpResponseMessage response) { if (typeof(...
4
votes
1answer
725 views

Block Scoped and Function Scoped Languages

I've noticed that some languages like C, C++, Java, Perl, and .NET Visual Basic have "block" scoping which means that a variable will only be defined within the specific code block it was declared in. ...
2
votes
1answer
390 views

Is it possible to have nested functions, without having closures, in a lexically scoped language which has first class functions?

A closure is a record storing a function, together with an environment: A mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope)...
1
vote
5answers
3k views

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

I'm reading the book JavaScript: The Good Parts. On page 113 it recommends function expressions instead of function statements, because statements are subject to hoisting: The statement: function ...
2
votes
1answer
741 views

How does static scoping apply to recursive functions?

Under static scoping, by its definition, how can we determine the scope of a variable inside a recursive function? For example, in a pseudo-language, int i=1; function myfun(){ if (i > 4){ ...
0
votes
1answer
135 views

Relation between static and dynamic bindings and scopings?

From https://en.wikipedia.org/wiki/Name_binding name binding is the association of entities (data and/or code) with identifiers.[1] An identifier bound to an object is said to reference that ...
4
votes
1answer
842 views

Can dynamic typing and dynamic scoping lead to each other?

By definition, dynamic typing and dynamic scoping have different purposes. But some cases make me wonder if they can lead to each other sometimes. Dynamic typing allows a name i.e. identifier to ...
2
votes
4answers
93 views

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

I have a similar scenario to this: public class VatCalculator { public decimal GetValueWithVat(decimal value, decimal? vatRatePercentage) { if(vatRate == null) { ...
2
votes
1answer
262 views

Pure functions and the outer scope [duplicate]

Excuse my ignorance, I come from the C family of languages but zero exposure to functional languages. I've read that pure functions only generate output based on a given input. Same input gives the ...
6
votes
2answers
2k views

Getting a reference out of a try block

Using C++ I'd like to do something along the lines of: Try to get a reference to something in, say, a map If it throws, then return straight away Otherwise, go and use the reference However because ...
2
votes
2answers
179 views

Nested functions: should I explicitly pass arguments or rely on scope?

Suppose I have a function f that uses two helper functions h and g to get the job done. There are two possible ways of dealing with this situation. (1) take explicit arguments and returns new objects ...
6
votes
3answers
309 views

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

Let's say I create a Collection in an instance method. I do not assign that reference to any instance variables. Rather, I just return it to the invoker. Then, I exit. Now, the only thing with a ...
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 ...
1
vote
2answers
935 views

What is the use case for shadowing variables?

One of the things the coffeescript programming language is criticized for is its treatment of variable declarations and scope (example). The answers to this question (and the blog I linked to above) ...
41
votes
9answers
5k views

Using compound statements (“{” … “}” blocks) to enforce variable locality [duplicate]

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; +...
5
votes
1answer
2k views

Pthread scheduling (System and Process)

I've read the operating system concepts 8th edition written by abraham silberschatz. However, I don't understand Pthread Scheduling. Thread-library schedules user-level-threads on LWP(Lightweight ...
6
votes
2answers
499 views

Automatic namespace resolution in C++

My question is focused on design, and the code included in this question is meant to clarify the problem I'm experiencing. I'm interested in conceptual level answers and not code in order to answer ...
2
votes
4answers
2k views

When was block scope for variables introduced to C, and why is it still frowned upon?

In light of the recent OBJ_obj2txt vulnerability in LibreSSL (which was found during the OpenSMTPD audit, and does not affect OpenSSL), it came to my attention that the memory leak issue likely ...
4
votes
2answers
6k views

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 ...
5
votes
4answers
3k views

Why shouldn't pure functions always be static and public? [duplicate]

What are the reasons, technical or otherwise, that pure functions (functions which do not rely on external state and have no side effects) should not always be public and static? When a static and ...
13
votes
3answers
873 views

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 ...
11
votes
4answers
9k views

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 ...
1
vote
5answers
4k views

Is it bad form to use the same variable name in different scopes?

Say you have some basic code where similar operations will take place in nearby lexical scopes. Take for example some simple pseudo code: variable = "foo" # Do something with variable if (True) { ...
0
votes
1answer
46 views

Scoping template variables in “has-a” (compositional) relationships

I'm developing a new templating language for PHP, and one of the features is an object-oriented approach to nested child templates. For example, I can declare an object of type HtmlBuilder like this: ...
2
votes
1answer
1k views

How does AngularJS manage scope in ng-repeats?

I have a table that has around 60 rows and 20 columns. Each cell has a directive with an isolated scope. I use 2 nested ng-repeats to generate the table (one for rows and one for columns), but I'm ...
1
vote
1answer
55 views

Scoping behavioral variations to dynamic extents

While reading a paper in the context of Aspect Oriented Programming, I encountered following statement: Aspects enable scoping variations in space (with a rich variety of static pointcuts), in ...
3
votes
2answers
595 views

Defensive copy in the client class or in the service class?

I have one service method that receive parameters as a Map. I will need to add new attributes to this map inside the service. If I pass the original Map object to the parameter instead of a copy, the ...
1
vote
2answers
277 views

Should I sacrifice code succintness to ensure the narrowest variable scope? [duplicate]

In many languages (e.g. both Perl and Java - which are the two languages I work most with) it is possible to narrow the scope of local variables by declaring them within a block. Although it adds ...
29
votes
6answers
7k views

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 ...
3
votes
1answer
3k views

Where should variables be declared [duplicate]

Considering I have a for loop in a method of a class. Should the incremented variable be declared as member of the class, or should it be declared in the method it uses it(or even in the for loop, ...
1
vote
2answers
1k views

Scope vs Visiblity vs Lifetime vs Accessibility vs etc

I have been trying to understand what it means to say "scope of a named entity". In reading about scope, there are terms like visibility, lifetime, accessibility and more that are used. What is ...
9
votes
5answers
379 views

How to make it obvious that a function is being accessed from the outside?

This is a C specific question. I am trying to keep everything possible inside the translation unit boundaries, exposing only a few functions through the .h file. That is, I am giving static linkage to ...
2
votes
6answers
3k views

Nested Classes or Namespace

Why do need namespaces when we have nested classes. What can be done through namespaces, can also achieved through nested classes. so I don't understand the reasoning of having namespaces ?
7
votes
3answers
3k views

Event-driven vs. 'reference'-driven programming (i.e. in JavaScript)

I haven't been able to find the appropriate terminology to search for content on the web related to what I'm asking, so I'm hoping someone on here can at least point me in the right direction. I'm a ...
3
votes
2answers
286 views

How to pass parameters to a function in C

Suppose I'm writing a program in C in which several parameters are asked at the beginning of the execution to the user and then remain costant until the end. Now, I need to pass these parameters to ...
0
votes
1answer
1k views

Is there any way to limit the scope of a knockout.js application? [closed]

I have a legacy project which I have been approved to work in a knockout.js module. This is great, however the application is extremely complex and I need to use some of the pre-built form validation ...
3
votes
1answer
2k views

Does setting a function public affect the C# compiler's ability to inline the function as an optimization?

This could be either for the .NET or Mono compilers. I know that under certain conditions the compiler can inline functions (e.g. small, single call site, etc.) as an optimization. However, if the ...
4
votes
1answer
291 views

What is the advantage of determining scopes statically and using them dynamically in case of Python?

Firstly let me clarify that I know C and am learning Python. So my OOPS is kind of bad. I was reading the official tutorial and found this Although scopes are determined statically, they are used ...
0
votes
2answers
809 views

Session or request scope for account details

I have a (browser based) web application. On each page, I want to have a little label and link in a corner that displays the user's username and other account information along with a logout button. ...