The most effective way to improve code is to iterate over it,

  • Look back in a few weeks or months.

if else

  • If statements always have two branches, and else statements may have duplicate code, which can be nested and indented, but is clean and tight.
  • Heavy truncation writing and fragmented if logic can make code difficult to maintain.
  • Perfect. // both template & js

modular

  • Having a folder called Module is not modularity. True modularity is not textual differentiation, but logical differentiation.
  • Define inputs and outputs, analyze what real dependencies are, what you can know and what you can’t know.
  • Avoid excessively long function bodies. (screen)
  • Define utility functions.
  • Each function should have a single responsibility.
  • Avoid passing information through global variables or external variables. // global vs state management

Readable code

  • Naming of functions and variables. (STH. DoSth.)
  • Local variables should be defined close to where they are called.
  • Local variable names can be short. // let age = getAgeFrom(user); let userAge = getAgeFromUser(user);
  • Do not reuse local variables. // You can use IIFE
  • Complex expressions make intermediate variables. // if ((xxx && yyy) || (mmm && nnn)) {… } => const isValid = (xxx && yyy) || (mmm && nnn); if (isValid) {… }
  • Reasonable line breaks and blank lines.
  • code standard(lint) sometimes is evil

simple & stupid // KISS

  • The features that the language or framework gives you are not necessarily right.
  • Blind pursuit of “short” and “concise”, or in order to show their smart mind, learn fast, so like the use of some special structure in the language, write too “smart”, difficult to understand the code.
  • Do not omit the curly braces.
  • Do not use && and | | to control code execution process. if (a() || b() && c()) { … } if (! a()) { if (b()) { c(); }}

Comments and code

  • code tell you what
  • comment tell you why

Some thoughts

  • Always write code as if you’re the one who’s going to maintain the code. Maintainence, Project vs Product
  • Figure out what the problem is before you solve it. // Code is the last step
  • Don’t optimize too early.

Duplication && Abstraction

  • DRY
  • The rule of three
  • Inheritance

Value object concept

Different Level

  • system level & application level
  • low level & high level
  • api level & business logic level

Engineer

  • The essence of engineering is not to create anything, but to eliminate risks, how to complete the established tasks with low cost, high efficiency and on time.
  • A good solution does not necessarily need to adopt the latest technology and framework, but is reflected in the details of the implementation. It is plain and simple but shows the foundation. Therefore, put down the impetuous heart, put down the pursuit of those so-called new technologies, solid polish the foundation, repeatedly consider the solution, and choose the right technology to solve the problem.