Home Home

Stack Overflow make us lazy

Arseni Mourzenko
Founder and lead developer, specializing in developer productivity and code quality
130
articles
February 24, 2015

It is common to think that the purpose of Stack Overflow is to make the world better, and it does, somehow. I think that it increased the overall quality of developer-oriented resources, provided a structured way to look for help and document certain aspects of programming languages, frameworks and libraries, and got rid of forums. This is the good part.

The bad part is that it encourages laziness and the lack of basic reasoning.

Here's a basic example. I was working on a Python project where I needed to check whether at least one element within a set of elements has a property equal to a given value. The actual code would be too difficult to explain, so let's consider the following imaginary situation: products are loaded from the database, and we should check whether there is a product with a specific identifier.

Two possible alternatives are the one which uses list comprehensions:

if product_id in [c["id"] for c in self.data.load_products()]:
    # Do something

and the one with generator expressions (notice the parentheses instead of square brackets):

if product_id in (c["id"] for c in self.data.load_products()):
    # Do something

The question I was asking myself is whether I should use the second alternative, that is whether generator expression would short-circuit in a presence of an in. While I know that it would in C#, I wasn't sure if the same logic applies to Python as well.

Doing the right thing

The question is not that hard. In fact, it is elementary to answer, and it took me 3 minutes and 40 seconds:

  1. Open two terminals.

  2. Type vi lazy.py in the first one.

  3. Type the following code:

        def get_values():
            values = [1, 5, 7, 7, 9, 2, 6]
            for value in values:
                print("    Yielding %s." % (value,))
                yield value
    
        print("Demo 1:")
        if 10 in [c + 1 for c in get_values()]:
            print("Found a match.")
    
        print("Demo 2:")
        if 10 in (c + 1 for c in get_values()):
            print("Found a match.")
    
  4. Switch to the second terminal and type python3 lazy.py, which results in the following output:

    Demo 1:
        Yielding 1.
        Yielding 5.
        Yielding 7.
        Yielding 7.
        Yielding 9.
        Yielding 2.
        Yielding 6.
    Found a match.
    Demo 2:
        Yielding 1.
        Yielding 5.
        Yielding 7.
        Yielding 7.
        Yielding 9.
    Found a match.
    

I now have the definitive answer.

Doing the Stack Overflow thing

Another way to get an answer is to post a question on Stack Overflow. That's what I did as an experiment. Surprisingly, it takes much longer, that is 9 minutes and 10 seconds to post the question, and another four minutes to get an answer.

This is extremely problematic. The whole Stack Overflow system encourages such questions. Not explicitly, but still. Within minutes, I received two upvotes. Why?! This is a fucking “I don't want to think by myself”, boring question which shows no research effort whatsoever. How could it be upvoted?! And still, it is, because of its good shape rather than its inherent qualities.

The first posted answer is straightforward: the second piece of code makes it possible to short-circuit the evaluation. Great. This answers the question, and is the only thing that I, as a naive person asking the question, would probably want to know.

But here resides the problem. While Stack Overflow is a Q&A website, it is not a website which encourages learning and thinking, at least not in persons asking questions. What could an answer which invites thinking look like? Well, there are several points it may contain:

  1. Most important: the suggestion to find the solution yourself. Not necessarily in a mean way, but, for instance, by showing how the author of the question could find the solution by his own.

  2. The explanation of what happens under the hood. While I included enough information in my question to show that I know the right terminology, I wasn't showing that I understand how it works.

  3. The review of the code itself. Seriously, I copy-paste a piece of code where I load every product from the database to check whether a product identifier is in use, and nobody makes a remark about how wrong this is?

    I could be a beginner who doesn't know that loading data from the database and then filtering it is terrible. I could be ignorant of caching as well.

    I mean, the idea of loading all the products and searching for the identifier (which is by the way probably the primary key, which means that it is indexed) is so terrible that it should catch an eye of every developer who is involved in code reviews. This is like concatenating strings to build an SQL query. The only possible response is: “Stop! Listen, you are doing it completely wrong. This is what you should do instead.”

Why is this happening?

The correlation of mutual interests makes such situations possible. The goal of Stack Overflow is to catch as many users as possible through Google. My question probably contributes to that. The goal of an imaginary person asking a question like this is to get a straight answer. This need is fulfilled. The goal of the person answering the question is to receive upvotes and to have a sense of accomplishment by knowing that the answer was helpful. A short, straight answer does the trick.

But this correlation doesn't make the world better. If I was an inexperienced programmer, I couldn't possibly guess from my Stack Overflow experience that I could have found the answer much faster by being on my own. Neither could I guess that my piece of code has a huge issue in it, and that I should be learning how to use databases properly. While everybody—the person who asked and the person who answered—has a sense of accomplishment, the world became slightly worse by encouraging a bad behavior from a bad programmer posting a bad question.

This is essentially why it is so important to have a mentor and to have code reviews. Mentors or code reviewers don't give straight answers; instead, they are expecting you to improve, to grow, to learn something.