Function calls as parameters for other functions
I'm typically an advocate of liberal use of space, but in this situation, I'd go with the third option:

soup = BeautifulSoup(mechanize.Browser().open(__URL))


(I'm not actually sure if that's valid syntax or not. I'm not very familiar with Python [I think it's Python?].)

I find that just as readable. There are of course situations where you must separate it though. The first thing that comes to mind is error handling. I suspect that open throws exceptions, but if it were to return boolean false on failure rather than a string, then I would be a fan of option two. Option two would still be brief, but would allow for properly checking for the false return.



I think this really comes down to personal taste. There's no magical rule book for this type of thing (though I'm sure there are convoluted, borderline-dogmatic metrics somewhere that support one way or the other).

I tend to just eyeball things like this and see if they make me visually uncomfortable or not.

For example:

superLongVariableNameHere(someParamWithLongName, someFunctionLong(param1, param2), someFunc3(param, func4(param1, param2, func5(param)))


Makes me cry a little whereas:

func(g(x), y(z))


Seems perfectly fine. I just have some weird mental barrier of what length/nesting level becomes excessive.



While I'm at it, I'd also disagree with Juann Strauss' dislike of JavaScript's practically idiomatic use of anonymous and inline-defined functions. Yes, people go overboard sometimes with a 30 line callback, but for the most part, there's nothing wrong with a 1-10 line anonymous function as long as it's used in one and only one place and it doesn't overly clutter its context.

In fact, to have a closure, you often have to define functions in the middle of scopes (well, by definition you do). Often in that situation you might as well inline it within another function call rather than defining it separately (once again, provided the body is brief).

If he means this though, then yeah, I'd agree 95% of the time:

f((function(x) { return x * x; }(5))


That gets ugly fast.