Stack Overflow as a Service

Providing Answers for Questionable Programming

Chet Haase
Pointer IO
Published in
5 min readAug 23, 2016

--

This article is exclusive to Pointer — a reading club for developers.

Sign up at Pointer.io for our weekly newsletter.

It’s a growing fact of programming life that developers are querying the community and using the resulting answers directly in their own code. Specifically, programmers are using sites such as Stack Overflow to pose questions. More importantly, most programmers are simply using previously asked questions and copying/pasting the posted answers. This new, exciting methodology is called Stack Overflow Driven Development (SODD). Some technology training consultants even offer an entire package that teaches the formal process, called Stack Overflow Driven Development, Official (SODD Off.).

This approach is rife with problems. First, a posted question may not apply exactly to your situation. Second, the answers to that question may not actually be correct. You are, after all, trusting the mob with your code and, by extension, your career.

Nevertheless, copying code is a lot easier than actually working for a living. And that social media feed isn’t going to read itself, is it? So you’ll go ahead and copy/paste anyway. If it compiles, it must work, right? Besides, isn’t correct code what all of those tedious tests you wrote (or copied and pasted) were about?

But there’s another aspect to SODD that continues to be tedious and error-prone: copying and pasting. First, you have to scroll down the page to look at the various answers. Then you have to find the code in question that you think might do the trick. Then you have to laboriously click and drag over the appropriate text and copy it. Finally, you have to return to your IDE and paste it into your code before submitting the resulting mess into your source repository (compilation and testing are, of course, optional).

Fortunately, there’s a new library available that simplifies all of this. No more going to the site and searching for answers. No more copying. No more pasting. In fact, no more work at all; you can just sit back and let your code do the work for you.

I’d like to introduce a new library/API, which I’m calling Stack Overflow as a Service, or SOaaS. This library allows you to use Stack Overflow answers at runtime to perform whatever task your code was supposed to do. No more browsing through the site to try to find the right question, or reading through the answers to figure out which one is the best. Just let Stack Overflow do it for you.

Here’s how it works: Stack Overflow already has a notion of “correctness” in the mob-voting scheme that it has. It also has a measure of how good or useful an answer, or even a question, is by the votes cast by its numerous members. All of us living in democracies know that popular-vote mechanisms always lead to the best result. Similarly, Stack Overflow voting obviously determines the best and most important answers for any given question. So you can rely on this information to help your code, just like citizens rely on their perfect, elected governments to always do the right thing.

SOaaS uses the voting/correctness mechanism, along with some simple search-engine technology, to automatically derive the code that you need for your situation. All you have to do is call the SOaaS API. The API is simple, requiring you to know only some search terms and parameters about how good you want the code to be. You can leave the rest to the library.

Let’s look at a simple example. Here, the programmer want to write a Hello World application in Java, but doesn’t know how to do it. This code imports the main library package and makes a call to SOaaS’s codeThatDoes() method, passing in a string with some search terms:

import com.soaas.PleaseHelpMe;public class MyHelloWorld {  public static void main(String args) {
PleaseHelpMe.codeThatDoes("Hello world Java");
}
}

The codeThatDoes() method essentially does exactly what you would have done before this awesome API made it so easy. It reaches out to Stack Overflow dynamically, runs a search on those terms, finds all questions that relate to that topic, and randomly picks one of them. It then finds all of the answers on that page, randomly picks one of those, copies everything in the code block in that answer, compiles it, injects it into your code, runs it, and returns.

No more opening a browser tab, no more going to the website, no more searching, no more reading, no more trying to understand, and no more copying and pasting. Just call this one method and you’re done.

The API also offers some overloads for codeThatDoes() that allow you to fine-tune the results. For example, sometimes you might want to use the most popular question, or the highest-voted answer for a topic, instead of questions and answers chosen at random. In this case, you could do this:

PleaseHelpMe.codeThatDoes("Hello World Java", true, true);

This variation will use ordered results and will choose the top rated result for both the question and answer.

Another advanced variation will even specify a minimum bar for the number of up-votes in the answer:

PleaseHelpMe.codeThatDoes("Hello World Java", true, true, 2);

This version requires that the returned answer be up-voted by at least two members.

There are other methods in the API, of course, for more niche requirements. For example, it can sometimes happen that you will not find an answer, or even a similar question, on Stack Overflow. Similarly, it is theoretically possible that your call to codeThatDoes() will fail and the result will be undefined. In that case you’ll want to handle the exception. But exceptions are tedious to implement, right? SOaaS to the rescue! All you need to do is call the API’s doException() method and it will use the same underlying tecnology to return the best exception code that Stack Overflow has to offer:

try {
PleaseHelpMe.codeThatDoes("I need help with my CS homework");
} catch (Exception e) {
PleaseHelpMe.doException("I don't understand what happened to cause this exception");
}

SOaaS also offers more complete solutions for the truly uninspired programmer, such as:

  • PleaseHelpMe.doAlgorithmFor(“Fibonachi fibbonaci Fib Oni Chi I don’t know how to spell his name”)
  • PleaseHelpMe.optimize(Method, “Order N-squared”)
  • PleaseHelpMe.libraryFor(“Fix security holes in military website”)

Don’t you want to work less and do more? Start using this library today!

PleaseHelpMe.libraryFor("SODD thing that I read about in some Medium article, I think");

SOaaS: When copying the answer is just too much work.

--

--

Chet Haase
Pointer IO

Android and comedy. Not necessarily in that order.