Tips on Improving Logical Thinking?

Started by
11 comments, last by larspensjo 11 years, 3 months ago

I've been programming for a little bit over a year, and have some experience in JavaScript, PHP, a tiny tiny amount of Java, and mostly C++. I've noticed that I've gotten to the point where I can program for, say, a couple of hours without checking to see if it runs, and then when I'm done for the night everything will build just fine. At least in the general case. That's very different from when I first started, and I'd have to click Buld and Run every 10 minutes just to make sure I'm not writing buggy code on top of already buggy code.

But now I'm starting to see that even though my code will build fine, there will almost always be some logical errors that, upon review, I say to myself "Why would I even write that? It's a simple mistake, but it makes no sense. I should've known from the start." I'd like to be able to change that about myself, and not make so many silly, illogical errors so that my first run of code on any project will be less buggy and weird.

Any suggestions on fun or interesting ways to change this? I've heard of Code Complete, but it's quite expensive. I thought maybe I should learn a bit more about actual computer science before diving into that book. I've never really enjoyed project euler. My mom loves doing things like cryptograms, but I never really had much fun with them. So other than stuff like that, what do you guys do that you think might improve your logical abilities?

Advertisement

You could request Code Complete at your local library for free; however, I don't think it teaches spotting logic errors - rather, it teaches how to code cleanly to increase maintainability and decrease bugs from things like poor allocation/deallocation strategies.

Could you give some examples of the kind of logical errors you are trying to avoid?

In terms of just exercises most puzzle games are logically oriented. Sudoku immediately comes to mind. When you first start playing Sudoku you probably have to think for a minute just to decide if it's even possible, then as you play you start to develop mental guidelines that help you solver faster. Over time you can get ridiculously fast at it just by streamlining your thinking.

In terms of coding as a whole, there's no way to completely control all of the influences that effect your brain and/or thinking process. Unit testing is not a sign of weak capability, it's a sign that you're wise enough to find bugs early rather than indulge your ego and assume that you'll never have a problem in your code. It's true that you get better at coding over time, but that's because over time you're indulging in the best exercise possible: coding.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

It's focus, you have the skills to execute but your not focusing on the details to see the ramifications. Create and foster an attentive pause in your programming routine where you ask and answer questions about what and how your doing something. Like how will it affect other submodules? Is there a better object model than this? Is this the fatest / easiest way to my goal? etc.. just asking questions will force you to go out of autopilot mode ur in and that will expand the depth of your understanding and hopefully catch these design issues early on.

A couple of coding tips which you may already practice, but will reduce the bugs in your code:

1. Clearly comment your code. Comments will help in debugging problems, but they also help to avoid bugs in the first place as they encourage the coder to spend a bit more time thinking about what the code needs to do / is doing.

2. Keep your code simple. Favoring simple solutions will reduce bugs and also make finding and fixing bugs easier.

ddn3's response makes plenty of good points about the value of up-front design too.

And of course, just keep practicing. The more coding you do the better you'll get.

Matt

It's focus, you have the skills to execute but your not focusing on the details to see the ramifications. Create and foster an attentive pause in your programming routine where you ask and answer questions about what and how your doing something. Like how will it affect other submodules? Is there a better object model than this? Is this the fatest / easiest way to my goal? etc.. just asking questions will force you to go out of autopilot mode ur in and that will expand the depth of your understanding and hopefully catch these design issues early on.

That's a great answer. I think you're right. It's probably more focus than logic.

Human minds are prone to errors. I think it's just mental fatigue. Stepping back occasionally and review your code is just as important as writing code. Don't hammer it away for a few hours. I always stop after a few minutes of coding, reviewing what I wrote, asking myself if I'm happy with it, constantly reviewing my approaches and revising code along the way.

Sometimes, I would write a single class, and without testing it first, I stop coding and step away from the PC, and rethink if it's something that I want or if I miss anything. Then come back finish what I need, then finally test it, and fine tune it after I am sure that I want to go in that direction.

I have the same problem. I dont like the feeling of debugging the program after it didnt work, and finding like 5 errors and fixing them, just to find that nothing changed and its still broken. -,-

Im going to be applying these magical tools from now on to avoid this:
-Use asserts
-Use smart pointers
-Error somehow if an object is used in an (likely) invalid(/nonintended?) state and try to prevent those situations from being possible.

o3o

Well, there's always the technique of: "Explain it to the duck!":
http://www.codinghorror.com/blog/2012/03/rubber-duck-problem-solving.html
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Thinking logically and knowing how to program are two separate but parallel paths on the way to becoming a good programmer. Being able to think in logical steps is one thing I was glad I was good at before I picked up my first programming book. Specifically training your logical brain is a bit different from learning to program though.

This might sound odd, but you might think about taking an Intro to Philosophy class or Intro to Logic class at one of your local colleges. If you can sort through the material, you should come away with a much better understanding of logic. It helped me quite a bit, and it was fun in the process.

Another good class to take is a college-level algebra class. I know I HATED algebra in high school and college, but thinking back, it was the really difficult problems that really give your logical brain a good work out. Teachers always want you to show your work, but I couldn't stand not taking shortcuts or just working it out in my head and throwing down the right answer. But if you really focus on committing each step to paper, your brain starts to understand better and better how to take a big problem and break it down into smaller and smaller steps. In retrospect, I'm pretty grateful 12th grade algebra teacher was such a hard-#^@ when it came to showing my work. Improved my thought-process.

This topic is closed to new replies.

Advertisement