Quick Tips for Using GitHub’s Code Search Effectively



When we’re unsure how to solve a coding problem, learning to use a design pattern, or handling anything code-related, our first instinct is to head to Google. We end up browsing through various well-known sites like Stack Overflow, Medium, documentation, and other blogs. During your search, you find a method that seems like it might solve your problem, you put it in your code, and… it doesn’t wor! Something is missing from the example you found. How to adapt it? Let’s find the answer by using GitHub and its powerful code search syntax.

In this article, I will practically demonstrate how I use GitHub’s code search in various different situations. I will use the Android stack (Java, Kotlin, ViewModels, etc.) in the examples since that’s the stack I’m familiar with. But you can do this for any technology you want. The idea here is not to simply replicate what is already officially documented by GitHub. I will only provide a few simple examples of how I do things in my daily routine. Feel free to keep the GitHub code search syntax documentation open on another screen while you read this article.

Github Code search

Table of Contents

  1. How people are using the Repository Pattern?
  2. Searching within a familiar repository
  3. Conclusion

How people are using the Repository Pattern?

Let’s say you, a young Android developer, are learning about best practices in Android development through Google’s official architecture guide. You start reading about the Data Layer, and it shows that the data layer has an entity called Repository, which is directly responsible for interacting with the application data. It is also mentioned that this entity is responsible for the application’s business logic, and we are presented with the following example:

class ExampleRepository(
    private val exampleRemoteDataSource: ExampleRemoteDataSource, // network
    private val exampleLocalDataSource: ExampleLocalDataSource // database
) {

    val data: Flow<Example> = ...

    suspend fun modifyData(example: Example) { ... }
}

Cool! Well… That doesn’t help much, does it? How about we take a look at how other developers are creating repositories?

Go to GitHub and search for the following:

"RepositoryImpl(" AND path:*Repository.kt

Notice that the search highlights some words in different colors. The AND is part of the logical operators along with OR and NOT, and in the example above, it means: Search for files that contain the text “RepositoryImpl” in files whose path ends with Repository.kt (pay attention to the wildcard).

Results for RepositorySearch

The result: 3.2k files found, 3.2k repositories for you to delight.

“But Gabriel, what if I want to see repositories written in both Java and Kotlin at the same time?!”

Well, don’t worry. Just use the following and be happy:

"RepositoryImpl(" AND (lang:Java OR lang:Kotlin) AND path:*Repository*
Figure 2: Another Result for repository search.

Now we got 71.7k results, because of the OR operator. Is showing both in Java or Kotlin… Good luck!

“Ah, cool… That’s how they declare a repository, either in Java or Kotlin. But Gabriel… The documentation said that the best practice is generally for the ViewModel to use a repository. How can I search for repositories being used inside a ViewModel?”

Good question! Indeed, RepositoryImpl in the previous search will only return the implementation of repositories. If you want to see a repository being used/injected by a ViewModel, search for the following:

"ViewModel()" AND "private val" "repository," lang:Kotlin

In the example above: “Return files that have “ViewModel()”, “private val,” and “repository,” in the Kotlin language, all at the same time.”

Notice that I didn’t use AND this time because it is the default when you don’t write anything, but placing quotes is important to get an exact match. If I didn’t use quotes around “private val,” for example, it would search for “private” and “val,” but I don’t want that. I want “private val.” Also, be aware of the necessity of having a basis for your search. I added the comma because I know that in Kotlin, repositories are often injected into the ViewModel and separated by commas, using “private val” in the constructor. My search aimed to see repositories that are exclusively injected via dependency injection. Therefore, notice the importance of having prior knowledge and being creative when searching. You won’t always find what you’re looking for on the first attempt.

Searching within a familiar repository

Gabriel! In Google’s architecture guides, they frequently mention a project called Now In Android. They also say that this architecture guide is being applied there. Is there a way for me to search only within that repository? I’d like to see how they are using the Repository Pattern.

Absolutely! Open the desired repository, in this case, Now In Android, go to the search field or just type “/” to search. GitHub will automatically fill in the following for you:

repo:android/nowinandroid

This is the pattern when you want to search only within a repository: repo:<repository_path>. Now we can search for Repositories only within Now In Android:

Figure 3: Search repository inside a specificy repository (well, that’s confusing)

Conclusion

That’s it. Notice that less is more. We don’t need to write complex queries to find what we want. Regex, for example, is possible, but I’ve never needed it myself. More information, check this github documentation.

Do your tests. As homework: How would you search for the .map API from the coroutines.flow library being used only within ViewModels in the Now In Android repository?

Best regards!

Github | Linkedin

$date =

;

author =

;

Discover more from Gabriel Machado - Software Engineer

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Gabriel Machado - Software Engineer

Subscribe now to keep reading and get access to the full archive.

Continue reading