新的代码风格


神经心理学知识如何帮助程序员设计代码?在开始编程之前,我学习了很长时间的神经心理学。随后,这些知识帮助我在短时间内取得了很高的开发成果。


在本文中,我想分享我的经验和代码样式的方法。我们将考虑编程方面的风格,然后触及神经心理学,以更深入地了解该过程及其对开发人员的重要性。去年,我一直在Secreate上工作,在那里我使用React Native框架开发移动应用程序,因此本文中的代码示例将使用Javascript。从本文中获得的知识可以轻松地应用于任何编程语言。在本文的结尾,您将找到有用的代码样式示例。享受阅读!


什么是程式化?


code style. — , : , , , . , -. .



:


import {SomeClass} from ‘some/file’

:


import { SomeClass } from ‘some/file’

:


import {SomeClass, someFunction, someConst, AnotherClass       } from ‘some/file’

:


import {
  SomeClass,
  somefunction,
  someConst,
  AnotherClass,
       
} from ‘some/file’


:


function foo() {
  some code...
}

:


function foo ()
{
  some code…
}


:


condition ?
some very long long long long long expression :
another expression

:


condition
  ? some very long long long long long expression
  :  another expression

. , , , .



. , . . , . , , , . . , . . . , . — !


? , . . , , . ? , , .


, . . . , , . . , ( ).


. , . . , . . : , , . . , . , .


. , , . , . , ( ). ( ). , , . , , .



. , — , , . - . — . , , . - , , . , . .


, . , , . , , , . , “ ”.



, . , , . - , code style.


, . , , . , , . . , . , . .


, . , .



. , . ( ) .



2 . , .


:


<View>
  <Text>Some text</Text>
  <TouchableOpacity>
    <Text>Title</Text>
  <TouchableOpacity/>
</View>


Animated.timing(
  animatedValue,
  {
    toValue: someValue,
    duration: someDuration,
    anotherOptions,
  },
)


, .


const object = { field: value, anotherField: value }

, , .


const object = {
  field: value,
  anotherField: value,
  moreFields,
}


. .


:


function someFunction() {
  some code
}


用空行分隔关键字。


例:


function sum(a, b) {
  const result = a + b

  return result
}

要么:


if (condition) {
  do this

} else {
  do that
}

 

switch (value) {
  case someCase:
    do this

    break

  case anotherCase:
    do that

    break
}

用空行分隔不同类型的函数声明和动作


例:


function someFunction() {
  do something
}

function anotherFunction() {
  do another thing
}

要么:


function someFunction() {
  const someConst
  const anotherConst

  let someLet
  let anotherLet

  if (condition) {
    do something
  }

  this.function()
  this.anotherFunction()
}

元素顺序


遵循严格的元素顺序。


扩展类React.Component的示例:


  1. 静态的。
  2. 类字段。
  3. 建设者
  4. 州。
  5. 生命周期功能。
  6. 次要功能。
  7. 动作处理程序。
  8. 助手渲染功能。
  9. 渲染。

用空格分隔自动关闭标签。


例:


<View style={styles.underline} />

如果有多个道具,将道具转移到下一行


例:


<TouchableOpacity
  style={styles.button}
  onPress={this.handlePress}
>
  <Text>Title</Text>
</TouchableOpacity>

条件长


将长条条件分成几行,每行以一个条件字符开头。


例:


if (
  condition
  && anotherCondition
  && (
    value < anotherValue
    || someCondition
  )
) {
  do something
}

要么:


<View>
  {
    this.state.isButtonVisible
      ? (
        <TouchableOpacity>
          <Text>Title</Text>
        </TouchableOpacity>
      )
      : null
  }
</View>

带空格的条件字符


例:


const sum = a + b

要么:


return a < b ? a : b

要么:


const object = {
  field: value,
  ...value > 0 ? { value } : {},
}

All Articles