The most useful shell trick I've used to date is mapping the 'cd' command to 'zoxide', which is a more powerful version of cd that remembers folders you've been to.
Small programming tricks
141–150 of 192 posts
Re: Small programming tricks
#142+1 for git log -S. Two I use daily: rg --hidden -g '!*.lock' to skip noise, and python3 -m json.tool to pretty-print API responses.
You can see the discussion about it here: https://news.ycombinator.com/item?id=39877637
Re: Small programming tricks
#143Earlier quoted context omitted.
There are several keyboard shortcuts that you would never find out from watching an agent, but are mindblowing to new Linux users. I suck at remembering vim keybindings, but I have ingrained ctrl+a ctrl+e for jumping to the start/end of a string (which also works all over OS X). I had been a developer for an embarassing amount of time before I discovered those. There are other terminal specific shortcuts for removing…
Another one, not quite as handy but still awesome: ctrl s / ctrl-q. This freezes/unfreezes the terminal output so you can read it, without interrupting your program. Useful for very fast walls of text.
Re: Small programming tricks
#144`find` does a lot more things than that.
And I don't think that's a good thing. Especially because AI coding agents use it a lot. They only need to hallucinate a little to destroy your filesystem.
Re: Small programming tricks
#145Earlier quoted context omitted.
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.
What people refer to when they say "branchless code" is something very particular, and it refers to not triggering the CPU's branch prediction. That is, don't make the CPU have to guess which fork in the code you're going to take. This is usually accomplished in one of two ways: either bit twiddling hacks or specialized instructions that do not affect the CPU's branch prediction, such as the 'cmov' family in x86. If…
Re: Small programming tricks
#146Earlier quoted context omitted.
Maybe something (contrived) like this providing no-op defaults? total = calculateOrderTotal(user.order); if (user.isPremiumMember) { total = total * 0.9; // 10% discount versus total = calculateOrderTotal(user.order); discount = calculateDiscount(user); // Returns 0.9 or 1.0 total = total * discount;
Didn't you just shift the branch to the calculateDiscount() function?
return 1 - user.isPremiumMember * 0.1;
would also cut it.Re: Small programming tricks
#147I find myself using ctrl-r less and less as I make sure that anything of value that I work out goes into a Makefile or the app tooling, for me this is the basis of the dev-ops approach to work (make sure everything is scripted, not worked out on the fly).
I’m lazy so With fish, I just type a part of the command and press up few times to get to where I want. There’s also fish-fzf.
Re: Small programming tricks
#148Earlier quoted context omitted.
Maybe something (contrived) like this providing no-op defaults? total = calculateOrderTotal(user.order); if (user.isPremiumMember) { total = total * 0.9; // 10% discount versus total = calculateOrderTotal(user.order); discount = calculateDiscount(user); // Returns 0.9 or 1.0 total = total * discount;
OK, or maybe... total = calculateOrderTotal(user.order); total = total * user.discount;
total = calculateOrderTotal(user.order);
total *= user.discount;
or: return
calculateOrderTotal(user.order)
* user.discount;Re: Small programming tricks
#149`find` does a lot more things than that.
It's essentially like the SQL SELECT expression.
Re: Small programming tricks
#150The thing with a lot of these tricks is that you have to get into the habit of using them. I knew `Ctrl+r` for history since I learned about the command line. I even have a nice shell integration with fzf. But I still used the up/down arrow keys for years or scrolled up when I was looking for a previous command, because I never remembered the shortcut and just took the path of least resistance to find something. Usua…