123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- ===================== HOW TO ========================
- by drummyfish, released under CC0 1.0 (public domain)
- How to:
- Program in C
- ------------
- - Don't program serious programs in anything else.
- - Adhere to minimalism, Unix and suckless philosophy.
- - Use the C99 standard.
- - Fit code to 80 columns, most (good) programmers use old low-res laptops and
- with screen split in vim they won't be able to read your 500 chars long lines.
- - Aim for extreme portability. Good programs try to serve everyone and try to be
- able to run on any computer that's just available. Porting to multiple
- platforms will also greatly help to discover bugs in your program.
- - Write platform-independent code, with platform-dependent parts hidden behind
- an abstraction.
- - Keep your program small, in both source and compiled form, for performance,
- portability, saving space, and also making it easily forkable, understandable
- to most programmers and requiring as little maintenance as possible. Many
- embedded platforms have just 256 kB of flash out of which some is taken by
- firmware, so try to always fit under 200 kB (including assets).
- - Don't use what you don't NEED, don't suppose a feature is present
- "everywhere":
- - Don't use standard library if you don't need it. It may be not available on
- all platforms or may be shitty and make your program slow etc.
- - Don't use printf when you can use puts.
- - Don't use lists when you can use arrays.
- - Don't use UTF if you can use ASCII.
- - Don't use quicksort if you can use bubblesort.
- - Don't use 64 bit ints if you can do with 32 bit or even lower.
- - Don't use floating point unless you ABSOLUTELY REALLY need it. Hint: you
- almost never need it, mostly you can just use integer interpreted as fixed
- point. Many embedded CPUs don't have a floating point coprocessor.
- - Don't use a database system when you can use a file.
- - Don't use files if you don't need them, you can embed your resources and
- configs right in the source code. Many platforms and computers may lack file
- system.
- - Don't use malloc when you can allocate statically or on stack. Avoiding
- malloc is pretty important and not that hard.
- - Don't use too much RAM. A megabyte is a lot. Embedded devices have
- kilobytes, and you won't need more if you write your program well.
- - Don't use multiple compilation units or even multiple source files unless
- you need to. Many programs can fit into a single nicely self-contained file,
- most can fit into single compilation unit. Linking sucks and is slow and
- not everyone may have a linker or know how to use it etc.
- - Don't use build systems, make your programs easily compileable with a
- single call of the compiler. You can provide a one-liner script that just
- runs the compiler with preferable flags. If your program is in single
- compilation unit, you won't even need a Makefile.
- - Don't use a library for what you can easily do, e.g. parse just a few simple
- CLI arguments, or export an image (export is at simple PPM with few lines
- of code, no need for PNG encoder). Don't add unnecessary bloat and
- dependencies.
- - Don't compute too precisely if you don't need to. Approximations may be
- good enough for what you're creating, e.g. a taxicab distance may replace
- Euclidean distance and save you both time and space.
- - Don't make dynamic what can be static. E.g. if you're writing a game, you
- can probably keep the resolution fixed, it doesn't have to change at run
- time. Make it a macro and recompile if you need different resolution. This
- will make your program more efficient and also simple, as managng dynamic
- things is difficult and leads to bugs.
- - Don't embed a scripting language if you can simply "script" your program in
- the same languge, i.e. C. Even if it requires recompiling, it's very often
- the best way.
- - Don't use GUI if your program can work in CLI. Don't make the program
- interactive if it doesn't have to be so. Don't worry, someone else can build
- a GUI for normies atop of your CLI program in the future, but it shouldn't
- be forced on everyone.
- - Don't aim for scalability unless it really your goal.
- - And so on.
- - If you struggle following the aboves because your program is big, needs to
- be composed of 100s of files as to make compilation times bearable, requires
- GB of RAM and so on, you're making a program that shouldn't exist or you've
- made a huge mistake in the design. Scratch it and start over.
- - Use the stdint library's known width integers. This will help portability a
- lot, as well as saving memory (usually don't use wider type than needed).
- The stdint library is very simple and can easily be replaced in case it is not
- present somewhere.
- - Never use tabs.
- - Leave vertical spaces to separate syntactically and semantically related
- parts of code. Code with no blank lines is just unreadable shit.
- - Write self-documenting code (e.g. "getTimeFromStartMs" instead of "getTime")
- but also comment a lot and make your source self-contained. At top of the file
- comment a description of the file, short help, conventions used in the
- program, notes, version, author and date of creating and the CC0 waiver.
- - Use powers of 2 anywhere you can. This will hugely increase performance and
- efficiency.
- - If you can convert a comparison to comparison with zero, do it, this can make
- your code more efficient.
- - Avoid branching if you can, it is inefficient. E.g. a == 0 ? 2 : 4 can be
- written as 2 + 2 * (a != 0).
- - Optimize with -O3 or -Os flags. Use pendantic flags to elkminate all your
- warnings.
- - Manually optimize the BOTTLENECKS. Optimizing elsewhere will likely bring a
- litera zero improvement, so use your brain to identify the critical places in
- your code.
- - If you need to optimize something for speed, know that you can basically
- always buy time in exchange for memory (no need to immediately use special HW
- acceleration). Anything can be precomputed, just take your time to think about
- how (many times making something static as opposed to dynamic will help you
- apply some acceleration structure such as a tree, hash table etc.).
- - DO NOT use too much abstraction. Use only as much as necessarily needed. Too
- much abstraction is extremely bad, but also extremely common. Don't use OOP,
- factories, databases and other bullcrap, just do it simply if you can, which
- is mostly the case.
- - DON'T always follow the usual "best practice", it's mostly a capitalist
- propaganda aimed at preventing code monkeys from shooting themselves in their
- legs. A good programmer knows what he's doing. Do:
- - Use global variables, they're very often the best solution of sharing data.
- - Use macros a lot. These are a simple and elegant solution to templating
- your code and moving computation from run time to compile time, making your
- program faster.
- - Write long functions. A long, flat and straightfoward function is better
- than N functions that all need to be called plus an extra glue function that
- calls them.
-
- - Don't rely on compiler too much for optimization, in many situations you are
- smarter than the compiler and know what to expect during program runtime,
- therefore you can tailor the code to be the most effective. But always test
- whether you're right!
- - Use gotos if you have a very good reason. But try to avoid them as they
- fuck up the structured nature of the source code which may complicate
- transpiling etc.
- - Make your code very readable, hack and modification friendly. Don't obfuscate
- anything or leave it in a state only you understand out of laziness.
- - Test: make a small unit test and run it on multiple very different platforms,
- e.g. little/big endien, x86/embedded ARM, 32/64bit, transpile to browser JS
- etc.
- - Refactor. This is to throw away more unnecessary stuff from your program.
- - Beware of undefined behavior, don't suppose every CPU is little endien, always
- test and analyse your code with appropriate tools.
- - Very clearly waive all your rights with CC0, let the program be in the public
- domain to help everyone even after you're dead. Don't require bullshit
- attributions or copyleft complicating burden. Your program needs to be simple
- to use in every way, even in the legal one.
- - Don't program for money, program to help ALL people. Make youre programs
- future-proof, expect technological collapse. Don't set deadlines, take as long
- breaks as you need. Your program is an art, not a product. Absolutely NEVER
- write proprietary programs, put on DRM etc.
- - Don't listen to code monkeys bashing your code, stick to these guidelines.
- - Don't code apps. Write programs.
- Write Websites
- --------------
- - Stick to minimalism and KISS principles.
- - Don't use things that you don't need:
- - Don't use multiple pages if you can fit everything on a single
- self-contained page (you very often can). This will make it possible to
- embed things like css in one file, avoid using site generators/frameworks
- and make it easy to download the page without crawling, also reducing
- multi-page overhead and avoid complicated navigation menus. A self-contained
- page can be also easily be treated like a book.
- - Don't embed images if you don't need to. Images prolong downloading, require
- multiple downloads and crawling, complicate formatting etc. You can mostly
- link to images as plain links, or even use simple ASCII art diagrams that
- do the job. Same with, videos iframes etc.
- - Don't use css if not needed. If using css, make sure you only use it lightly
- and that your website also works without it.
- - Restrain from using JavaScript – websites aren't programs! JS poses a
- security threat and supports bad philosophy and bloat. Many people turn JS
- off or have browsers without JS support. Only use it if ABSOLUTELY
- necessary and if so, use it very lightly, make sure your program is free SW
- (this will be covered by the CC0 waiver) and that your page works even with
- JS turned off! You can very often replace browser JS with a server-side
- script, which is totally fine.
- - Don't use static site generators or other framework or tool for website
- creation if possible, try to write your page in pure HTML in a plain text
- editor. Keep it simple.
- - If you absolutely need multiple pages, you can still avoid an overkill
- feature heavy static site generators. E.g. if you just need to put a
- navigation menu on top of each page, you can use a very simple BASH script.
- Don't make your website depend on what may likely die.
- - Clearly waive your rights to the page document(s) with CC0, both in the code
- and on the rendered pages. Don't embed copyrighted content on your website,
- link to it if necessary.
- - Use a public domain font, such as Aileron, if possible. Don't support bullshit
- like SIL licenses or even proprietary fonts.
- - Use proper grammar and consistent formatting. Using cursive and bold
- higlighting greatly helps the reader. Proper use of lists, tables and other
- formatting ways greatly improves readability.
- - Use IDs for headings to make it possible for people to link to specific
- sections of your website. You can also make a small TOC thanks to this, if
- your page is longer.
- - Format the source code of your web pages nicely! This should come naturally if
- you're writing it by hand, as you should.
- - Format the page semantically correctly, i.e. use <h> for headings, <p> for
- paragraphs etc., don't use custom <span>s for existing HTML elements. This
- helps computers understand your pages and e.g. convert them to other formats.
- - Properly fill in the page metadata, such as page description, keywords etc.
- This helps people and search engines find your website.
- - Check the validity of your website with proper tools, and by opening it in
- different browser. Don't forget the simple browsers such as lynx or NetSurf.
- - Don't listen to idiots bashing your website, stick to these guidelines.
- Use technology
- --------------
- Live Your Life
- --------------
- - Adhere to minimalist life philosophy. Solve things simply. Ignoring or
- avoiding an issue is often its best solution.
- - Be pacifist, reject violence, but be strict in your philosophy and use
- non-violent means to resist evil.
- - Be anarchist, reject social hierarchies and opression. e.g. capitalism.
- - In important decisions always be rational.
- - Follow ideas, not people.
- - Hate ideas, not people.
- - Love all living beings, even those who hurt you and who's ideas or actions you
- dislike, and even yourself.
- - Don't eat meat, you don't want to support unnecessary murder of animals. Bonus
- points for being a vegan.
- - Never take revenge.
- - Don't work, do useful activity. Minimize your spendings so that you don't have
- to spend much time at work. If you can avoid work by getting money from state
- or the rich, e.g. a pension, do it. You can not do good thing by being an
- employee or employer. Dedicate your life to doing good things.
- - Be humble, don't show pride, don't celebrate.
- - Don't compete or fight, collaborate and educate.
- - Don't listen to the propaganda of your time. Yes, you have been raised under
- heavy propaganda, even you who are constantly reminded you live in the "good"
- times, you are not. Revise your life, identify all propaganda in it and
- separate it from your knowledge of facts.
- - If you can "steal" a lot of money from the rich, do it. Money is evil. Get as
- much of it as you can so that you don't have to care about money any more and
- can focus on doing good. Even when rich, spend as little as possible, live as
- if you were poor. If you have more than you need, give some to someone poor.
- - Question EVERYTHING: can a murder be moral? (no), can stealing be moral?
- (yes), should people have the right to keep secrets? etc.
- - Care about others as you care about yourself. If you can, share material
- things with others. Always share information with others.
- - Enjoy life, don't be a slave to anyone or anything. Society is here for you,
- not vice versa.
- - Be nice and friendly to everyone.
- - Always plan long term.
- - Restrict from consumerism, don't waste resources, don't get addicted to
- things, don't be a slave. Don't have what you don't need.
- - Never lie.
- - Boycott capitalism, block ads, pirate movies, help others pirate, abuse
- and harm companies, don't buy things, don't work for corporations and don't
- use their products, spread the word about anarchism etc.
- - Try to use and support only ethical technology and art, i.e. free software,
- free hardware, free culture, public domain materials etc.
- - Be free and keep the same freedom for others, don't be a slave to "property"
- you own or bullshit ideas of the propaganda.
- - Avoid owning things that require a lot of maintenace, prefer no maintenace
- things, as you don't want to be a slave to your tools. Simple and older things
- are usually more reliable, made to last longer and having less features that
- can break, so prefer e.g. an old bicycle to a "modern" one.
- - Don't use things you don't NEED:
- - Don't own a car and avoid needing it, it's a huge hassle. Bycicle is usually
- enough to own. In need you can use public transport.
- - Don't own a house, it's a gigantic hassle and it enslaves you. Owning a
- small flat is better, living in a rented flat is even better if you can
- afford it, living in a small portable box is even better, if it's
- self-sufficient it's the best.
- - Don't get haircuts, it costs money, time and energy and serves absolutely no
- purpose. Grow long hair and cut it shorter yourself every half a year. Also
- don't shave very often for similar reasons.
- - Don't use a smart phone, don't use mobile data, don't use credit cards,
- - Don't eat fancy food and drink fancy drinks if you can do it.
- - Don't own a lot of property if you e.g. inherit it. Sell it and live off of
- the money.
- - Don't go to dentists or doctors unless you're really seriously ill. They
- will just want to enslave you by making you addicted to pills, they will
- waste your time and energy and will try to get money off of you.
- TODO
|