215

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 therefore they do not treat them properly. Yes, IndexOutOfRangeException should contain the precise index that was out of range, as well as the range that was ...


126

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 behaviour of the GC pretty intimately to do this with any logic or reason. The only advice on collection I can give is: Never do it. If you truly know the ...


122

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, containing a whole lot more than just garbage collection code, and calls to this library will be inserted into each executable it creates. This is called the ...


110

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 whenever you want. The deep reason why promises are often better is that they're more composeable, which roughly means that combining multiple promises "just ...


90

There are two major concepts in confusion: binding and loading. It is conflated by the concept of DataBinding, which is somewhere in the middle often doing both. After considering it, I am going to add one more concept, to complete the trifecta, dispatch. Types Late Binding: type is unknown until the variable is exercised during run-time; usually ...


75

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 TestIntegerArithmetic() { int newValue = 101; int oldValue = 10; int SOME_CONSTANT = 10; if(newValue / oldValue > SOME_CONSTANT) { ...


69

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 experience with .NET and GC literature. It should generalize well to other platforms (at least those that have a significant GC). Benchmarks of various kinds. You want ...


62

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++) { //...do something } } Honestly the only good reason to cram initializing j (now limit) into the loop header is to keep it correctly scoped. All it takes to ...


58

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”. But yes, that’s what’s happening. This is nothing special: compilers usually link tons of libraries into the programs they compile; otherwise compiled ...


55

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 ahead-of-time. (Tracing) garbage collection usually starts by walking the call stacks of all threads that are currently running. Objects on those stacks are ...


52

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 all you have is a stack trace is a null pointer exception when you try using that class: how would you resolve that? It could take hours, where at least just ...


51

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 language. But you also don't hear item.AddTo(items) in natural english language, do you? Normally there is someone who supposed to add the item to the list. Be it ...


48

The deeper pattern is that we naturally use "[thing that varies] [comparison] [thing that does not vary]" as the standard order. This principle holds true for your example because position may vary, while size will not. The only common exception is when testing for equality some programmers train themselves to use the opposite order (known as Yoda ...


47

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 that these sorts of reasons would also apply to system components - but I don't know for sure. Security focused people see exceptions as a source of ...


47

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

You learn how to write programs by writing programs. But you gotta start small, man. public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } From there, begin building... public class HelloWorld { static String test = "This is a test"; public static void main(String[] args) { ...


44

If you care enough about rolling out the new hashing scheme to all users as quickly as possible (e.g. because the old one is really insecure), there is actually a way for instantaneous "migration" of every password. The idea is basically to hash the hash. Rather than waiting for users provide their existing password (p) upon next login, you immediately use ...


43

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, which uses unification. Read also about denotational semantics and operational semantics. Type checking can be simpler if: all your variables like a are ...


42

The usual term is "transcompilation" - not a common term, but the technically correct one. It's not just for trendy new scripting languages (CoffeeScript -> JavaScript) either; the first C++ compilers were in fact transcompilers to C.


42

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 language users, the primary reasons that this does not exist in most languages is due to its importance relative to other features and the unwillingness of the ...


40

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 < y, then you get a type error trying to do boolean < z. In order for x < y < z to work as you discussed, you have to create a special case in the ...


38

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 difference. Even if the compiler can't optimize it, as people have pointed out in the comments about the case with function calls, in the vast majority of ...


35

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 sometimes in foreign currency: you will never use the default built-in feature. Instead you’ll store a currency amount and a currency code. in a currency-neutral ...


34

to hide what the core cause was in places where it doesn't matter the top level only needs to know that a storage exception occurred instead of an SQLException, which may not happen if you decide to migrate to a non-sql data store not wrapping also leaks the abstraction and requires reimplementation of the top level when doing a migration of a lower level ...


34

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.log("Hello"); }


33

Is it bad practice to change a simple statement like this to the following?: bool test2 = (str == "checked"); No, it's good practice. To me, the longer code: if (str == "checked") { test1 = true; } else { test1 = false; } indicates that the programmer doesn't understand Boolean expressions. The shorter form is much clearer. Similarly, don't ...


27

Sorry gents, but the word for this is just "compiled". It means turning one type of code into another type of code. It makes no real difference if that code is English, C++, x86 machine code, ARM machine code, Java, bytecode, or back to English. We generally use it to mean the trip from programming language to machine code, but that's an aspect the word has ...


27

Anything that is decided by compiler while compiling can be refer to EARLY/COMPILE TIME Binding and anything that is to be decided at RUNTIME is called LATE/RUNTIME binding. For Example, Method Overloading and Method Overriding. 1) In Method Overloading your method calls to the methods are decided by the compiler in the sense that which function is going ...


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