233
votes
Accepted
Why do many exception messages not contain useful details?
Exceptions do not contain useful details because the concept of exceptions has not matured yet enough within the software engineering discipline, so many programmers do not understand them fully, and ...
124
votes
How does garbage collection work in languages which are natively compiled?
Does the compiler store a copy of some garbage collection program and paste it into each executable it generates?
It sounds unelegant and weird, but yes. The compiler has an entire utility library, ...
113
votes
Accepted
Is there really a fundamental difference between callbacks and Promises?
It is fair to say promises are just syntactic sugar. Everything you can do with promises you can do with callbacks. In fact, most promise implementations provide ways of converting between the two ...
76
votes
Accepted
Is it good practice to replace division with multiplication when possible?
Two common cases to consider:
Integer arithmetic
Obviously if you are using integer arithmetic (which truncates) you will get a different result. Here's a small example in C#:
public static void ...
62
votes
Should non-trivial conditional statements be moved to the initialization section of loops?
What I'd do is something like this:
void doSomeThings() {
final x = 10;//whatever constant value
final limit = Math.floor(Math.sqrt(x)) + 1;
for(int i = 0; i < limit; i++) {
//...
60
votes
How does garbage collection work in languages which are natively compiled?
Or does the compiler include some minimal garbage collector in the compiled program's code.
That’s an odd way of saying “the compiler links the program with a library that performs garbage collection”...
55
votes
Accepted
How does garbage collection work in languages which are natively compiled?
Garbage collection in a compiled language works the same way as in an interpreted language. Languages like Go use tracing garbage collectors even though their code is usually compiled to machine code ...
51
votes
Accepted
Arguments against error suppression
Imagine code with thousands files using a bunch of libraries.
Imagine all of them are coded like this.
Imagine, for example, an update of your server causes one configuration file disappear; and now ...
48
votes
Why do many exception messages not contain useful details?
Why is it that many common exceptions from system components do not contain useful details?
In my experience, there are a number of reasons that exceptions do not contain useful information. I expect ...
47
votes
Accepted
What's the name for the 'spec' of a function/method?
Usually this is called a type signature.
A type signature includes the function's return type, the number of
arguments, the types of arguments, or errors it may pass back.
45
votes
What is the common procedure used when compilers statically type check "complex" expressions?
What is the usually method used when a compiler is type checking expressions with many operators and operands.
Read wikipages on type system and type inference and on Hindley-Milner type system, ...
44
votes
Accepted
Is extracting an interface just for testing purposes a code smell?
In the described context, there is some unstructured legacy code. Now to improve this situation, you add more structure to it by using classes and interfaces for creating sensible abstractions - just ...
43
votes
Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?
Why is x < y < z not commonly available in programming languages?
In this answer I conclude that
although this construct is trivial to implement in a language's grammar and creates value for ...
40
votes
Accepted
Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?
These are binary operators, which when chained, normally and naturally produce an abstract syntax tree like:
When evaluated (which you do from the leaves up), this produces a boolean result from x &...
38
votes
Should non-trivial conditional statements be moved to the initialization section of loops?
A good compiler will generate the same code either way, so if you are going for performance, only make a change if it is in a critical loop and you have actually profiled it and found it makes a ...
35
votes
What's the use case for formatting monetary values with a *system-dependent* currency symbol?
Is there a use-case for build-in currency formatting?
Basically, with currencies you have two ways of working:
in a currency-aware environment, where people register amount sometimes in local and ...
30
votes
Accepted
Why do heavily object-oriented languages avoid having functions as a primitive type?
IMO...
Because Java and C# are not true OO languages.
Functional programming was not in vogue when they were designed.
I agree with Jörg W Mittag, neither C# nor Java are true object-oriented ...
27
votes
Accepted
How can you TDD for a bug that can only be tested after it has been fixed?
When I understood you correctly, you cannot even write a reliable automated test for your "ghost image" example after you found a solution, since the only way of verifying the correct behaviour is to ...
27
votes
Is extracting an interface just for testing purposes a code smell?
The central misconception causing you to doubt this design is right on your question:
My doubts are that the only HasHorsePower implementation will be...well.. only the Car
If you introduce test ...
26
votes
Short circuit evaluation, is it bad practice?
Let's say you were using a C-style language with no && and needed to do the equivalent code as in your question.
Your code would be:
if(smartphone != null)
{
if(smartphone.GetSignal() > ...
26
votes
Why do heavily object-oriented languages avoid having functions as a primitive type?
This is a little bit of a silly question. You're asking why object-oriented languages are object-oriented. If they passed functions around as first class types we wouldn't describe them as object-...
25
votes
Is there a single data representation that works for all currencies (even those different from Dollars, Euros, and Pounds)?
eg, with the dollar, you never have precision of less than $0.01
Oh really?
the age old issue of why you shouldn't store currency as an IEEE 754 floating point number.
Please feel free to store ...
25
votes
Accepted
Is it possible to create a "bootstrapped" interpreter independent of the original interpreter?
The short answer is: you are right in your suspicion, you always need either another interpreter written in X or a compiler from Y to some other language for which you have an interpreter already. ...
25
votes
Is it good practice to replace division with multiplication when possible?
I like your question as it potentially covers many ideas. On the whole, I suspect the answer is it depends, probably on the types involved and the possible range of values in your specific case.
My ...
24
votes
Accepted
Writing comments for some small code with rather large background
Having a big comment section explaining "whys" and "hows" details of a complicated algorithm is a good idea. And it is better to have close to the code, so that developer does not need to switch ...
24
votes
Is it good practice to replace division with multiplication when possible?
No.
I'd probably call that premature optimization, in a broad sense, regardless of whether you're optimizing for performance, as the phrase generally refers to, or anything else that can be ...
23
votes
What's the name for the 'spec' of a function/method?
TL;DR You're probably talking about a function signature (or method signature), part of which is a type signature.
But really depends on what you consider a function "spec". I interpret it as "all ...
23
votes
How does garbage collection work in languages which are natively compiled?
How would this work with compiled languages though?
Your wording is wrong. A programming language is a specification written in some technical report (for a good example, see R5RS). Actually you are ...
21
votes
What to return if something failed, rather than doing something 'random'?
Follow the principle of least surprise for whatever language you are coding in. People are going to, at some level, expect your function to follow the paradigms and standards of your language. So if ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
language-agnostic × 548programming-languages × 52
design × 49
object-oriented × 40
design-patterns × 38
programming-practices × 31
language-design × 31
algorithms × 28
terminology × 25
coding-style × 24
object-oriented-design × 22
unit-testing × 18
naming × 15
exceptions × 14
c++ × 13
java × 12
architecture × 12
api × 12
functional-programming × 12
coding-standards × 12
compiler × 12
math × 11
web-development × 10
c# × 9
language-features × 9