guide.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* My Personal Coding Style
  2. Coding style is important because readability of code is important.
  3. In other words, good coding style improves readability.
  4. This could be all I need to know about coding style.
  5. However, sometimes readability is vague.
  6. Thus I need other more concreate principles to pursuit better coding style.
  7. Coding Style Should Be Personal
  8. The common way is to stick to the most popular style in the community.
  9. But I prefer my own way.
  10. First, for most projects, if not all projects,
  11. I am the main, if not only, reader of the code.
  12. Second, popularity does not mean clearness.
  13. Third, different people have different ways of thinking,
  14. and different tastes.
  15. Coding Style Should be Universal
  16. Do not create individual coding style for different languages.
  17. Instead, stick to one universal coding style across languages.
  18. After all, now matter how different languages are,
  19. they are all programming languages.
  20. If one concept can improve readability in one programming language,
  21. why cannot it be applied to another programming language?
  22. For example, if I am satisfied with the if function in scheme:
  23. (if condition then-clause else-clause)
  24. Then I can write the following code in C:
  25. if (condition) {
  26. then-clause;
  27. } else {
  28. else-clause;
  29. }
  30. even though `if` is not a function in C.
  31. The advantage of this code style is to avoid not considering all conditions.
  32. If a coding style is not possible or very difficult to port to another language,
  33. then I will check the following things:
  34. First, a coding style rule should be a high level concept,
  35. not a low level dogma restricted to certain languages.
  36. An example of a low level rule is that
  37. the opening and closing braces of a block should be on the same column.
  38. It cannot be applied to languages such as
  39. Lisp (not using braces at all),
  40. Python (using indentation instead of braces),
  41. and Go (forcing the opening brace on the same line).
  42. The high level version of this rule is
  43. opening and closing marks of structures of same semantic level
  44. should be matched up consistently.
  45. This high level concept applies to programming languages universally,
  46. and it covers more conditions,
  47. for example, elements in lists, etc.
  48. Whether the opening and closing marks are visible or invisible,
  49. and how the opening and closing marks match up visually,
  50. is low level details to a specific language.
  51. Second, another language may not have the mechanism to apply the style.
  52. It is normal that different programming languages provide different mechanisms.
  53. But most mechanisms are universal, across languages.
  54. A few certain rules of a style may have difficult to find their room in a new language,
  55. but the style as a whole should fit in the new language well.
  56. Otherwise the new language is not general enough,
  57. lacks of a lot of important mechanisms.
  58. For example, I think it is a good idea to declare variables with an explicit type,
  59. instead of a mark for inferred type like `auto` in C++.
  60. But a lot of dynamic typed languages do not support static typing.
  61. So this rule does not apply to those languages.
  62. But it is still unnecessary to maintain separated styles
  63. for static typed and dynamic typed languages.
  64. Third, am I using some clever corners of the language?
  65. Or some distinct and trivial features of the language?
  66. If so, then rewrite it in a simple, explicit, and stupid way.
  67. Do not assume readers are familiar with all corners of the language,
  68. or distinct and trivial features absent in other languages.
  69. For example, in some languages zero is false.
  70. Thus I could have written:
  71. if (integer) {
  72. // when integer is not zero
  73. }
  74. instead of the long, explicit, and stupid version in other languages:
  75. if (integer != 0) {
  76. // when integer is not zero
  77. }
  78. But I prefer the latter, even in languages where zero is false.
  79. Consistent and Automatic Formatting Is Preferred
  80. Formatting is mostly low level, so it does not need too much attention.
  81. For formatting, consistence and automation is more important.
  82. For example, Go unifies formatting via `go fmt`.
  83. Although some choices of `go fmt` are against my taste,
  84. I still think `go fmt` is an advantage of Go.
  85. `go fmt` format code for me, so I do not have to format it manually.
  86. */
  87. package style