<p>The previous chapter covered where to search in quite a lot of detail. In this chapter, we are going to look at how to search.</p>
<p>The biggest problem you face when searching the internet is the amount of information that is available. This is compounded by the fact that the developers of these sites will usually take steps to ensure that their website appears at the top of any list of results they appear in. They want to ensure that you are directed to their site. However, there are some tools and techniques you can use to help you get better results and these sometimes involve searching in places other than search engines.</p>
<h2class="sectiontitle">GOOGLE 101</h2>
<p>There are some advanced features built into Google to help you when you are searching.</p>
<divclass="inset">
<p>• If you want an exact match, put your search string in quotes. This is especially useful when searching for code examples or looking up errors.</p>
<p>• In tandem with this, you can often get more revelant results by adding additional terms to a search after the double quotes.</p>
<p>• You can use a minus sign to specify terms that should be exclude from the the search results. As an example, let's say that you are looking for information on the Gatsby authors plugin, that's Gatsby the React-based framework rather than the novel. Your search might look something like this.
<pre>
author gatsby
</pre>
You will find that the top result from a search like this will be F.Scott Fitzgerald. If we amend the search as follows
<pre>
author gatsby -fitzgerald
</pre>
the results will be much more relevant.
</p>
<p>• Add an authoritative source to youir search string. For example, you might be searching for information on the date function in JavaScript, the search string might look like this.
<pre>
javascript date
</pre>
This will work reasonably well, you might find that the top results are from MDN Web Docs and then there might be some from w3webschools.com. There may be more results from MDN further down that may be more relevant to you. If you amend the search like this.
<pre>
javascript date mdn
</pre>
Any results from MDN will tend to bubble up to the top. Having more results from MDN grouped together like this makes it more likely that you will find a result that meets your needs.
</p>
<p>• If you only want to search for results on a specific site, such as MDN, you cna take this a step further by adding a specific site (this doesn't necessarily have to be the full URL, but that can sometimes be helpful, especially if the URL is based on a common term such as nodejs.org. Our search for information on the date function might now look something like this.</p>
<pre>
javescript date site:mdn
</pre>
This should work on any site, it doesn't use the sites own search engine so it can be used to search on any site. A good example of this is that you can search for something on this site. For example, let's say I want to search for information on flex layouts on this site which, at time of writing, doesn't have its own search engine. I can use Google to run the search like this.
<pre>
flex layout site:osztromok
</pre>
If you want to search for results that are similar to a particular technology such as ReactJS without including results for it, you can search using the related keyword like this.
<pre>
related: ReactJS
</pre>
This will give you results are related to ReactJS but will not give you results for ReactJS.
<p>• You can use logical operators such as OR or AND.</p>
<p>• You can also use keywords to specify where to search in a particualr site including</p>
<pre>
- intitle
- allintitle
- intext
- allintext
- inurl
- allinurl
</pre>
</div>
<p>A typical approach to a difficult search might be</p>
<divclass="inset">
<p>• Narrow a search down by adding a site or a typically to the ending of a search. This will often be MDN.</p>
<p>• You can also add an entire error message inside quotation marks to get specific hits.</p>
<p>• If you are searching for a specific error message that includes details that are very specific such as a user name, a filename a URL and so on, you can replace the specific date with a wildcard to get better results.</p>
<p>• Type out a verbose search string searching for something specific such as</p>
<pre>
how do I create a drop down menu with javascript
</pre>
<p>In many cases, the more complex the search string, the more likely it will be that you will get a specific hit.</p>
</div>
<h2class="inset">Filter By Publishing Date</h2>
<p>We mentioned previously how important it is to ensure that the data we get from the internet is up to date and it is important to remember that what we mean by up to date can vary greatly. For example, you might researching that hasn't changed much for decades, in which case, results from ten years ago might still be relevant and useful. On the other hadn, if the technology is realtively new and/or rapidly changing, results that are two or three years old may be too old to still be relevant.</p>
<p>When you run a search, it can be quite difficult to see the most recent results because older results may be more popular and they may appear more prominently in the search results. We can avoid a problem like that by filtering results by date. As an example, let's say we make the following search.</p>
<preclass="inset">
build a date component in react</pre>
<p>The results that appear at the top are between two and three years old, but we may be m ore interested in results that are, say, no more than a year old. We can filter the reuslts by clicking on Tools in Google. This makes the time amd results menus appar and we can select the time drop down, by default it will be set to Alny time, and select an optin such as Past year. We can also sepcify a custom date range and we can specify complete dates such as 1/6/2021 to 22/2/2022 or we can be a little broader and sepcify just the year such as 2021 to 2022.</p>
<p>As a quick side note here, it's worth remembering that when you are searching inside a site like Stack Overflowm it can be important to check when a question was asked but also when it was answered. You may find that the 'best' answer is a few years old but as we mentioed earlier, you may find a less popular answer that is actually better.</p>
<p>In addition, you may also find an answer further down the list that is more recent, it may also be the case that it is too recent to have become popular enough to rise to the top of the list of answers.</p>
<p>Another point worth considering with sites like Stack Overflow that you may have a quiestion that is quite old and with an answer that is also old, but you may find the answer has been recently updated. As an example, the following question</p>
<p>was asked in 2008 (more than 13 years ago as of today's date). Giventhe subject matter, you may well consider a question from 13 years ago as well as any answer given is no longer going to be relevant or useful. In iths case, however, it was edited in October 2021 amd some of the comments are also quite recent, so there is a good chance that the information provide here is going to be worth checking out.</p>
<h2class="inset">Search Engines Outperforming Internal Site Search</h2>
<p>Let's say that we want to perform a search for how to extend a class in JavaScript. If we perform this search using the internal search engine on the MDN site</p>
<preclass="inset">
extend class javascript</pre>
<p>we are getting some relevant results, but nothing that answers the precise question that we were looking for an answer too. The top results provides information on the Selection.extend method and this is followed by a selection of general results relating to the word class.</p>
<p>If we perform exactly the same search in Google, the top result seems to be what we are looking for and it is on the MDM site. The next few results are from other sites but they also seem to be providing the information that we need.</p>
<p>If we only want to see results from MDN, we can combine these two approaches with the following search.</p>
<preclass="inset">
extend class javascript: site mdn</pre>
<p>The top result here is the same top result we got with the Google search above. The searches below that are perhaps not as precisely relevant, but are still pretty close. However, they are all much more relevant than the results we got by searching directly on MDN and they might be considered useful additional information. More to the point, they are satisfying the requirement of providing results from MDN.</p>
<p>In this instance, the Google search engine is just better at interpreting our search string and determining exactly what we are looking for. This can be a little more difficult for MDN's internal search engine, particularly because terms like class and extends can have several different meanings, even within the context of JavaScript.</p>
<p>In some cases, the internal search engine will perform better. Let's say that we want to perform the following search.</p>
<preclass="inset">
JavaScript ??</pre>
<p>If we run this search on JavaScript, it seems to ignore the double question marks and returns results for JavaScript only. If we perform the same search in MDN using its own search engine, it recognises the double question marks as being the Nullish coalescing operator so the top result returned is information relating to exactly that. I believe that Google recognise ?? as some kind of oprerator although I'm not sure exactly what it does, but in JavaScript, it represents the Nullish coalescing operator so in this case, you are probably going to get better results by searching in MDN. Having said that, you may want to see results from outside of MDN in which case you are probably going to want to run the search on Google as</p>
<preclass="inset">
javascript nullish coalescing operator</pre>
<p>You can also append MDN onto the end of this search to get results back from MDN only and this seems to give better results than its internal search engine. Without MDN, you get the same top result but you also get relvant results from other sources.</p>
<p>To summarise, Google is better at interpreting search queries and so will usually give you better results than a sites internal search engine, but an internal search engine may be better at interpreting search terms that are specific to its own domain.</p>
<h2class="sectiontitle">Searching GitHub</h2>
<p>GitHub is the largest repository of open source code on the Internet and these are several ways in which we can use it to find answers to specific coding questions including:</p>
<divclass="inset">
<p>• Searching for a repository that may solve the problem.</p>
<p>• Searching inside a repository for code examples.</p>
<p>• Searching in issues, pull requests and discussions for information on problems such as a specific error.</p></div>
<p>Let's say that you run the following search on Google.</p>
<preclass="inset">
react modal</pre>
<p>The top result is a package on GitHub followed by a link to the nodejs site and then you have additional results from other sites. This is often a good first step in looking for an appropriate repository.</p>
<p>We can perform the same search using GitHub's internal search engine and this gives us the same top result along with links to other relevant GitHub repositories. You can also run the same search in Google with GitHub tagged on the end (you might also try mdn or nodejs.org). In general, you will probably find that searching with GitHub added to your search term is going to give better results, but searching directly on GitHub or using a different additional term may yield better results, largely dependent on exactly what you are looking for.</p>
<p>If you are looking for code examples within a repository, GitHub provides an advanced search option which can be useful here. It's worth noting that in general, searching on GitHub can take a bit of getting used to. When you search for a specific term, you are usually given an option of where you want to search. You can search in all of GitHub or, depending on where you are, you can elect to search under the current user or the current repository.</p>
<p>To give you an example, let's say that we initially search for promises. Our only option is all of github. Now, let's search for a user called addyosmani and we will select the user to go to his profile page.</p>
<p>If we search for promises again, we can now see that their are options to search in all of GitHub, or to search 'In this user' or 'In this repository'. So this gives us a better way of searching for code examples.</p>
<p>Let's say that we select the option to search 'In this user'. We can see a summary of results. That is, we have no respositories but there are 63 results for Code, 15 for Commits, 26 for issue and 44 for topics. At the bottom, there is a link for Advanced search.</p>
<p>In advanced search, we can specify a search term and let's say that we input flex for that. We might then add a user to look for code from a specific person, let's say Chrsitina Truong and let's also add CSS as a language. There are a whole load of other options you can select to refine your search including the name of the repositoty if you know it, the date of the last update, the popularity rating and so on. You may notice that the options you add on to the search are shown in the search box at the top of the page, but you can't edit them there. You have to edit these by entering the option in the appropriate filter. When you have finished adding options, click Search.</p>
<p>Assuming you have made good choices with the filters, you should then see a list of results that are very relevant to you. It can sometimes be a bit of a concoluted process to get access to advanced search but you can also go directly to the URL for <ahref="https://github.com/search/advanced">Advanced search</a>. An advanced search can be quite complex so you may also want to check out the <ahref="https://docs.github.com/en/search-github">Search section</a> of the GitHub docs.</p>
<p>An example of that might be where you are looking for information on a specific feature such as paginate. In order to get results that are more specific to you, you may want to include a snippet of code that is as unique to the type of code you are interested in as possible so that might be something like ({ and you might also want to include a filename and that might be something like filename:gatsby-node.js path:/ so this gives us a search term such as</p>
<p>So that is a useful method for searching for code examples but for things like errors, you will most likely get better results by searching in other areas of GitHub and that includes issues, pull requests and discussions. You may also find that you can get better results by using Google to run a search across GitHub rather than using the internal search engine.</p>
<p>To summarise, GitHub can be an amazing resource and this is particularly true if you are using some of the advanced search techniques here. At the same time, searching on GitHub can be quite complicated so you will probably want to refer to the docs. There is also a course on LinkedIn Learning given by Ray Villalobos called <ahref="https://www.linkedin.com/learning/github-quick-tips">GitHub Quick Tips</a> which is quite short at about 47 minutes and fairly up to date (it was released in 2020). If you are intending to do a lot of searching on GitHub, which would be true for most developers, this course would definetely be worth taking.</p>