Write unit tests, write integration tests, write regression tests, get into formal verification, make tests super fast, integrate them into your build or continuous deployment system.
-
-
If there are invariants that should hold in your program, check those too! You can pretty much never check things too much. And if things don't add up .... bail! Be very very defensive in your programming.
Show this thread -
This also relates to a simple programming tip. It can be easy to fall into a pattern of ... if (condition1 == good) { ... if (condition2 == good) { ... if (condition3 == good) { do_something(); but this is bad and becomes dangerous.
Show this thread -
Instead do: if (condition1 != good) { bail(); } ... if (condition2 != good) { bail(); } if (condition3 != good) { bail(); } usually makes context much clearer, avoids nesting confusion, and builds in that pattern of bailing!
Show this thread -
o.k. last piece of advice and it is ... don't write "what" comments, write "why" comments. If you have to comment on what your program is doing, then the code itself was not readable! Instead use comments to provide context.
Show this thread -
Use readable variable names that are nouns and meaningful function names that are verbs. The code doesn't have to be poetry, but it can absolutely be easy to follow. Give your future self an easier time. Code is written to be read.
Show this thread -
Very very rarely there is code that can't be easily read; if you're using bitwise operators as part of cryptography or compression or something, for example. Comment those with a "WTF is this doing" ... be very very verbose. But that's the only exception I've found!
Show this thread -
Which brings me to the $1000 programming contest! 3 prizes: $500, $300, $200 for the most readable, easy to follow, tested, Apache Software License 2.0'd, implementation of
@lemire's nearly division-less RNG.https://lemire.me/blog/2019/06/06/nearly-divisionless-random-integer-generation-on-various-systems/ …Show this thread -
I hope he doesn't mind because I didn't ask! I've chosen
@lemire's algorithm because it is awesome and ground breaking, very short, and intrinsically hard to follow if you're not into the math.Show this thread -
His blog post contains a 14 line implementation in C, and there's also a paper getting into it: https://arxiv.org/abs/1805.10941 , so there's great material to start from. But how can we make it more readable and easy to follow for a beginner or non-math-expert? how can we test it?
Show this thread -
Have at it and give it a go! Any programming language you like. Apache Software License so that it can be included in other things. Closing date: September 1st 2019.
Show this thread -
Message me a gist, or a link, or send me an e-mail, whatever works ... and we can talk readability and testing about it too!
Show this thread -
For further reference: here's my rejection sampling RNG implementation with more comments than code. https://github.com/awslabs/s2n/blob/master/utils/s2n_random.c#L182 … End of thread!
Show this thread
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.