How to think like a programmer


#1

Hi All,
Sometimes, I can instantly solve a challenge in the python course. Other times, I come up with an intricate, hair-brained scheme for implementing a code.

For example, on the 15/13 challenge -- define a function that returns the product of a list -- I thought, "I will mutliple the first two elements of the list, remove the first two elements from the list, place the result of the multplication in the new 0 position of the list, and then repeat the process until I hit the max lenght of the list minus 1".

Don't understand that? Well, good. It's totally hair-brained.

But, the problem is that I was stuck on trying to implement that code until i looked at the forum. So, my question is this:

Is there any sure-fire way to start thinking like a programmer? How do you deal with yourself when you're feeling stubborn?


#2

Hi @cookdj0128,

This is a very hard question to answer! :smile:

The first thing that comes to mind is this: You should see programming as a form of communication. It happens between you, a slow and smart human and a computer, which is a fast and dumb machine.

To me, the easiest way to understand a problem you are trying to solve is generally to try and break it down to its simplest expression. Knowing exactly what kind of result you are trying to achieve, you can then start designing the simplestest step-by-step solution to your problem you can think of.

To help with that, some programmers speak the code out loud to themselves or try to explain it to a friend and some actually use a method involving a rubber duck! :hatched_chick:

Why would anyone do that, you ask? Well it's simple. That's what's programming is all about: Explaining to a dumb computer every single steps it needs to take to perform a very smart and sometimes complex program you have imagined to solve a given problem.

But to be able to explain it to a computer, it helps to understand it yourself first.

Sometime this will be, as you have stated, almost instantaneous. Other times your brain will just have a hard time trying to wrap itself around the problem and this is just perfectly normal state of affair. When this happens, I usually just step back from the problem and start tackling another one or start doing something altogether different. Stepping away and coming back have proven to be a huge time saver for me because most of the time, I will end up finding the answer much more quickly than if I had spent the entire afternoon trying!. :smile:

Also, some people will stop when they get a program that just works. Others will not be content and will try and optimize the step-by-step solution into a more efficient one. Sometimes it will involve using less code or maybe using more complex mathematical concepts but most of the time this (and debugging) will be the most time-consuming part of a programmer's job.

At the introductory level, I would say not to worry too much about optimization just yet. I would say that more important to thinking like a programmer is just experimenting a lot. Trying a lot of different solutions to a single problem. Changing the input of a given program and see what kind of effect it has on the output.

Trying to break a perfectly fine and working program, trying to make it fail and guess or predict what will happen can help you better understand how the computer interprets what you are trying to say to him with your code.

Have fun! :smile: