• 原文 address: How to think like a programmer — lessons in problem solving
  • Original article by Richard Reis
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: mingxing47
  • Proofreader: Rockyzhengwu Park-Ma

Originally written by Richard Reis

If you’re interested in programming, you’ve probably heard this phrase before:

“Everyone should learn programming because it teaches you how to think.” “– Steve Jobs

You might also be wondering, what does it really mean to think like a programmer? In the meantime, how do you do that?

In essence, thinking like a programmer is a more effective way to solve problems.

With this article, my goal is to teach you to think that way.

At the end of the day, you’ll have a better idea of what steps you need to take to become a better problem-solver.

Why is this important?

Problem solving is the most fundamental meta-skill.

All of us have problems big and small. And too often, how we solve these problems seems, so to speak… Is random.

Unless you have a system for solving problems, this is probably how you “solve” them (which is where I often go wrong in my early programming days) :

  1. Try some kind of solution.
  2. If this solution doesn’t work, try another one.
  3. If the other option still doesn’t work, repeat Step 2 until luck strikes.

You see, sometimes you might get lucky and work things out. But this is the worst way to solve the problem. Also, it’s a huge waste of your time.

The best approach involves: a) having a framework for solving problems; B) Keep practicing the framework.

“Almost all employers put problem solving skills first.

Problem solving is the most important quality that almost all employers consistently look for, even more than programming language proficiency, debugging, and system design.

Demonstrating computational thinking, or the ability to decompose large, complex problems, is at least as valuable to a job as, if not more valuable, the basic technical skills required.” — Hacker Rank, Developer Skills Report 2018

Have a framework for solving problems

To find the right framework, I followed the advice in Tim Ferriss’s book on learning, The 4-Hour Che.

