229 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 ...
user avatar
  • 31.4k
127 votes
Accepted

When is it a good idea to force garbage collection?

You really can't make blanket statements about appropriate way to use all GC implementations. They vary wildly. So I'll speak to the .NET one which you originally referred to. You must know the ...
user avatar
  • 15.9k
122 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, ...
user avatar
112 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 ...
user avatar
  • 27.2k
75 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 ...
user avatar
  • 24.6k
69 votes

When is it a good idea to force garbage collection?

Sadly, nobody there elaborates on what are such cases. I'll give some examples. All in all it is rare that forcing a GC is a good idea but it can be totally worth it. This answer is from my ...
user avatar
  • 2,674
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++) { //...
user avatar
58 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”...
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 ...
user avatar
51 votes

Is there a pattern for a more "natural" way of adding items to collections?

No, item.AddTo(items) it is not more natural. I think you mix this up with the following: t3chb0t.Add(item).To(items) You are right in that items.Add(item) is not very near to the natural english ...
user avatar
  • 2,409
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 ...
user avatar
  • 3,456
49 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 ...
user avatar
  • 107k
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.
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, ...
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 ...
user avatar
  • 184k
42 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 ...
user avatar
  • 5,697
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 &...
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 ...
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 ...
user avatar
  • 66.8k
34 votes

What is the benefit of a function without parameters which only calls another function

I think its useful sometimes for hiding implementation. function sayHello() { window.alert("Hello"); } And this gives you the flexibility to change it later function sayHello() { console....
user avatar
27 votes

When is it a good idea to force garbage collection?

As a general principle, a garbage collector will collect when it runs into "memory pressure", and it's considered a good idea to not have it collect at other times because you could cause performance ...
user avatar
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 ...
user avatar
  • 184k
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() > ...
user avatar
  • 2,095
26 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 ...
user avatar
25 votes

Are `break` and `continue` bad programming practices?

Yes you can [re]write programs without break statements (or returns from the middle of loops, which do the same thing). But you may have to introduce additional variables and/or code duplication both ...
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. ...
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 ...
user avatar
  • 351
24 votes
Accepted

Why don't more languages have the ability to compare a value to more than one other value?

The syntax issue is – that it requires syntax. Whatever syntax your language has, people using the language have to learn it. Otherwise they run the risk of seeing code and not knowing what it ...
user avatar
  • 12.8k

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