
Smart Algorithms in general can be defined as algorithms that have maximally valid/desirable output with minimally valid/desirable input. They make a computer feel more human and often create WOW effects.
Smart Algorithms in browsers
Chris Harrelson from the Google Chrome team says in his Web Browser Engineering tutorial the following:
HTML, CSS, HTTP, hyperlinks, and JavaScript — the core of the web — are approachable enough, and if you’ve made a web page before you’ve seen that programming ability is not required. That’s because HTML & CSS are meant to be black boxes — declarative APIs — where one specifies what outcome to achieve, and the browser itself is responsible for figuring out the how to achieve it. Web developers don’t, and mostly can’t, draw their web page’s pixels on their own.
The only reason, HTML, CSS, HTTP and JavaScript are so “approachable” is because web browsers all use Smart Algorithms which handle errors perfectly. Have you ever gotten a syntax error when opening a webpage (or building one)?
Hopefully not, because the browser handles errors and again — he tries to get the maximally valid output from the maximally invalid input. You could put total garbage into an HTML file, open it up in your favorite browser, and you will still see something useful. The same applies to CSS more than to JS. In CSS, an error inside a rule will only invalidate that rule and not the whole document. And no browser or webpage has ever crashed because of a poorly written JS script. In contrary to most other apps where a little bug can lead to sudden and unpleasant crashes.
Browsers also make URLs smart. You can go to google.com, but you are really going to https://www.google.com.
Smart Algorithms in the Python standard library
In this post, Steven D’Aprano responds to a request to change how the standard lib function str.join
should work
# this works
", ".join(["1","2","3","4"])
# this doesn't because the iterable contains non-strings
", ".join([1,2,3,4])
The proposal was that join
should automatically stringify the elements of its iterable. Steven D’Aprano answered (excerpt)
Why call
str
on the elements? Why notrepr
, orascii
, or some other converter?We need to distinguish between functions which are part of a low-level API, and those expected to work as part of a higher level API.
I’m unlikely to capture the output of
str.join
is part of a low-level string API. It is right and proper that it only works on strings, not arbitrary objects. Being low-level, it shouldn’t guess what to do with non-string values
He calls the functions high and low level but what he is really saying, is that print
is a smart function and str.join
is not. And he identifies some attributes of both.
print
should take any input (and still give valid output) whilestr.join
should only take constraint input.str.join
should be precise and not have to guess what it should do.print
can “guess” what it should do. In other words, its output doesn’t need to be precise.
These attributes might make print
feel like a black box, but it also produces amazing results — to both new and advanced users — just like a web browser.
AI — When Smart Algorithms become black boxes
ChatGPT amazed the world again and again and for any input it will create valid output (when we define valid not as factually correct but as linguistically correct and with content related to the current topic). Therefore, it definitely can be described as a Smart Algorithm. This applies to most AI systems — whether they use neural networks or not.
Neural Networks
The problem with modern machine learning algorithms is that they produce real black boxes. In contrast to conventional algorithms, that only seem unpredictable to an outsider: The print
function for example is very predictable for someone who understands how it works (and for that you don’t need to be an implementor). The same applies to the inner workings of web browsers. With NNs however, no one — not even the designers — really know what is going on. In the future however, machine learning will be the way to produce smart software that impresses users.
Conventional AI in Games
Conventional AI is a classic example of Smart Algorithms. It is/was used in many games to give the user the impression of playing with real (meaning: human) characters. In more abstract and simple games like chess, conventional AI has already overtaken humans by far and for a long time. In most games however, AI is not yet on the level of a human, which coined the phrase “bot” to describe characters that have common AI features (strange, stopping walk and aimlessness) or are generally bad even when played by humans.
Bad AI make bad games and good AI make good games. Mark Brown made a highly professional video essay: “What makes good AI?”
He explains that as a game designer, you need to show the player what the AI is thinking. The challenge is to make it intelligent but still predictable for an experienced player — exactly the characteristics of Smart Algorithms.
Smart Algorithms in User Interfaces and Tools
We all use tools — software that helps us to do our work. IDEs like VS Code, speech assistants like Siri and modern spell checkers like LanguageTool are good examples. While this is more obvious with speech assistants, the best tools feel like human (personal) assistants. They do work that normally a human would have to do. In this context of real user interaction, we focus more about the part of smart algorithms that feel human and generate WOW effects.
In IDEs, there are actually two parts:
Firstly, code often contains boilerplate, so IDEs help you to avoid having to write much yourself, and they make it easier to navigate and edit code blocks.
Then there are actually smart parts. A cool feature I love is “Rename variable” that renames all occurrences of a variable. The smart part is that it treats the code like a human would do as an AST and not just like text. Similarly, when renaming a file, all imports of that file in code can be automatically updated to reflect the change.
Some might find this pretty simple. We can see that the border between smart and not smart is a fine line, subjective and constantly shifting.
How can I make Smart Algorithms
Because of the subjectivity of Smart Algorithms in user interactions, the power of Smart Algorithms is more defined by its outcome than necessarily by its internal complexity.
Take as an example the task to open a program on a computer given the program's name. Approaching this task using a method for calculating the Levenshtein distance, it could be as easy as looking at all installed programs in some Windows registry, or it could be as hard as finding the correct program in all executables on the whole disk.
But generally, expect smart algorithms to need a lot of data (parameters). Maybe a list of all directories that installed programs are typically at, or some common synonyms. You will need a lot of ifs and elses and error handling.
For these kinds of tasks, machine learning might be suitable. Maybe the exact example is not good, but for things like NLP, image, character or voice recognition ML is perfect. All of these algorithms and tasks are considered smart (that’s why it’s called artificial intelligence, duh)
However, as we already saw, ML is not always the best option and collecting enough data to train your model can be unviable. But its applications become wider and wider, and its capabilities greater and greater, as we have seen in the recent year with DALL-E and ChatGPT.
The most important thing about Smart Algorithms, however, is the programmer. The smarter he is and the more knowledge he has about the topic and his users’ desires, the smarter his algorithms will appear for those users.
Appendix: Some Examples for illustration
Example 1: Google Calendar
Google makes some good products, and one example is Google Calendar. Do the following
- Create a new entry and name it something like “12:00 Video Meeting”
- Then try to set the time for that Meeting
- Calendar will automagically delete the 12:00 from the title and enter it into the time field
- This is a perfect example: The invalid input (times should not be in the title) was converted to valid output (the time field contains the time and the title field contains just the topic), the app feels like your personal assistant and, finally, it creates this WOW effect we all want to feel
I said before
You will need a lot of ifs and elses and error handling.
Think about it. Some Google developer will definitely have written some code that says:
When the “set time” button is clicked, then if the title starts with a time, insert that time into the time field and delete it from the title.
And I have already really simplified this because obviously removing the time doesn’t just mean remove_prefix
. You also need to remove the whitespace between the time and the actual title.
And even a “12:00: Video Meeting” will be reduced to “Video Meeting”. It’s not going to return “: Video Meeting” which could easily happen, if this was “dumb” code.