Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

前端 未结 16 1009
庸人自扰
庸人自扰 2020-11-22 06:23

One of the tips for jslint tool is:

++ and --
The ++ (increment) and -- (decrement) operators have been known to contribute

16条回答
  •  孤街浪徒
    2020-11-22 06:44

    Is Fortran a C-like language? It has neither ++ nor --. Here is how you write a loop:

         integer i, n, sum
    
          sum = 0
          do 10 i = 1, n
             sum = sum + i
             write(*,*) 'i =', i
             write(*,*) 'sum =', sum
      10  continue
    

    The index element i is incremented by the language rules each time through the loop. If you want to increment by something other than 1, count backwards by two for instance, the syntax is ...

          integer i
    
          do 20 i = 10, 1, -2
             write(*,*) 'i =', i
      20  continue
    

    Is Python C-like? It uses range and list comprehensions and other syntaxes to bypass the need for incrementing an index:

    print range(10,1,-2) # prints [10,8.6.4.2]
    [x*x for x in range(1,10)] # returns [1,4,9,16 ... ]
    

    So based on this rudimentary exploration of exactly two alternatives, language designers may avoid ++ and -- by anticipating use cases and providing an alternate syntax.

    Are Fortran and Python notably less of a bug magnet than procedural languages which have ++ and --? I have no evidence.

    I claim that Fortran and Python are C-like because I have never met someone fluent in C who could not with 90% accuracy guess correctly the intent of non-obfuscated Fortran or Python.

提交回复
热议问题