Live data from Hacker News

Small programming tricks

will-keleher.com

271–278 of 278 posts

Re: Small programming tricks

#271
post #270

This will probably seem to obvious to mention to many, but it took me a few years to realise that you could replicate the "BREAK XYZ" from languages of yesteryear: FOR I = 1 TO 10 FOR J = 1 TO 10 FOR K = 1 TO 10 DOSOMETHING I, J, K REM Breaks out of the middle loop, continues next I iteration IF SOMECONDITION(I, J, K) THEN BREAK J NEXT K NEXT J NEXT I in modern languages by refactoring the loop you want to break out…

Ugh, TIL that Python doesn’t have labeled breaks. Now I’m wondering what other languages in current use don’t have this functionality.

I was about to mention C#, but I just checked and it’s coming to C# 15 in a couple of months.

Re: Small programming tricks

#272

I am so proud of this (I'm sure others came up with it way before me, but I still came up with it all by myself!) //* normal path /*/ debug path //*/ take away the the first slash to toggle the debug zone ON !

#if 0 // Normal path #else // Debug path #endif Only works with C and C++, though.

C#:

#if DEBUG

    Console.WriteLine("This only compiles and runs in Debug mode.");
#else

    Console.WriteLine("This only compiles and runs in Release mode.");
#endif

Re: Small programming tricks

#273
post #77

The 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…

on fish shell i never felt the need for any of this, since pressing up does a history search on what you have typed

Re: Small programming tricks

#274

I am so proud of this (I'm sure others came up with it way before me, but I still came up with it all by myself!) //* normal path /*/ debug path //*/ take away the the first slash to toggle the debug zone ON !

Pretty cool, I love when I catch a coworker doing small tricks like this during pairing sessions.

Re: Small programming tricks

#276

I remember when I was using JetBrains IDE they had this feature that would list all shortcuts and IDE features and how frequently I use them. This was excellent for feature discovery. I would regularly check it out and scroll to the list of features I never used and try them. I always wanted the same for Vim, zsh, etc.

Do they still do this? I'm using PyCharm daily but have never seen it. Would indeed be pretty cool!

There’s a nice feature that will tell you when you could have used a shortcut instead of the manual thing.

Re: Small programming tricks

#277
post #270

This will probably seem to obvious to mention to many, but it took me a few years to realise that you could replicate the "BREAK XYZ" from languages of yesteryear: FOR I = 1 TO 10 FOR J = 1 TO 10 FOR K = 1 TO 10 DOSOMETHING I, J, K REM Breaks out of the middle loop, continues next I iteration IF SOMECONDITION(I, J, K) THEN BREAK J NEXT K NEXT J NEXT I in modern languages by refactoring the loop you want to break out…

Ugh, TIL that Python doesn’t have labeled breaks. Now I’m wondering what other languages in current use don’t have this functionality.

C added it to the C2y draft about a year ago (so not in any actual released version of C). C++ is still working on adding it to the C++ draft.

Re: Small programming tricks

#278
post #77

The 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…

I probably knew and then forgot the Ctrl+r trick at some point because 99.9% of the time, the up arrow and grepping history is sufficient for my needs. Then again, I don't do that much on the command line anymore
Post reply on HN