Skip to main content
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,197
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
  • 135k
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,586
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
  • 34.2k
61 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,040
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
45 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
  • 78.7k
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
  • 59.9k
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
  • 34.2k
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
  • 210k
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
  • 52.9k
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,962
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,877
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,628
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

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.4k
19 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
  • 848
17 votes
Accepted

Complex variable name with plural nouns

Variable names should be grammatically correct (and spelled correctly), unless you have a compelling reason to do otherwise. So companyNames. I you are in doubt, just think about what you would write ...
JacquesB's user avatar
  • 59.9k
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
17 votes

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

Variables are language constructs. They have a name, reside within a scope, may be referenced by other parts of the code, etc. They are a logical entity. The compiler is free to implement this ...
cmaster - reinstate monica's user avatar
16 votes

Do inline expressions obstruct readability of code?

“Refactoring” means changing what the code looks like, without changing what it does. A book about refactoring will tell you about changes you can make and how to make them without affecting what your ...
gnasher729's user avatar
  • 46.1k
15 votes

Should we eliminate local variables if we can?

Maybe. Personally I would hesitate eliminating local variables if a typecast is involved. I find your compact version less readable as the number of brackets start approaching a mental limit that I ...
9Rune5's user avatar
  • 337
15 votes

How to prevent 'global variables' in a big project?

In a big C++ project I would like to have a mechanism [...] to prevent such data in most parts. There is such a mechanism. It is called code reviews - especially when done by the experienced guys ...
Doc Brown's user avatar
  • 210k

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