Skip to main content
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.6k
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
57 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
46 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
  • 210k
42 votes
Accepted

When should a function be given an argument vs getting the data itself?

When should a function be given an argument vs getting the data itself? When it has a job besides getting the data. I can see why you'd be confused about this. None of your functions are named after ...
candied_orange's user avatar
37 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
  • 78.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
  • 1,195
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
  • 210k
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 ...
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
  • 15.1k
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
  • 83.4k
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
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
Accepted

Complex variable name with plural nouns

Variable names should be grammatically correct (and spelled correctly), unless you have a compelling reason to do otherwise. So companyNames. I you are in doubt, just think about what you would write ...
JacquesB's user avatar
  • 59.9k
17 votes
Accepted

What is the common procedure used when compilers statically type check "complex" expressions?

Recursion is the answer, but you descend into each subtree before handling the operation: int a = 1 + 2 - 3 * 4 - 5 to tree form: (assign (a) (sub (sub (add (1) (2)) (mul (3) (4))) (5)) Inferring ...
Simon Richter's user avatar
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's user avatar
  • 1,319
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.4k
16 votes
Accepted

How could thread safety be provided by a programming language similar to the way memory safety is provided by Java and C#?

Races occur when you have simultaneous aliasing of an object and, at least, one of the aliases is mutating. So, to prevent races, you need to make one or more of these conditions untrue. Various ...
Alex's user avatar
  • 3,912
15 votes
Accepted

How do you deal with dependencies that require the object you inject into?

Take for example a Potato, which is arguably a type of Food. Generally, potatoes don't whip out a food processor and turn themselves into cottage pie when you put them next to ground beef and carrots. ...
Dogs's user avatar
  • 1,176

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