234

It appears that these variable names are based on the abbreviations you'd expect to find in a physics textbook working various optics problems. This is one of the situations where short variable names are often preferable to longer variable names. If you have physicists (or people that are accustomed to working the equations out by hand) that are ...


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


89

Variables with short lifetimes should be named shortly. As an example, you don't write for(int arrayCounter = 0; arrayCounter < 10; arrayCounter++) { .... Instead, you use for(int i .... In general rule of thumb it could be said that the shorter the variable scope the shorter the name should be. Loop counters are often only single letters, say i, j and k....


88

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


78

An objective response: While my initial response to this question was based on my empirical experience as a soon-to-graduate CS student and my projected opinion of the type of people I wanted to work with in the CS field. There is actually an objective (with respect to the subjective opinions of the ACM SIGCSE and IEEE computing societies) answer. Every 10 ...


73

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) { ...


68

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


60

All names should be meaningful. If _ was a well known standard at your company or in the wider community, then it would be meaningful as a "name that does not matter". If it's not, I would say it's bad practice. Use a descriptive name for what you refer to, especially since the name might matter in the future.


60

It's not cheating. Programs like IB are tools. Use the right one for the job. There is no need to get dogmatic about it. If you are more effective using such a tool, use it. The only caveat is that you should learn the trade-offs when making your decisions. Doing layouts by hand give you precise control at the expense of drag-and-drop ease. Drag-and-drop ...


60

Ok, going down the list: I was taught long ago that instantiated objects in a loop is generally a bad idea Not in any languages I've used. In C it is a good idea to declare your variables up front, but that is different from what you said. It might be slightly faster if you declare objects above the loop and reuse them, but there are plenty of languages ...


59

The usual reason for writing numbers, in code, in other than base 10, is because you're bit-twiddling. To pick an example in C (because if C is good for anything, it's good for bit-twiddling), say some low-level format encodes a 2-bit and a 6-bit number in a byte: xx yyyyyy: main() { unsigned char codevalue = 0x94; // 10 010100 printf("x=%d, y=%d\n"...


58

Using Maybe (or its cousin Either which works basically the same way but lets you return an arbitrary value in place of Nothing) serves a slightly different purpose than exceptions. In Java terms, it's like having a checked exception rather than a runtime exception. It represents something expected which you have to deal with, rather than an error you did ...


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


57

That reminds me this code: if ( ... ) try { .. } catch (Exception e) { .. } else { ... } Every time you are combining two types of blocks, forgetting braces and not increasing indentation, you are creating code very difficult to understand and maintain.


55

Perhaps it is because I learnt my trade (way back when) using the Jackson Entity Structure Diagram method, but I subscribe to the view that the only correct unbraced term after an if or an else is a subsequent if (ie allowing an else if ladder) Anything else (no pun intended) leaves potential for misunderstanding and/or maintenance issues. That is the ...


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


54

Should comments say WHY the program is doing what it is doing? Unequivocally yes. There don't necessarily need to be many comments, mind you, but if you have them, WHY is the only question worth answering outside of a few bizarre fringe scenarios. The reasoning is simple. If I read your code, good or bad, I can see what the program is doing. I have no idea ...


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


50

Short answer: We don't. Both examples are absolutely fine. There are three reasons why people use temporary variables anyway (like in your first example): It gives an explicit name to the intermediate value (we now know that it's a message, not just any old string). It helps prevent statements getting too long and too complex. It makes step-debugging ...


48

The problem with the code is not the short names, but rather the lack of a comment which would explain the abbreviations, or point to some helpful materials about the formulas from which the variables are derived. The code simply assumes the problem-domain familiarity. That is fine, since problem-domain familiarity is probably required to understand and ...


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

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

The main reason I use different bases is when I care about bits. It's much easier to read int mask=0xFF; byte bottom_byte = value & mask; than int mask=255; byte bottom_byte = value & mask; Or image something more complex int mask=0xFF00FF00; int top_bytes_by_word = value & mask; compared to int mask=4278255360; //can you say magic ...


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