howto.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. ===================== HOW TO ========================
  2. by drummyfish, released under CC0 1.0 (public domain)
  3. How to:
  4. Program in C
  5. ------------
  6. - Don't program serious programs in anything else.
  7. - Adhere to minimalism, Unix and suckless philosophy.
  8. - Use the C99 standard.
  9. - Fit code to 80 columns, most (good) programmers use old low-res laptops and
  10. with screen split in vim they won't be able to read your 500 chars long lines.
  11. - Aim for extreme portability. Good programs try to serve everyone and try to be
  12. able to run on any computer that's just available. Porting to multiple
  13. platforms will also greatly help to discover bugs in your program.
  14. - Write platform-independent code, with platform-dependent parts hidden behind
  15. an abstraction.
  16. - Keep your program small, in both source and compiled form, for performance,
  17. portability, saving space, and also making it easily forkable, understandable
  18. to most programmers and requiring as little maintenance as possible. Many
  19. embedded platforms have just 256 kB of flash out of which some is taken by
  20. firmware, so try to always fit under 200 kB (including assets).
  21. - Don't use what you don't NEED, don't suppose a feature is present
  22. "everywhere":
  23. - Don't use standard library if you don't need it. It may be not available on
  24. all platforms or may be shitty and make your program slow etc.
  25. - Don't use printf when you can use puts.
  26. - Don't use lists when you can use arrays.
  27. - Don't use UTF if you can use ASCII.
  28. - Don't use quicksort if you can use bubblesort.
  29. - Don't use 64 bit ints if you can do with 32 bit or even lower.
  30. - Don't use floating point unless you ABSOLUTELY REALLY need it. Hint: you
  31. almost never need it, mostly you can just use integer interpreted as fixed
  32. point. Many embedded CPUs don't have a floating point coprocessor.
  33. - Don't use a database system when you can use a file.
  34. - Don't use files if you don't need them, you can embed your resources and
  35. configs right in the source code. Many platforms and computers may lack file
  36. system.
  37. - Don't use malloc when you can allocate statically or on stack. Avoiding
  38. malloc is pretty important and not that hard.
  39. - Don't use too much RAM. A megabyte is a lot. Embedded devices have
  40. kilobytes, and you won't need more if you write your program well.
  41. - Don't use multiple compilation units or even multiple source files unless
  42. you need to. Many programs can fit into a single nicely self-contained file,
  43. most can fit into single compilation unit. Linking sucks and is slow and
  44. not everyone may have a linker or know how to use it etc.
  45. - Don't use build systems, make your programs easily compileable with a
  46. single call of the compiler. You can provide a one-liner script that just
  47. runs the compiler with preferable flags. If your program is in single
  48. compilation unit, you won't even need a Makefile.
  49. - Don't use a library for what you can easily do, e.g. parse just a few simple
  50. CLI arguments, or export an image (export is at simple PPM with few lines
  51. of code, no need for PNG encoder). Don't add unnecessary bloat and
  52. dependencies.
  53. - Don't compute too precisely if you don't need to. Approximations may be
  54. good enough for what you're creating, e.g. a taxicab distance may replace
  55. Euclidean distance and save you both time and space.
  56. - Don't make dynamic what can be static. E.g. if you're writing a game, you
  57. can probably keep the resolution fixed, it doesn't have to change at run
  58. time. Make it a macro and recompile if you need different resolution. This
  59. will make your program more efficient and also simple, as managng dynamic
  60. things is difficult and leads to bugs.
  61. - Don't embed a scripting language if you can simply "script" your program in
  62. the same languge, i.e. C. Even if it requires recompiling, it's very often
  63. the best way.
  64. - Don't use GUI if your program can work in CLI. Don't make the program
  65. interactive if it doesn't have to be so. Don't worry, someone else can build
  66. a GUI for normies atop of your CLI program in the future, but it shouldn't
  67. be forced on everyone.
  68. - Don't aim for scalability unless it really your goal.
  69. - And so on.
  70. - If you struggle following the aboves because your program is big, needs to
  71. be composed of 100s of files as to make compilation times bearable, requires
  72. GB of RAM and so on, you're making a program that shouldn't exist or you've
  73. made a huge mistake in the design. Scratch it and start over.
  74. - Use the stdint library's known width integers. This will help portability a
  75. lot, as well as saving memory (usually don't use wider type than needed).
  76. The stdint library is very simple and can easily be replaced in case it is not
  77. present somewhere.
  78. - Never use tabs.
  79. - Leave vertical spaces to separate syntactically and semantically related
  80. parts of code. Code with no blank lines is just unreadable shit.
  81. - Write self-documenting code (e.g. "getTimeFromStartMs" instead of "getTime")
  82. but also comment a lot and make your source self-contained. At top of the file
  83. comment a description of the file, short help, conventions used in the
  84. program, notes, version, author and date of creating and the CC0 waiver.
  85. - Use powers of 2 anywhere you can. This will hugely increase performance and
  86. efficiency.
  87. - If you can convert a comparison to comparison with zero, do it, this can make
  88. your code more efficient.
  89. - Avoid branching if you can, it is inefficient. E.g. a == 0 ? 2 : 4 can be
  90. written as 2 + 2 * (a != 0).
  91. - Optimize with -O3 or -Os flags. Use pendantic flags to elkminate all your
  92. warnings.
  93. - Manually optimize the BOTTLENECKS. Optimizing elsewhere will likely bring a
  94. litera zero improvement, so use your brain to identify the critical places in
  95. your code.
  96. - If you need to optimize something for speed, know that you can basically
  97. always buy time in exchange for memory (no need to immediately use special HW
  98. acceleration). Anything can be precomputed, just take your time to think about
  99. how (many times making something static as opposed to dynamic will help you
  100. apply some acceleration structure such as a tree, hash table etc.).
  101. - DO NOT use too much abstraction. Use only as much as necessarily needed. Too
  102. much abstraction is extremely bad, but also extremely common. Don't use OOP,
  103. factories, databases and other bullcrap, just do it simply if you can, which
  104. is mostly the case.
  105. - DON'T always follow the usual "best practice", it's mostly a capitalist
  106. propaganda aimed at preventing code monkeys from shooting themselves in their
  107. legs. A good programmer knows what he's doing. Do:
  108. - Use global variables, they're very often the best solution of sharing data.
  109. - Use macros a lot. These are a simple and elegant solution to templating
  110. your code and moving computation from run time to compile time, making your
  111. program faster.
  112. - Write long functions. A long, flat and straightfoward function is better
  113. than N functions that all need to be called plus an extra glue function that
  114. calls them.
  115. - Don't rely on compiler too much for optimization, in many situations you are
  116. smarter than the compiler and know what to expect during program runtime,
  117. therefore you can tailor the code to be the most effective. But always test
  118. whether you're right!
  119. - Use gotos if you have a very good reason. But try to avoid them as they
  120. fuck up the structured nature of the source code which may complicate
  121. transpiling etc.
  122. - Make your code very readable, hack and modification friendly. Don't obfuscate
  123. anything or leave it in a state only you understand out of laziness.
  124. - Test: make a small unit test and run it on multiple very different platforms,
  125. e.g. little/big endien, x86/embedded ARM, 32/64bit, transpile to browser JS
  126. etc.
  127. - Refactor. This is to throw away more unnecessary stuff from your program.
  128. - Beware of undefined behavior, don't suppose every CPU is little endien, always
  129. test and analyse your code with appropriate tools.
  130. - Very clearly waive all your rights with CC0, let the program be in the public
  131. domain to help everyone even after you're dead. Don't require bullshit
  132. attributions or copyleft complicating burden. Your program needs to be simple
  133. to use in every way, even in the legal one.
  134. - Don't program for money, program to help ALL people. Make youre programs
  135. future-proof, expect technological collapse. Don't set deadlines, take as long
  136. breaks as you need. Your program is an art, not a product. Absolutely NEVER
  137. write proprietary programs, put on DRM etc.
  138. - Don't listen to code monkeys bashing your code, stick to these guidelines.
  139. - Don't code apps. Write programs.
  140. Write Websites
  141. --------------
  142. - Stick to minimalism and KISS principles.
  143. - Don't use things that you don't need:
  144. - Don't use multiple pages if you can fit everything on a single
  145. self-contained page (you very often can). This will make it possible to
  146. embed things like css in one file, avoid using site generators/frameworks
  147. and make it easy to download the page without crawling, also reducing
  148. multi-page overhead and avoid complicated navigation menus. A self-contained
  149. page can be also easily be treated like a book.
  150. - Don't embed images if you don't need to. Images prolong downloading, require
  151. multiple downloads and crawling, complicate formatting etc. You can mostly
  152. link to images as plain links, or even use simple ASCII art diagrams that
  153. do the job. Same with, videos iframes etc.
  154. - Don't use css if not needed. If using css, make sure you only use it lightly
  155. and that your website also works without it.
  156. - Restrain from using JavaScript – websites aren't programs! JS poses a
  157. security threat and supports bad philosophy and bloat. Many people turn JS
  158. off or have browsers without JS support. Only use it if ABSOLUTELY
  159. necessary and if so, use it very lightly, make sure your program is free SW
  160. (this will be covered by the CC0 waiver) and that your page works even with
  161. JS turned off! You can very often replace browser JS with a server-side
  162. script, which is totally fine.
  163. - Don't use static site generators or other framework or tool for website
  164. creation if possible, try to write your page in pure HTML in a plain text
  165. editor. Keep it simple.
  166. - If you absolutely need multiple pages, you can still avoid an overkill
  167. feature heavy static site generators. E.g. if you just need to put a
  168. navigation menu on top of each page, you can use a very simple BASH script.
  169. Don't make your website depend on what may likely die.
  170. - Clearly waive your rights to the page document(s) with CC0, both in the code
  171. and on the rendered pages. Don't embed copyrighted content on your website,
  172. link to it if necessary.
  173. - Use a public domain font, such as Aileron, if possible. Don't support bullshit
  174. like SIL licenses or even proprietary fonts.
  175. - Use proper grammar and consistent formatting. Using cursive and bold
  176. higlighting greatly helps the reader. Proper use of lists, tables and other
  177. formatting ways greatly improves readability.
  178. - Use IDs for headings to make it possible for people to link to specific
  179. sections of your website. You can also make a small TOC thanks to this, if
  180. your page is longer.
  181. - Format the source code of your web pages nicely! This should come naturally if
  182. you're writing it by hand, as you should.
  183. - Format the page semantically correctly, i.e. use <h> for headings, <p> for
  184. paragraphs etc., don't use custom <span>s for existing HTML elements. This
  185. helps computers understand your pages and e.g. convert them to other formats.
  186. - Properly fill in the page metadata, such as page description, keywords etc.
  187. This helps people and search engines find your website.
  188. - Check the validity of your website with proper tools, and by opening it in
  189. different browser. Don't forget the simple browsers such as lynx or NetSurf.
  190. - Don't listen to idiots bashing your website, stick to these guidelines.
  191. Use technology
  192. --------------
  193. Live Your Life
  194. --------------
  195. - Adhere to minimalist life philosophy. Solve things simply. Ignoring or
  196. avoiding an issue is often its best solution.
  197. - Be pacifist, reject violence, but be strict in your philosophy and use
  198. non-violent means to resist evil.
  199. - Be anarchist, reject social hierarchies and opression. e.g. capitalism.
  200. - In important decisions always be rational.
  201. - Follow ideas, not people.
  202. - Hate ideas, not people.
  203. - Love all living beings, even those who hurt you and who's ideas or actions you
  204. dislike, and even yourself.
  205. - Don't eat meat, you don't want to support unnecessary murder of animals. Bonus
  206. points for being a vegan.
  207. - Never take revenge.
  208. - Don't work, do useful activity. Minimize your spendings so that you don't have
  209. to spend much time at work. If you can avoid work by getting money from state
  210. or the rich, e.g. a pension, do it. You can not do good thing by being an
  211. employee or employer. Dedicate your life to doing good things.
  212. - Be humble, don't show pride, don't celebrate.
  213. - Don't compete or fight, collaborate and educate.
  214. - Don't listen to the propaganda of your time. Yes, you have been raised under
  215. heavy propaganda, even you who are constantly reminded you live in the "good"
  216. times, you are not. Revise your life, identify all propaganda in it and
  217. separate it from your knowledge of facts.
  218. - If you can "steal" a lot of money from the rich, do it. Money is evil. Get as
  219. much of it as you can so that you don't have to care about money any more and
  220. can focus on doing good. Even when rich, spend as little as possible, live as
  221. if you were poor. If you have more than you need, give some to someone poor.
  222. - Question EVERYTHING: can a murder be moral? (no), can stealing be moral?
  223. (yes), should people have the right to keep secrets? etc.
  224. - Care about others as you care about yourself. If you can, share material
  225. things with others. Always share information with others.
  226. - Enjoy life, don't be a slave to anyone or anything. Society is here for you,
  227. not vice versa.
  228. - Be nice and friendly to everyone.
  229. - Always plan long term.
  230. - Restrict from consumerism, don't waste resources, don't get addicted to
  231. things, don't be a slave. Don't have what you don't need.
  232. - Never lie.
  233. - Boycott capitalism, block ads, pirate movies, help others pirate, abuse
  234. and harm companies, don't buy things, don't work for corporations and don't
  235. use their products, spread the word about anarchism etc.
  236. - Try to use and support only ethical technology and art, i.e. free software,
  237. free hardware, free culture, public domain materials etc.
  238. - Be free and keep the same freedom for others, don't be a slave to "property"
  239. you own or bullshit ideas of the propaganda.
  240. - Avoid owning things that require a lot of maintenace, prefer no maintenace
  241. things, as you don't want to be a slave to your tools. Simple and older things
  242. are usually more reliable, made to last longer and having less features that
  243. can break, so prefer e.g. an old bicycle to a "modern" one.
  244. - Don't use things you don't NEED:
  245. - Don't own a car and avoid needing it, it's a huge hassle. Bycicle is usually
  246. enough to own. In need you can use public transport.
  247. - Don't own a house, it's a gigantic hassle and it enslaves you. Owning a
  248. small flat is better, living in a rented flat is even better if you can
  249. afford it, living in a small portable box is even better, if it's
  250. self-sufficient it's the best.
  251. - Don't get haircuts, it costs money, time and energy and serves absolutely no
  252. purpose. Grow long hair and cut it shorter yourself every half a year. Also
  253. don't shave very often for similar reasons.
  254. - Don't use a smart phone, don't use mobile data, don't use credit cards,
  255. - Don't eat fancy food and drink fancy drinks if you can do it.
  256. - Don't own a lot of property if you e.g. inherit it. Sell it and live off of
  257. the money.
  258. - Don't go to dentists or doctors unless you're really seriously ill. They
  259. will just want to enslave you by making you addicted to pills, they will
  260. waste your time and energy and will try to get money off of you.
  261. TODO