用js编写干净的代码

    科技2023-12-14  96

    Keep functions small.(Preferably < 30, but never more than 50).

    保持函数较小(最好<30,但不要超过50)。 Concise use of if/else. Keep the if/else depth to max of 2 levels.

    简明地使用if / else。 保持if / else深度最大为2级。 Concise use of try/catch. try/catch should be first statement in function.

    简洁地使用try / catch。 try / catch应该是函数中的第一条语句。 Use forEach/map/reduce/flatMap(chain)/transduce etc. while looping arrays. Avoid for loops until necessary.

    在循环数组时使用forEach / map / reduce / flatMap(chain)/ transduc等。 除非有必要,否则避免循环。

    Avoid unnecessary promise wrapping. Understand Promise “then”, it acts like ‘map’ on scalar values and ‘flatMap’ on wrapped promises. e.g Promise.resolve(1)

    避免不必要的承诺包装。 了解Promise“ then”,它的作用就像在标量值上的“ map”,在包装的Promise上的“ flatMap”。 例如Promise.resolve(1)

    .then(a => a * 2) // a = 1 here, acts like map

    .then(a => a * 2)// a = 1,就像地图

    .then(a => Promise.resolve(a * 2)) // a = 2, acts like flatMap.

    .then(a => Promise.resolve(a * 2))// a = 2,作用类似于flatMap。

    .then(a => console.log(a)) // a = 4

    .then(a => console.log(a))// a = 4

    Practice KISS(Keep it simple stupid), DRY(Don’t repeat yourself) and YAGNI(You aint gonna need it).

    练习KISS(保持简单愚蠢),DRY(不要重复自己)和YAGNI(您不会需要它)。

    Keep Patience. It took time to learn things. https://www.norvig.com/21-days.html

    保持耐心。 花时间学习东西。 https://www.norvig.com/21-days.html

    Read Pragmatic Programmer and Clean Code.

    阅读实用程序员和清洁代码 。

    翻译自: https://medium.com/@shiningsandy/writing-clean-code-in-js-b8535c108fc4

    相关资源:很有趣的javascript编写的代码
    Processed: 0.016, SQL: 8