Questions tagged [local-variable]

A variable whose scope is contained to a smaller unit of code such as a function or class.

6 questions
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
1
vote
4 answers

Is a class with a high number of member variables bad and is there a design structure for data processing?

Context: Java, fairly new developer I have inherited code from a friend for a project that processes variables. The first thing i notice is the class has a ton of member variables. I have always been under the impression that a lot of member…
0
votes
1 answer

Scheme's define in Common Lisp

In Common Lisp, we have to use the let form to declare a new lexically-scoped variable. This means that the code either looks like that written in C89 (all variables declared on top of scope), or acquires unreadably deep nesting. let* is somewhat…
ndsrib
  • 9
  • 2
0
votes
2 answers

Symbolic constants versus variables

coders. I'm learning C programming language and found out such thing as #define directive that allows us to use symbolic constants (following Brain Kernigan). I know that this directives "insert" literals or expressions right into code. Their…
CoderDesu
  • 1,005
  • 5
  • 10
0
votes
2 answers

Reassign parameter to local variable

On Stack Overflow I frequently see questions with code in the following style: function funcName(parameter) { let variable = parameter; // rest of function uses variable rather than parameter } Is this just some cargo-cult practice that some…
Barmar
  • 324
  • 2
  • 5
-1
votes
2 answers

Is there any consequences to write for(float i=0, myFloatVar=0; i < n; i++)?

For example, sometimes I need a float variable to be used inside a for loop, e.g.: float sum=0; for(int i = 0; i < students.length; i++) { sum += Math.random(); students[i].result = sum; } I feel uncomfortable that sum is accessible outside…
ocomfd
  • 5,652
  • 8
  • 29
  • 37