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 ...
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 ...
user avatar
  • 9,007
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 ...
user avatar
  • 123k
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....
user avatar
  • 23.8k
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 ...
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 ...
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 ...
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 ...
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 ...
user avatar
  • 32.3k
59 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(); /...
user avatar
  • 944
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 ...
user avatar
  • 1,465
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 ...
user avatar
  • 1,774
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 ...
user avatar
  • 27.2k
47 votes
Accepted

Why do we have to mention the data type of the variable in C

You are comparing variable declarations to #defines, which is incorrect. With a #define, you create a mapping between an identifier and a snippet of source code. The C preprocessor will then literally ...
user avatar
  • 123k
42 votes
Accepted

Where should variables be declared

Rule of thumb: variables should always be in - or as close as possible to - the scope where they are needed. Another way to phrase it is that variables should be enclosed inside the context in which ...
user avatar
  • 1,617
40 votes

Why is *declaration* of data and functions necessary in C language, when the definition is written at the end of the source code?

A design constraint of the C language was that it was supposed to be compiled by a single-pass compiler, which makes it suitable for very memory-constrained systems. Therefore, the compiler knows at ...
user avatar
  • 123k
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 ...
user avatar
  • 54.6k
38 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 ...
user avatar
  • 66.8k
37 votes

Is the use of one-letter variables encouraged?

Properly naming things is hard. Very hard. If you look at it the other way, you can also take this to mean that properly named things are important. (Otherwise, why would you have spent the effort ...
user avatar
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. ...
user avatar
  • 32.3k
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 ...
user avatar
  • 184k
33 votes
Accepted

In Python 3.4+, 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 ...
user avatar
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 ...
user avatar
  • 37.3k
30 votes
Accepted

Why is *declaration* of data and functions necessary in C language, when the definition is written at the end of the source code?

Because C is a single-pass, statically-typed, weakly-typed, compiled language. Single-pass means the compiler does not look ahead to see the definition of a function or variable. Since the compiler ...
user avatar
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 ...
user avatar
  • 2,938
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 ...
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 + ...
user avatar
  • 4,941
23 votes

Is the use of one-letter variables encouraged?

If your loop does nothing but use a variable for counting for(int i = 0; i < 10; i++) { System.out.println("Stop it! I really mean it!!"); } then yes, this is the best name you could use....
user avatar
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 ...
user avatar
20 votes
Accepted

Why to declare a String (as final) and then use it?

At runtime, it does not make a difference. The point is readability - as a member variable, it likely is declared at the beginning of the class' source code, and making it static means it doesn't ...
user avatar

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