This advice led me to interview two really impressive people: C. Jordan Ball (# 1 or # 2 among Coderbyte’s 65,000+ users) and V. Anton Spraul (author of “Think Like a Programmer: An Introduction to Creative Problem Solving”).

I asked them the same question, and guess what? Their answers are basically the same!

Pretty soon, you’ll recognize the answers, too.

Author’s note: This does not mean that they do everything the same way. Everyone is different, and you are different from them. But if you start with good principles that we all agree on, you’ll go further and faster.

“The biggest mistake I see new programmers make is focusing on learning the syntax of the programming language instead of learning how to solve problems.” – v. Anton Spraul

So what do you do when you encounter a new problem?

Here are some steps to solve the problem:

1. Understand the problem

Understand exactly what the question is asking about. Most of the problems are simply that you can’t understand them (which is why understanding them is the first step).

How do you know if you understand a problem? You really understand it when you can put it into words.

Do you remember a scenario where you were stuck on a problem and you started to explain it, and then all of a sudden, you found a hole in the logic that you had never considered before?

Most programmers can relate to this feeling.

That’s why you should write your problems down, draw a picture, or tell someone (another thing is… Some programmers use yellow duck debugging to solve problems).

“If you can’t explain something simply, you haven’t understood it yet.” – Richard Feynman

Make a plan

Don’t jump into a problem without a plan (unless you expect to muddle through). Be sure to plan!

If you can’t write down the exact steps to do something, nothing can help you.

In programming, that means don’t brute force it in the first place. Be sure to give your brain time to analyze the problem and process the information first.

To get a good problem-solving plan, answer the following questions:

“Given the input X, what are the necessary steps to take to get the return value Y?”

Author’s note: Programmers have a great tool for solving this problem… That’s a comment!

3. Unit the problem

Notice, this is the most important step of all.

Don’t try to solve a big problem. You’re bound to cry if you do.

Instead, break the big problem down into subproblems that are easier to solve.

Then, attack each of these sub-problems individually. Let’s start with the simplest question. The simplest questions mean that you know the answer to the question (or at least are closer to it).

In addition, the simplest problem means that the resolution of this subproblem does not depend on the resolution of other problems.

Once you’ve solved each of the subproblems, then connect the dots.

Connecting each of your “subsolutions” ** will lead you to the final solution to the original problem. Congratulations, you’ve solved the problem!

This problem solving technique is the cornerstone of all problem solving. Keep it in mind (read this step again if necessary).

“If I could teach every new programmer one problem solving technique, it would be ‘problem reduction techniques.

For example, if you’re a new programmer, then you’re asked to write a program that reads 10 numbers and then figures out which number is the third largest. This can be a daunting task for a new programmer, even though it requires only basic programming syntax.

If you get into trouble, you should simplify the problem. So instead of looking for the third largest number, what do you do if you’re looking for the largest number? Or too difficult? So what do you do if you want to find the largest of three numbers? What if you look at two numbers?

Keep simplifying until you can write a solution. Then expand on the problem a little bit and write down a solution, and expand on it until the source problem is solved.” – v. Anton Spraul

4. Stuck in a problem?

Now, you’re probably sitting there thinking, “Hey, Richard… It’s cool, but what if I get stuck and can’t even solve the subproblem?”

First, take a deep breath. Second, it is fair.

Don’t worry, my friend. It happens to everyone!

The difference is that the best programmers/problem solvers are more interested in bugs or bugs than annoyed.

In fact, here are three things to try when the unfortunate are faced with a dilemma:

  • Debug: Go through your solution step by step and try to find what went wrong. Programmers call this debugging (in fact, it’s all done by the debugger).

“The art of debugging is to find out what you really tell your program to do, not what you think you’ve told your program to do.” – Andrew Singer

  • Reconsider: Take a step back. Look at the problem from another Angle. Is there a more general way to abstract it?

“Sometimes we get lost in the details of a problem and lose sight of the general solutions that can be applied at a more general level. […].

The classic example, of course, is the sum of continuous natural numbers, 1 + 2 + 3 +… + n, and very young Gauss quickly realized that the answer was simply n(n+1)/2, so that he didn’t have to add.” – c. Jordan Ball

Author’s note: Another way to reevaluate is to start over. Delete everything and start fresh with a fresh eye. I’m serious. You’d be surprised how effective it is.

  • Search: Aha, you read it right, search it well. Whatever problem you have, chances are someone has already solved it. To find that person or find that solution. In fact, even if you solve the problem, you can investigate it again! (You can learn a lot from other people’s solutions.)

Note: Don’t look for a solution to a big problem. Only look for solutions to subproblems. Why is that? Because unless you work hard (even a little bit), you won’t learn anything. If you’re not learning anything, you’re wasting your time.

Practice the framework

Don’t expect to be great after just a week. If you want to be a good problem solver, solve more problems!

Practice, practice, practice. Sooner or later you realize that “this problem can be easily solved by < insert concept here >”.

How do you practice? There’s so much to choose from!

Chess puzzles, math puzzles, Sudoku, Go, Monopoly, video games, Crypto-Cat, and more.

In fact, a common pattern of successful people is the habit of practicing “micro-problem solving.” Peter Thiel, for example, exercises by playing chess, or Elon Musk by playing video games.

“Byron Reeves said, ‘If you want to know what business leaders are going to look like three to five years from now, look at what’s happening in online gaming. ‘”

Fast forward to today. Elon Musk, Reid Hoffman, Mark Zuckerberg and many others have said that gaming is the cornerstone on which they have successfully built their companies.” — Mary Meeker, Internet Trends Report 2017

Does that mean you should only play video games? Not so.

But what are video games really about? Yes, problem solving!

So, what you should do is find opportunities to practice. It can be something that allows you to solve lots of little problems (ideally, this should be something you enjoy).

For example, I love programming challenges. Every day, I try to solve at least one challenge (usually on Coderbyte).

As I said, all problems have a similar pattern.

conclusion

That’s all!

By now, you have a better idea of what it means to “think like a programmer.”

You also learned that problem solving is an incredible skill (meta-skill).

If that’s not enough, note that you also know what to do to practice your problem-solving skills!

Tut tut… Sounds pretty cool, right?

Finally, may you have a lot of problems.

You read it correctly, but may you have more problems. Because at least now you know how to solve the problem! (Also, you’ll know that you’re making progress with every problem you solve.)

“Just when you think you’ve crossed one barrier, you bump into another. But that’s the wonderful thing about life. […].

Life is a process of breaking through the barriers — the barriers we must break through as we grow.

Every time, you learn something new.

Each time, you grow stronger, wiser and more insightful.

Every time, some challenge is overstepped and goes away. Until the end: the best of you.” — Ryan Holiday The Obstacle is The Way

Go solve some problems right now!

Good luck 🙂

Special thanks to C. Jordan Ball and V. Anton Spraul for their invaluable advice.

In addition, without Lambda School, I could not acquire all the programming knowledge in such a short time. I would like to thank them very much and highly recommend them.

Thank you for reading! 😊 If you like this article, please clap your right hand violently to your left hand and see how many times you can clap 👏 in 5 seconds. This is finger aerobics that is good for you, and of course it will help others see the story.

“Say hello” to me on Twitter!

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.