As of May 31, 2023, we have updated our Code of Conduct.
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 ...
Mike Nakis's user avatar
  • 31.9k
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, ...
Kilian Foth's user avatar
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 ...
Ixrec's user avatar
  • 27.6k
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 ...
John Wu's user avatar
  • 25.8k
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++) { //...
candied_orange's user avatar
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”...
Konrad Rudolph's user avatar
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 ...
avdgrinten's user avatar
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 ...
Walfrat's user avatar
  • 3,456
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 ...
Telastyn's user avatar
  • 109k
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.
Kilian Foth's user avatar
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, ...
Basile Starynkevitch's user avatar
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 ...
Doc Brown's user avatar
  • 196k
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 ...
Aaron Hall's user avatar
  • 5,875
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 &...
Karl Bielefeldt's user avatar
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 ...
Karl Bielefeldt's user avatar
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 ...
Christophe's user avatar
  • 72.7k
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 ...
Schwern's user avatar
  • 800
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 ...
Doc Brown's user avatar
  • 196k
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 ...
Greg Burghardt's user avatar
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() > ...
Jon Hanna's user avatar
  • 2,095
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-...
Telastyn's user avatar
  • 109k
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 ...
candied_orange's user avatar
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. ...
Jörg W Mittag's user avatar
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 ...
dave's user avatar
  • 351
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 ...
Kromster's user avatar
  • 468
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 ...
svidgen's user avatar
  • 13.3k
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 ...
kdbanman's user avatar
  • 1,447
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 ...
Basile Starynkevitch's user avatar
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 ...
Becuzz's user avatar
  • 4,805

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