Hi Mike, > What I am about to say is ironic since I too often feel people are trying to > enforce their view of coding on the world, > but I do not think we want to give people tools to make it easier to work > with long functions/scripts.
Blocked-scoped variables is a tool which can make short code and long code more readable. Making it easier to work with long functions is an example of a benefit of having block-scoped variables, not the main goal of this RFC idea. https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/scope%20%26%20closures/ch3.md#blocks-as-scopes goes into some of the other reasons to use block-scoped variables in JS - Many languages other than JavaScript/PHP support Block Scope, and so developers from those languages are accustomed to the mindset, whereas those who've primarily only worked in JavaScript may find the concept slightly foreign. - Block scope is a tool to extend the earlier "Principle of Least Privilege Exposure" from hiding information in functions to hiding information in blocks of our code. - Developers may prefer to check themselves against accidentally (re)using variables outside of their intended purpose, such as being issued an error about an unknown variable if you try to use it in the wrong place. Block-scoping (if it were possible) for the `i` variable would make `i` available only for the for-loop, causing an error if `i` is accessed elsewhere in the function. This helps ensure variables are not re-used in confusing or hard-to-maintain ways. > I would be very interested in seeing business logic coded into a long > function that literally cannot be made better by refactoring into multiple > smaller functions. > If you have some, please do post to a Gist and share it to see if it in fact > it cannot be refactored and made "better." One example is if PHP itself is used for HTML rendering in top-level statements - pages or components of a page may be spread out across hundreds of lines. This could be moved to a different templating engine for PHP instead of raw PHP, but the refactoring would be time-consuming and error-prone. Separately from HTML rendering, some problem domains are just complex. For example, consider the functions such as decodeMPEGaudioHeader in https://github.com/WordPress/WordPress/blob/master/wp-includes/ID3/module.audio.mp3.php (I'm not involved in that project or file) You could split it up into multiple helper functions, but instead of hundreds of lines in one function for a complicated file format, you'd have to jump across even more abstractions/lines to know where XYZ was set. Fully refactoring that might not make ever make business sense, but there is a benefit to having the ability to mark if a variable is actually block-scope or used elsewhere (e.g. to have a better idea of if refactoring to a different function is safe) > Given the nature of long functions, I think the awkward syntax here provides > a nice disincentive to writing overly long functions. > > But as I said, the closure does exist in case someone absolutely needs to > control > the scope of a block within an existing long function, such as the 1400 line > function I've been refactoring for months now... Also, closures aren't a convenient substitute for everything the local scope can do for other reasons. E.g. modifying by reference will mask issues with undefined or possibly undefined variables when modifying values outside of the closure scope. ``` function compute_product($values) { // Other lines omitted foreach ($values as $value) { (function() use (&$product, $value) { $letVar = $value + 1; // Other lines omitted $product *= $letVar; })(); } return $product; } ``` This has the bug that $product was never defined, but no notices are emitted because $product gets set to null when the reference is taken by reference, and php currently doesn't warn about multiplying by null. Using an actual `let $var = $value + 1`, this would get the notice for the variable being undefined. - Tyson -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php