Skip to content

Go values

  1. A good package starts with a good name
    1. Every package should have a single purpose and the best way to know a package's purpose is by its name - a noun
  2. Simplicity matters
    1. Simple means readable, understandable, maintainable, reliable, relatable
    2. Simplicity is prerequisite of reliability
    3. Simple code is preferable to clever code
  3. Explicit is better than implicit.
    1. How to achieve it
      1. Avoid package level state
        1. Because of:
          1. we can't test it in isolation, in parallel
          2. we can't use more then one instance at a time
      2. Low coupling and high cohesion
  4. Plan for failure, not success
    1. Every error should be handled
  5. Return early rather than nesting deeply
    1. Avoid control flow that requires deep indentation
      1. Use guard closes (сторожевые методы)
      2. Place the successful return statement at the end of function
      3. Reduce an indentation by extracting logic in another functions and methods
  6. if you think it’s slow, prove it with a benchmark
    1. Don’t complicate your code because of outdated dogma, and, if you think something is slow, first prove it with a benchmark.
  7. Before you launch a goroutine, know when it will stop
    1. Before you launch a goroutine answer on questions:
      1. Under what condition will a goroutine stop?
      2. What is required for that condition to arise?
      3. What signal will you use to know the goroutine has stopped?
  8. Leave concurrency to the caller
    1. Let the caller choose how they want to start, track, and wait on your functions execution.
  9. Write tests to lock in the behaviour of your package’s API
    1. Your tests are the contract about what your software does and does not do
  10. Maintainability counts
    1. The real goal is to write maintainable code. Code that can live on after the original author. Code that can exist not just as a point in time investment, but as a foundation for future value. It’s not that readability doesn’t matter, maintainability matters more.

References

  1. https://dave.cheney.net/2020/02/23/the-zen-of-go