Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news-peer.in.tum.de!news.belwue.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1543943.qVoOGUtdWV@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 19 Nov 2011 16:05:34 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Function arguments vs. local variables (was: Difference between findPos("divThis") and findPos(divThis)) Newsgroups: comp.lang.javascript References: <4775241.ypaU67uLZW@PointedEars.de> <2394720.ArG6xLiZAS@PointedEars.de> <4ec07ef1$0$28711$a8266bb1@newsreader.readnews.com> <4ec11fb1$0$28605$a8266bb1@newsreader.readnews.com> <4ec15e82$0$28617$a8266bb1@newsreader.readnews.com> <9iles3FnvhU1@mid.individual.net> <1550675.qVoOGUtdWV@PointedEars.de> <9imn3lF61bU1@mid.individual.net> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 112 NNTP-Posting-Date: 19 Nov 2011 16:05:35 CET NNTP-Posting-Host: efc80068.newsspool2.arcor-online.net X-Trace: DXC=5d_m@SRTjcjFXUDVUnEXQmA9EHlD;3Ycb4Fo<]lROoRa8kFo\TI>PGgX` X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:8423 Frobernik wrote: > Thomas 'PointedEars' Lahn wrote: >> Frobernik wrote: >>> function findPos(obj, name, colour, b, c) { >>> name = obj.name; >>> colour = obj.colour; >>> b = 17; >>> c = [{s:'monkey', x:'male', a:3}, {s:'elephant',x:'female',a:7}]; >>> } >>> findPos({name:'df', colour:'green'}) >> >> We have been over this. > > We have? Yes. We, the previous subscribers of this newsgroup, have. You should read a fair amount of past discussions before you post to a newsgroup: >> Declaring arguments instead of local variables may be great for code >> golfing (140 characters maximum), but it is a Really Bad Idea for all >> other code. To begin with, by looking at the code you cannot tell whether >> you are modifying an argument or not. Editors and linters which can >> differentiate between arguments and local variables could not tell as >> well. > > An editor or linters not going to know more about my code than me Whether that is true depends on what you *actually* know about your code. I know at least one ECMAScript-supporting editor, which includes a linter, that can differentiate between function arguments and local variables: Eclipse JavaScript Developer Tools (JSDT). It is rather obvious that this is possible for a machine because each /FunctionDeclaration/ or /FunctionExpression/ has an argument list, and identifiers need to be *declared* variable names in order to be *variable* names. So the following heuristics can be applied to a standalone /IdentifierName/ within a function body: N¹ A V I B Meaning ------------------------------------------------------------------------- − − − − + Probable ReferenceError (all modes) − − − + + Possible "Implied global", i. e. property of an object in the function scope's scope chain created; possible ReferenceError (strict mode) − − + − − Local variable, unused − − + − + Local variable, used − − + + + Local variable (initialized), used – + − − – Function argument, unused – + − − + Function argument, used − + − + + Function argument (used), possible default value init. − + + − − Local variable (unused), hiding a function argument − + + − + Local variable (used, uninitialized), hiding a function argument − + + + + Local variable (used), hiding a function argument + − − − − Non-local property or variable + − − − + Property in the function scope's scope chain (perhaps uninitialized), used + − − + + Property in the function scope's scope chain, used + − + − − Local variable (unused), hiding a property in the function scope's scope chain + − + − + Local variable (used, uninitialized), perhaps hiding a property in the function scope's scope chain + − + + + Local variable (used), hiding a property in the function scope's scope chain + + − − – Function argument (unused), perhaps hiding a property in the function scope's scope chain + + − − + Function argument (used), perhaps hiding a property in the function scope's scope chain + + − + + Function argument (used, perhaps assigned to), perhaps hiding a property in the function scope's scope chain + + + − − Local variable (unused), hiding a function argument, which hides a property in the function scope's scope chain + + + − + Local variable (used), hiding a function argument, which hides a property in the function scope's scope chain + + + + + Local variable (used, initialized), hiding a function argument, which hides a property in the function scope's scope chain _____ ¹) N: Non-local occurence A: Occurence in function's argument list V: VariableDeclaration in function body I: Initialization/assignment in function body B: Occurence in function body +: Applies −: Does not apply In an implementation of similar heuristics, Eclipse JSDT allows function arguments and variables to be displayed differently. For example, I have set it up so that it would display argument declarations and references in bluish italic characters; the identifier in variable declarations in regular style, but underlined; and local variable references in normal-colored italic characters. If I were to use your approach, I could not tell at a glance if an identifier was an argument or a local variable name. I could be ending up assigning to arguments, inadvertently altering the program flow after that assignment. If someone would call my function, and I would forget to assign to the argument but used it later, they could, intentionally or accidentally, alter the inner workings of my function. AISB, a Really Bad Idea for a number of reasons, another one being that in an API you only expose to the world what needs to be exposed to it. PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from (404-comp.)