Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17937

Re: Two versions of code - advantages and differences

From Gregor Kofler <usenet@gregorkofler.com>
Newsgroups comp.lang.javascript
Subject Re: Two versions of code - advantages and differences
Date 2013-01-04 11:25 +0100
Organization A noiseless patient Spider
Message-ID <kc6am2$b32$1@dont-email.me> (permalink)
References <kc4uud$fam$1@speranza.aioe.org>

Show all headers | View raw


Am 2013-01-04 09:15, Cezary Tomczyk meinte:
 > W dniu 2013-01-04 00:41, Stefan Weiss pisze:
 >> On 2013-01-03 22:58, Cezary Tomczyk wrote:
 >>> Version 1
 >>>
 >>> var el = document.getElementById('test');
 >>> var fn = function(){
 >>>       if( !el ){
 >>>           // fallback if el is not available and then return
 >>>       }
 >>>       return el;
 >>> };
 >>>
 >>> Version 2
 >>>
 >>> var fn = (function(){
 >>>       var el = document.getElementById('test');
 >>>
 >>>       if(el){
 >>>           return function(){
 >>>         return el;
 >>>           }
 >>>       } else {
 >>>           // fallback if el is not available and then return
 >>>       }
 >>> }());
 >>>
 >>> Correct me, if I am wrong.
 >>>
 >>> a) Version 1 has an advantage over Version 2, because Version 2 is
 >>> slower (?) and consumes more memory (?).
 >>> b) Version 2 has a closure and inside fn everything is not available
 >>> outside (except what return "return").
 >>
 >> Your interpretation is broadly correct, but the example is too trivial
 >> to make a good distinction (especially concerning performance or
 >> memory). If that's all there is to it, version 1 would usually be
 >> preferred for its simplicity.
 >
 > As I mentioned the examples is just a simple examples. Yes, they are
 > trivial, but just wanted to show general idea. They can contains more
 > complicated code.
 >
 >> There are several situations where a closure like version 2 would be
 >> better suited. For example:
 >>
 >> - When the function performs something computationally expensive (or
 >> taking a long time), the result will be "remembered" and available for
 >> subsequent calls to 'fn'.
 >
 > But same can be done in Version 1.

At the cost of requiring more non-private variables.

 >> - When, depending on the availability of 'el', two different functions
 >> will be returned.
 >
 > That's true, but this can be done also in Version 1. Functions can be
 > defined and returned.

When you start returning functions, then you end up with version 2.

 >> - When it uses an internal state (for example, a counter).
 >
 > This also can be done in Version 1 and I do not see advantage here over
 > Version 2.

See above.

 >> - When it uses private helper functions that are useless in the
 >> surrounding scope.
 >
 > Yes. This is the advantage Version 2 over Version 1. Of course, in some
 > cases and it depend on what is required.
 >
 >> In general, if the same goal can be achieved in a simple and a
 >> complicated way, choose simple :)
 >
 > I agree :-) Just wanted to get some confirmations if I am thinking in
 > the right way ;-)
 >
 >> By the way, one way of keeping it simple would be to use
 >>    function fn () {}
 >> instead of
 >>    var fn = function () {};
 >> for version 1.
 >>
 >> Some people recommend to always use function expressions, but IMO, if
 >> you know how variable and function declarations are handled when an
 >> execution context is entered, this is unnecessary in most cases.
 >
 > You mean this?
 > http://stackoverflow.com/a/3344397

As long as you understand the differences between the two you can use 
both (or prefer whatever meets your personal preference).

Gregor

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-03 22:58 +0100
  Re: Two versions of code - advantages and differences Stefan Weiss <krewecherl@gmail.com> - 2013-01-04 00:41 +0100
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 09:15 +0100
      Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 11:26 +0100
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 14:29 +0100
          Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 14:56 +0100
            Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 16:52 +0100
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 09:19 +0100
  Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 11:25 +0100
  Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-04 15:31 +0100
    Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 08:37 -0800
      Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-05 18:14 +0100
        Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 10:51 -0800
          Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-05 20:46 +0100
            Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 17:12 -0800
              Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 17:43 -0800
              Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 03:58 +0100
                Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 19:23 -0800
                Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 05:19 +0100
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 22:30 +0100
      Re: Two versions of code - advantages and differences Stefan Weiss <krewecherl@gmail.com> - 2013-01-07 02:35 +0100
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 22:45 +0100
      Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 04:36 +0100
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 23:06 +0100
          Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-08 12:35 +0100
            Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-09 08:34 +0100
  Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 09:13 -0800
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 11:09 +0100
      Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 12:56 -0800
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 22:53 +0100
  Re: Two versions of code - advantages and differences Luc Yen <luc@goal.tw> - 2013-01-05 12:54 -0800
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 21:38 +0100

csiph-web