Skip to content

Instantly share code, notes, and snippets.

@firefirer1983
Last active December 3, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firefirer1983/4dc20ed94f248036119ba39b4d40dd0f to your computer and use it in GitHub Desktop.
Save firefirer1983/4dc20ed94f248036119ba39b4d40dd0f to your computer and use it in GitHub Desktop.
[Don't write classes] 不要写类 #编程原则

Don't write classes

  1. We ship features, not code.

  2. We want as little code as possible

  3. readibility counts 优先级是(从高到低): 交付功能,不交付代码,

    目标驱动,不忘初心.

    越少代码越好

    没有代码,是最好的代码

    代码精简化时,不能影响可读性

    代码需要人去维护,那么可读性就必须考虑.

Namespace是用于解决命名冲突,而不是用于分类

namespace is not for taxonomy!

当一个类除了__init__外只有一个方法,那么这个类其实是多余的.

用functools.wrap来解决问题.

当我们认为未来需要而使用类的时候,不如等以后需求确实存在后再用类实现.

当存在数据和方法强相关性的时候,才需要考虑使用类.

类的作用只限于数据和方法的绑定.

当需要一个小的命名空间存储一些常量,变量,或者函数的时候,而又觉得使用模块作为命名空间又太过,可以考虑使用class作为命名空间.

dataclass就是最好的例子

函数命名,应该以函数的做什么来命名,而不是以这个是什么来命名.

a function name easier to read when it tells me what the function does instead of telling me what kind of function it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment