Why is the code in most STL implementations so convoluted?

About the variables names, library implementors must use “crazy” naming conventions, such as names starting with an underscore followed by an uppercase letter, because such names are reserved for them. They cannot use “normal” names, because those may have been redefined by a user macro. Section 17.6.3.3.2 “Global names” ยง1 states: Certain sets of names … Read more

Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

Don’t put anything between the strings. Part of the C++ lexing stage is to combine adjacent string literals (even over newlines and comments) into a single literal. #include <iostream> #include <string> main() { std::string my_val =”Hello world, this is an overly long string to have” ” on just one line”; std::cout << “My Val is … Read more

How to find good looking font color if background color is known? [closed]

If you need an algorithm, try this: Convert the color from RGB space to HSV space (Hue, Saturation, Value). If your UI framework can’t do it, check this article: http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_RGB_to_HSL_or_HSV Hue is in [0,360). To find the “opposite” color (think colorwheel), just add 180 degrees: h = (h + 180) % 360; For saturation and … Read more

Should a developer aim for readability or performance first? [closed]

You missed one. First code for correctness, then for clarity (the two are often connected, of course!). Finally, and only if you have real empirical evidence that you actually need to, you can look at optimizing. Premature optimization really is evil. Optimization almost always costs you time, clarity, maintainability. You’d better be sure you’re buying … Read more

`if key in dict` vs. `try/except` – which is more readable idiom?

Exceptions are not conditionals. The conditional version is clearer. That’s natural: this is straightforward flow control, which is what conditionals are designed for, not exceptions. The exception version is primarily used as an optimization when doing these lookups in a loop: for some algorithms it allows eliminating tests from inner loops. It doesn’t have that … Read more

How to split a long regular expression into multiple lines in JavaScript?

Extending @KooiInc answer, you can avoid manually escaping every special character by using the source property of the RegExp object. Example: var urlRegex= new RegExp(” + /(?:(?:(https?|ftp):)?\/\/)/.source // protocol + /(?:([^:\n\r]+):([^@\n\r]+)@)?/.source // user:pass + /(?:(?:www\.)?([^\/\n\r]+))/.source // domain + /(\/[^?\n\r]+)?/.source // request + /(\?[^#\n\r]*)?/.source // query + /(#?[^\n\r]*)?/.source // anchor ); or if you want to … Read more

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