What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. The technical name for it is an Immediately Invoked Function Expression (IIFE). jQuery plugins are usually written like this. In Javascript, you can nest functions. So, the following is legal: function outerFunction() { … Read more

“std::endl” vs “\n”

The varying line-ending characters don’t matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for. The only difference is that std::endl flushes the output buffer, and ‘\n’ doesn’t. If you don’t want … Read more

What’s the best way to convert a number to a string in JavaScript?

like this: var foo = 45; var bar=”” + foo; Actually, even though I typically do it like this for simple convenience, over 1,000s of iterations it appears for raw speed there is an advantage for .toString() See Performance tests here (not by me, but found when I went to write my own): http://jsben.ch/#/ghQYR Fastest … Read more

Order of items in classes: Fields, Properties, Constructors, Methods

According to the StyleCop Rules Documentation the ordering is as follows. Within a class, struct or interface: (SA1201 and SA1203) Constant Fields Fields Constructors Finalizers (Destructors) Delegates Events Enums Interfaces (interface implementations) Properties Indexers Methods Structs Classes Within each of these groups order by access: (SA1202) public internal protected internal protected private Within each of … Read more

Styling multi-line conditions in ‘if’ statements? [closed]

You don’t need to use 4 spaces on your second conditional line. Maybe use: if (cond1 == ‘val1’ and cond2 == ‘val2’ and cond3 == ‘val3’ and cond4 == ‘val4′): do_something Also, don’t forget the whitespace is more flexible than you might think: if ( cond1 == ‘val1’ and cond2 == ‘val2’ and cond3 == … Read more

Python `if x is not None` or `if not x is None`? [closed]

There’s no performance difference, as they compile to the same bytecode: >>> import dis >>> dis.dis(“not x is None”) 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 RETURN_VALUE >>> dis.dis(“x is not None”) 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 … Read more

What are the most common Python docstring formats? [closed]

Formats Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post. Note that the reST is recommended by the PEP 287 There follows the … Read more

Alternatives for returning multiple values from a Python function [closed]

Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example. >>> import collections >>> Point = collections.namedtuple(‘Point’, [‘x’, ‘y’]) >>> p = Point(1, y=2) >>> p.x, p.y 1 2 >>> p[0], p[1] 1 2 In recent versions of Python 3 (3.6+, I think), the new typing library got … Read more

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