Unconventional Programming Tips: Search Better

TLDR: Make a habit of using the most uncommon substring from your search term. Simple example: Instead of searching for function doSearch(), try searching for n do or ch(.


Who is this for?

This article is mainly intended for programmer. If you frequently search for code snippets in your code base, then this article is for you.

One of the most common tasks when I code is not actually writing or reading code, but rather looking for something. I have a code base and want to find the following function:

function isSign(firstNumber0: NumberPad | null) {
  return firstNumber0 == NumberPad.PLUS || firstNumber0 == NumberPad.MINUS;

I could search for function isSign or isSign to find the above function. Both would yield the expected results. Though, In many cases you would end up with many more unrelated results as well, and have to filter them by hand:

Search Result

I am using rg for demonstration purposes. The searches are mostly done in a decent IDE.

What changed

Once I was working on a python project and was frequently searching for functions. Remember, python functions are like def myFunction(). A team member (🙏 Florian) pointed out, that I could just look for f myFunction. That was tiny hint, but then, I have been using that technique for almost daily.

How I search now

To find the above function, you could look for n isSign or even shorter terms are n i or Sign(.

The general rule is: If you want to search better, then look for the most uncommon part of the search term.

This trick works even better with HTML. For example, on this blog, I want to find where the menu is defined. There is a TIL item, so in the source code, there must be a tag like <a>TIL</a>. In those cases, I made a habit of looking for TIL<. That way, I don’t see all the noisy content where TIL is defined in comments or text.

Once you start frequently using this technique, you start immediately spotting what is common (in your code base) and what is uncommon.

This tip is not limited to code. You want to find any text, then train yourself (by experience) to the uncommonness: Search Result

Thank you!

Thank you for reading the full article ! If you learned something new, you may find my unconventional tips for coding comments interesting as well.