237 votes
Accepted

Should we eliminate local variables if we can?

Code is read much more often than it is written, so you should take pity on the poor soul who will have to read the code six months from now (it may be you) and strive for the clearest, easiest to ...
Tony BenBrahim's user avatar
119 votes
Accepted

Clean Code: Functions with few parameters

I don't share your opinion. In my opinion using global variables is a worse practice than more parameters irrespective of the qualities you described. My reasoning is that more parameters may make a ...
Samuel's user avatar
  • 9,167
109 votes

How do variables in C++ store their type?

Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data ...
amon's user avatar
  • 133k
103 votes
Accepted

Why do so few languages with a variable-type 'operator' exist?

Operators are just functions under funny names, with some special syntax around. In many languages, as varied as C++ and Python, you can redefine operators by overriding special methods of your class....
9000's user avatar
  • 24.2k
98 votes

Should we eliminate local variables if we can?

Some highly upvoted comments stated this, but none of the answers I saw did, so I will add it as an answer. Your main factor in deciding this issue is: Debuggability Often, developers spend far more ...
DVK's user avatar
  • 3,576
73 votes

Using compound statements ("{" ... "}" blocks) to enforce variable locality

It is indeed a good practice to keep your variable's scope small. However, introducing anonymous blocks into large methods only solves half the problem: the scope of the variables shrinks, but the ...
Kilian Foth's user avatar
71 votes
Accepted

Why are variables declared without a value in C?

Versions of C up to and including C89 (i.e. the language version standardised in 1989; note this was the last major revision to the C standard before 1999) allowed variables to be declared only at the ...
Philip Kendall's user avatar
69 votes

Clean Code: Functions with few parameters

You should avoid global variables like the plague. I wouldn't put a hard limit to number of arguments (like 3 or 4), but you do want to keep them to a minimum, if possible. Use structs (or objects ...
robert bristow-johnson's user avatar
69 votes
Accepted

What's the difference between a variable and a memory location?

A variable is a logical construct that goes to the intent of an algorithm, whereas a memory location is a physical construct that describes the operation of a computer.  Generally speaking, in order ...
Erik Eidt's user avatar
  • 33.6k
60 votes
Accepted

Do inline expressions obstruct readability of code?

Personally I prefer having temporary variables with explicit names (but don't abuse them either). For me: void foo() { int const number_of_elements = (function_1() + function_2()) / function_3(); /...
f222's user avatar
  • 1,020
58 votes

Clean Code: Functions with few parameters

We're talking about cognitive load, not syntax. So the question is... What is a parameter in this context? A parameter is a value which affects the behaviour of the function. The more parameters, the ...
Quentin's user avatar
  • 1,475
54 votes

How do variables in C++ store their type?

The other answer explains well the technical aspect, but I'd like to add some general "how to think about machine code". The machine code after the compilation is pretty dumb, and it really just ...
Frax's user avatar
  • 1,844
48 votes

Should we eliminate local variables if we can?

Only if it makes the code easier to understand. In your examples, I think it makes it harder to read. Eliminating the variables in the compiled code is a trivial operation for any respectable ...
whatsisname's user avatar
  • 27.7k
42 votes
Accepted

Why should I use namedtuple over SimpleNamespace when not using dict, they seem very similar

SimpleNamespace is basically just a nice facade on top of a dictionary. It allows you to use properties instead of index keys. This is nice as it is super flexible and easy to manipulate. The ...
unholysampler's user avatar
40 votes

Why are variables declared without a value in C?

It's not just a question of sytle. The two ways of declaring the variable are not equivalent: In the first case, i exists after the loop and you could use it. This is useful, for example if the ...
Christophe's user avatar
  • 75.9k
38 votes

Clean Code: Functions with few parameters

Having many parameters is considered undesirable, but turning them into fields or global variables is a lot worse because it doesn't solve the actual problem but introduce new problems. Having many ...
JacquesB's user avatar
  • 57.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.6k
35 votes

Clean Code: Functions with few parameters

IMHO your question is based on a misunderstanding. In "Clean Code", Bob Martin does not suggest to replace repeated function parameters by globals, that would be a really awful advice. He suggest to ...
Doc Brown's user avatar
  • 203k
33 votes
Accepted

What does "set" mean in programming languages like C#?

This question is one of English semantics, not programming, which initially urged me to vote to close this question as being off topic. However, because "set" is notoriously the word with ...
Flater's user avatar
  • 47.5k
29 votes

Should we eliminate local variables if we can?

Your question "is it a good practice to eliminate a local variable if it is just used one time in the scope?" is testing the wrong criteria. A local variable's utility does not depend on the number of ...
Jack Aidley's user avatar
  • 2,954
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
26 votes

Do inline expressions obstruct readability of code?

I think there are two key factors here: How complex is the expression? How meaningful is the intermediate variable? Take an example where we have three elements to get a final price: total = price + ...
IMSoP's user avatar
  • 5,742
24 votes
Accepted

How to name a Boolean variable that represents either of two options?

Don't use a Boolean. Use an enum. E.g TransactionType with instances Buy and Sell. That is unambiguous and far easier to understand. If you want to persist the data efficiently, the boolean can be a ...
marstato's user avatar
  • 4,548
22 votes

Using compound statements ("{" ... "}" blocks) to enforce variable locality

Often if you find places to create such a scope it's an opportunity to extract out a function. In a language with pass-by-reference you would instead call swap(x,y). For writing the file that would ...
ratchet freak's user avatar
21 votes
Accepted

How are variables stored in a language compiler or interpreter?

Interpreter An intepreter will work about the way you guessed. In a simple model, it will maintain one dictionary with the variable names as dictionary keys and the variable values as dictionary ...
Ralf Kleberhoff's user avatar
20 votes

Why does Kotlin require type after variable, rather than before?

From Kotlin's FAQ: Why have type declarations on the right? We believe it makes the code more readable. Besides, it enables some nice syntactic features, for instance, it is easy to leave type ...
Andres F.'s user avatar
  • 5,139
20 votes

What's the difference between a variable and a memory location?

Is it safe to say that a variable is the same thing as a memory location? No. Variable and memory location are two abstractions at two different abstraction levels. Variable and pointers are higher ...
Lie Ryan's user avatar
  • 12.3k
18 votes
Accepted

Why are variables in Python different from other programming languages'?

This is not a Python vs Other Languages distinction - it's actually Value Types vs Reference Types distinction. Python uses reference types, and while many modern languages also tend to use reference ...
Idan Arye's user avatar
  • 12.1k
18 votes
Accepted

Is there a convention for returning multiple items?

I think the choices need to be considered strictly from the caller's point of view: what is the consumer most likely to need to do? And what are the salient features of each collection? The tuple ...
jscs's user avatar
  • 838
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

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