#ifdef vs #if – which is better/safer as a method for enabling/disabling compilation of particular sections of code?

My initial reaction was #ifdef, of course, but I think #if actually has some significant advantages for this – here’s why: First, you can use DEBUG_ENABLED in preprocessor and compiled tests. Example – Often, I want longer timeouts when debug is enabled, so using #if, I can write this DoSomethingSlowWithTimeout(DEBUG_ENABLED? 5000 : 1000); … instead … Read more

if checkbox is checked, do this

I would use .change() and this.checked: $(‘#checkbox’).change(function(){ var c = this.checked ? ‘#f00’ : ‘#09f’; $(‘p’).css(‘color’, c); }); — On using this.checked Andy E has done a great write-up on how we tend to overuse jQuery: Utilizing the awesome power of jQuery to access properties of an element. The article specifically treats the use of … Read more

Is using if (0) to skip a case in a switch supposed to work?

Yes, this is supposed to work. The case labels for a switch statement in C are almost exactly like goto labels (with some caveats about how they work with nested switch statements). In particular, they do not themselves define blocks for the statements you think of as being “inside the case”, and you can use … Read more

Most elegant way to write a one-shot ‘if’

Use std::exchange: if (static bool do_once = true; std::exchange(do_once, false)) You can make it shorter reversing the truth value: if (static bool do_once; !std::exchange(do_once, true)) But if you are using this a lot, don’t be fancy and create a wrapper instead: struct Once { bool b = true; explicit operator bool() { return std::exchange(b, false); … Read more

Regex matching in a Bash if statement

There are a couple of important things to know about bash’s [[ ]] construction. The first: Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. The second thing: An additional binary … Read more

jQuery: Test if checkbox is NOT checked

One reliable way I use is: if($(“#checkSurfaceEnvironment-1”).prop(‘checked’) == true){ //do something } If you want to iterate over checked elements use the parent element $(“#parentId”).find(“checkbox”).each(function(){ if ($(this).prop(‘checked’)==true){ //do something } }); More info: This works well because all checkboxes have a property checked which stores the actual state of the checkbox. If you wish you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)