Only a few of these are actual programming tricks. The problem with sharing them is that they'll typically seem obvious to you, since you know them. It's difficult to know what is actually unknown to other people, and if you share stuff everybody knows you risk coming off as arrogant.
Here's one that I think more people should know: avoid branches. If I can do the same thing without an if statement and even a logical expression, the code typically both becomes easier to understand for people and easier to run for the CPU.
I'm struggling to comprehend how branches can be avoided (or why one would want to, as they are the cornerstone of programming). I can only think how to obfuscate them, which is rarely useful.
Just a simple example. I'm not sure if this is what you consider "obfuscating" the branches. Logically the same, but a bit more linear to understand?
winternewt · · focus · HN ↗
Here's one that I think more people should know: avoid branches. If I can do the same thing without an if statement and even a logical expression, the code typically both becomes easier to understand for people and easier to run for the CPU.
cachvico · · focus · HN ↗
corps_and_code · · focus · HN ↗
if (thingThatIsTrue):
else: they mean:if (thingThatIsTrue):
return dothisWhenFalse()Just a simple example. I'm not sure if this is what you consider "obfuscating" the branches. Logically the same, but a bit more linear to understand?
Edit: I am bad at formatting comments here.
taink · · focus · HN ↗
Example:
No space before start of line.
One space before start of line.
Thus, you can put multiple lines of code with indentation as well as long as you put two spaces at the start of the line:int main() { return 0; }
See <a href="https://news.ycombinator.com/formatdoc">https://news.ycombinator.com/formatdoccorps_and_code · · focus · HN ↗