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


158

When I see a line like if (!lateForMeeting()), I read that as "If not late for meeting", which is quite straight-forward to understand, as opposed to if (lateForMeeting() == false) which I'd read as "If the fact that I'm late for meeting is false". They're identical in meaning, but the former is closer to how the equivalent English sentence would be ...


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


121

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


99

Writing == false and == true is redundant. It can be taken to arbitrary extremes, too. If you start writing if (condition == false) { ... } Then why not if ((condition == false) == true) { ... } Or why not if ((someExp == anotherExp) == true) { ... } The moral of this story is that if condition is a boolean expression, then you don't need to add == ...


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


87

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

In C and some similar languages, comparing boolean expressions for equality to false or true is a dangerous habit. In C any scalar expression (numeric or pointer) can be used in a boolean context, for example as the condition of an if statement. The C rule is that if (cond) is equivalent to if (cond != 0) -- i.e., zero is false, and any non-zero value is ...


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


67

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

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

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


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


54

Comments should never duplicate your code. Comments should not answer the "how?" question, but only "why?" and "what?". Why such an algorithm is chosen, what are the implicit assumptions here (unless your language is powerful enough to express it with type system, contracts and alike), what is a reason for doing this thing at all, etc. I'd recommend to take ...


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


53

Comments should describe the code, not duplicate it. This header comment just duplicates. Leave it out.


53

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

If answering the question tells you what you want to know about a candidate, then it's a good interview question. If it doesn't tell you that, it's a bad question. Easy questions like FizzBuzz do serve a specific purpose. If a candidate can't code FizzBuzz, they simply can't code and you can end the interview early. I'd rate implementing a linked list ...


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


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