125 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
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
  • 26.4k
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
56 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,486
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
  • 206k
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,945
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
  • 76.6k
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
  • 1,068
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
  • 206k
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

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
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

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
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
  • 14.6k
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,835
21 votes
Accepted

What should I do when optimistic locking doesn't work?

The ETag mechanism specifies only the communication protocol for optimistic locking. It's the responsibility of the application service to implement the mechanism to detect concurrent updates to ...
Lie Ryan's user avatar
  • 12.4k
21 votes
Accepted

Returning true/nothing instead of true/false?

It depends on what you mean by "OK". In the languages that I'm familiar with that have the capability to do so, a null/nil/undefined value is falsy. That means that it would be interpreted the same ...
Thomas Owens's user avatar
  • 82.3k
20 votes
Accepted

How does sleeping a thread work?

There is much more involved in running a program than just the code within that program. Any program that runs in a multi-process OS is under the control of the OS's scheduler, and the scheduler does ...
Kilian Foth's user avatar
19 votes
Accepted

Why should a language prefer indentation over explicit markers for blocks?

Guido Von Rossum From an interview with Guido Van Rossum, which can be seen in fulltext with books.google.com (emphasis mine): The choice of indentation for grouping was not a novel concept in ...
coredump's user avatar
  • 5,945
18 votes

How does sleeping a thread work?

As Doc Brown mentioned in a comment, interrupts are the key, and not just for sleeping. An interrupt is a hardware signal that the processor should stop what it's doing and run a piece of code. ...
kdgregory's user avatar
  • 5,250
17 votes

Writing a language agnostic API?

The accepted answer is more or less correct, but doesn't offer you anywhere to go if language independence is actually a burning issue for you. C++ is fairly callable from other languages (C#, Python, ...
Nathan Cooper's user avatar
16 votes

Why do so many languages restrict string literals to a single source line?

What happens when you didn't mean to have a multi-line string, but instead forgot to close the quote? The parser will chew through the code until it hits another quote in a completely different part ...
Telastyn's user avatar
  • 109k
16 votes

What to return if something failed, rather than doing something 'random'?

A popular way, amongst functional languages at least, is to use a Maybe type (or Option in some functional languages). Maybe<int> FindPosition(string stringToSearch, char charToFind) // ...
David Arno's user avatar
  • 39.2k

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