Sign in to follow this  
Gage64

Thinking before coding

Recommended Posts

One of the first things they teach you at Intro to CS courses is that when you have a problem to solve, you should not jump straight into the code. First you should list the basic steps required to solve the problem, then you should write pseudo-code and only then translate that into code. This sounds simple enough, but it is usually difficult for me to do this, even for relatively simple problems. When I think about the solution, it's hard for me to think about it in abstract terms and ignore the specifics of the programming language. As a result, I usually jump straight into the code and spend a lot of time chasing bugs. Basically my question is, are there any resources (books, articles, etc.) that are targeted towards training yourself to think before you code? Are there any tips you can give me? If you ever had this problem, how did you deal with it? Any advice you can give me will be greatly appreciated.

Share this post


Link to post
Share on other sites
Learn functional programming. For instance, Objective Caml or Haskell.

There are two advantages: the first is that unlike most imperative languages, if you start hacking in a functional language without planning ahead, you will get compiler error instead of runtime errors. This means that your code won't run until it's correct, which is a lot easier that having to look for bugs.

The second advantage is that you'll get used to solving problems in functional terms. This is, once you get the hang of it, much easier than solving problems in imperative terms, because you don't get bogged down in language details. You'll be solving the problems and then translating your solution to imperative terms, which will result in far less bugs.

Share this post


Link to post
Share on other sites
That's a very interesting suggestion, and not at all the answer I was expecting - which makes it even more interesting. [smile]

Now, I started a thread about this a while ago, but which functional language would you recommend?

Take into consideration that I know nothing about functional programming, but I'm not new to programming in general (I'm familiar with C, C++ and C#, though I'm definitely not an expert in any of them). Also, what resources (presumably books) would you recommend for learning the language?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Now, I started a thread about this a while ago, but which functional language would you recommend?


My personal suggestion would be Objective Caml, but I am biased because I use that language far more than I use Haskell. One issue I would see with Objective Caml is that it's a bastard language that also allows the use of imperative features, meaning that you won't be coaxed into functional-only programming. This has some perks (for instance, as an imperative programmer, you won't be completely lost at first) but also some problems (you'll run the risk of trying to constantly use the imperative features).

As for books, the Objective Caml online manual is a fairly thorough exploration of what the language can do. Once you get the hang of it, the O'Reilly Objective Caml book by Chailloux et al. is a good follow-up.

Share this post


Link to post
Share on other sites
Try sitting somewhere other than in front of your PC, and illustrate your complete solution on paper before coding anything out. The illustration then guides the definition of core concepts and behaviours, which then translate to API documentation (e.g. javadoc comments), unit tests, pseudocode and then code.

If you can't illustrate your solution, then it is unlikely that you entirely understand the problem and/or your proposed solution.

Sitting away from the computer helps to resist the temptation to start coding.

By 'illustrate' I don't necessarily mean a UML diagram - an annotated box and line drawing is fine. The idea is to put everything on paper in order to close the semantic gap between your mental pictures and the programming language.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajones
If you can't illustrate your solution, then it is unlikely that you entirely understand the problem and/or your proposed solution.


Yes, that is my biggest problem. It's hard for me to "think in an organized manner" (for lack of a better term). That's basically what I need help with.

Quote:
Original post by ToohrVyk
My personal suggestion would be Objective Caml


Somehow I knew you would suggest that... [wink]

A couple of questions:

1) I heard that Haskell is a pure functional language. Given that my goal is to learn a different way of thinking and not really a particular language, do you think that it might be a better choice?

2) I heard that F# is basically O'Caml minus a few features but is somewhat easier to work with (in particular, it's error messages are less cryptic). Do you think that would be a better choice? (again, for my purposes)

Finally, while learning functional programming sounds interesting, I'm sure it will be time consuming, so I would also like to hear some other suggestions.

Thank you both for your suggestions so far.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
are there any resources (books, articles, etc.) that are targeted towards training yourself to think before you code?

Programming Pearls by Jon Bentley.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajones
Try sitting somewhere other than in front of your PC


Agreed, if I sit at the pc and try to think about a problem I get too tempted to just jump into code.

I find sitting somewhere else for a while and writing the problem on paper and working it out has resulted in some of my best code, Its surprising how smoothly the transition from pseudo code to code can go and its great when the solution works first time.

Also thinking of the problem this way will result in better code because you know exactly what you need when you start to write the code I can remember those early days with code littered with unnecessary variables and functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Gage64
are there any resources (books, articles, etc.) that are targeted towards training yourself to think before you code?

Programming Pearls by Job Bentley.


Heh, I just borrowed that from my college library a while ago. I also already returned it.

I read the first few sections and while there were some interesting bits, I felt like the book was moving too fast and his explanations were very short and not always written in a clear way.

Then again, I think this book is aimed for someone with more knowledge of data structures and algorithms. I'll be taking a course on data structures in the next semester, so maybe I'll revisit the book after that.

Quote:
Original post by BlackSeeds
if I sit at the pc and try to think about a problem I get too tempted to just jump into code.

I find sitting somewhere else for a while and writing the problem on paper and working it out has resulted in some of my best code, Its surprising how smoothly the transition from pseudo code to code can go and its great when the solution works first time.

Also thinking of the problem this way will result in better code because you know exactly what you need when you start to write the code I can remember those early days with code littered with unnecessary variables and functions.


I agree completely, and I've had the same experience several times, but I still feel like this is harder than it should be. Sometimes I need to think for like an hour before I see a simple solution, and when I finally find it, I wonder why the hell it took me so long to figure out such a simple thing.

I think it's because my thought process is not effective, so I'm looking for ways to improve it.

Share this post


Link to post
Share on other sites
I took a course last semester on mathematical modelling, which might be somewhat along the lines of what you want. It basicly boils down to training yourself to expressing your problem in pure mathematics, which you then can solve with a computer using standard algorithms. We didn't use any book for the course, but the lecturer recommended First Course in Mathematical Modeling if we wanted a book about the subject.

I also second the suggestion of learning a functional language. I'd suggest learning Haskell rather than OCaml, as it won't be as easy to write bastardized imperative code in it, making it a better tool for learning functional programming. I also find Haskell's type-system more sophisticated than the one in OCaml, for good and bad. That said, I wouldn't find it surprising if OCaml/F# should prove more useful in practice. Of course, most concepts carry over between functional languages, so it should be easy enough to learn another, once you learnt one of them.

Share this post


Link to post
Share on other sites
Here are some references on Haskell:

Learning Haskell - http://www.haskell.org/haskellwiki/Learning_Haskell
Haskell in 5 steps - http://www.haskell.org/haskellwiki/Haskell_in_5_steps

You might even find the time to look into Natural Deduction - http://lambda-the-ultimate.org/node/1892

I have no experience with F# or O'Caml, and have only briefly passed upon some Haskell (It was assumed knowledge for a course I'm doing), but I would definitely recommend it.

Hope you find the time to read the material, as there are only benefits to gain.

Share this post


Link to post
Share on other sites
@Luctus: Mathematical modeling sounds like it will be too complicated for me, but I'll take a look at it.

About Haskell - can you recommend a resource for learning the language?

Thanks again to everyone for their suggestions.

EDIT:

@Chu: Just saw your post. Thank you for the links.

Share this post


Link to post
Share on other sites
Off-topic of Haskell, but on the topic of functional programming, there's a paper titled "Why Functional Programming Matters", it's an interesting read.

Here's a link to the .pdf (I hope it's free!) - http://www.md.chalmers.se/~rjmh/Papers/whyfp.pdf

Share this post


Link to post
Share on other sites
We used The Craft of Functional Programming in our introductory course in programming. It's okay as a reference, but we barely used it as a teaching aid so I can't really comment on it as a learning resource.

I've heard good things about Write Yourself a Scheme in 48 Hours, haven't read it myself though.

Share this post


Link to post
Share on other sites
I'll second the sit away from the PC suggestion. Grab a pencil and paper, and go somewhere else to draw boxes and lines on it describing what you want to do.

I actually used to make entry-level engineers write up a paragraph describing how they were going to solve a problem before I would let them write a single line of code. It did two things: 1. improved their writing communication skills, and 2. forced them to think through the problem before diving into the code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Luctus
We used The Craft of Functional Programming in our introductory course in programming. It's okay as a reference, but we barely used it as a teaching aid so I can't really comment on it as a learning resource.

That is also the "recommended reading" text for my "Formal Methods in Software Engineering" course.

I have read the first 9 chapters, although the only thing(s) I have gotten out of it are list comprehension and recursive functions, but I am slowly getting through it.

It's quite friendly to the reader (I really need to drill things into my head, I am a slow learner :)), and covers material in short, easy to understand paragraphs, which ultimately means the sections are fairly short and to the point (which is an excellent quality for a programming text!).

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
A couple of questions:

1) I heard that Haskell is a pure functional language. Given that my goal is to learn a different way of thinking and not really a particular language, do you think that it might be a better choice?

2) I heard that F# is basically O'Caml minus a few features but is somewhat easier to work with (in particular, it's error messages are less cryptic). Do you think that would be a better choice? (again, for my purposes)

Finally, while learning functional programming sounds interesting, I'm sure it will be time consuming, so I would also like to hear some other suggestions.

Thank you both for your suggestions so far.


1) OCaml has imperative features but their use is frowned upon and they are not really obvious to use. you will actually have to hunt to find weird concepts like ref and magic. Just dont use the imperative features like loops and assignment, its simple.

2) (a) I think F# will be the better choice because. It is easier to work with since it is on the .NET framework and has the visual studio IDE. In addition to the massive BCL you get access to loads of libraries released for the .NET framework like SlimDX , SDL.net or the neoaxis engine which are CLS compliant.

(b) It definitely does not have less cryptic errors than OCaml. Because the language is not yet released properly About 40% or more of my errors give the highly descriptive syntax error. The supposedly cryptic errors of functional language will come to become vital to you. stuff like expecting type blah but given type bleh, type this and that are not compatible is actually nice and gets you thinking about what you are doing.

(c) F# is different from OCaml. Although they share the same large core subset, the following are different. F# has an optional lighter whitespace based syntax. This will be important because keeping track of semicolons and let bindings was hard for me when I started.

Most importantly F# does not have Polymorphic Variants nor Parametrized modules. F# does not have row polymorphism, it uses as is aproppriate on the .NET framework subtyping. Essentially F# classes and objects are very not the same as OCaml classes. This will likely be unimportant to you. What F# does have that is different is ad hoc polymorphism aka overloading, interfaces, Object Expressions, Quotations, Computation Expressions,reflection, Active Patterns/Extensible matching and I think that about covers the main differences between the two.

Finally F# has been getting alot of press since it is an official .Net language now but there is another OOP functional language called Nemerle* which I feel does a number of things better than F# (other things worse but I use both since on .Net using multiple languages in one project is no different than using multiple classes).

*That twice today Ive posted that. heh.

Share this post


Link to post
Share on other sites
1. Away from the computer is good.

2. Have you read through a design patterns book? I found that knowledge helped me abstract problems into more well defined chunks; even for things that are not the traditional patterns.

3. While it is useful to think in abstract terms and ignore the specifics of a language, phrasing your thoughts in pseudo-code or even mostly proper code of a particular language is practical. The goal is to get a good grasp of your problem and its solution. Thinking wholly abstractly is better, but don't lose sight of the goal. You'll construct things better and better with practice.

Share this post


Link to post
Share on other sites
With regards to sitting away from the computer:

Invest a little bit of money on a whiteboard and some dry-erase markers. You can even get one on wheels, if you want to be able to refer to your notes while coding. Just wheel it back into the computer room when you're ready!

Back in school, during our more challenging tasks (and especially when our courses stopped being courses, and became open-ended semester-long projects), whiteboards were a most desired commodity. People greedily clung to study rooms with whiteboards, they hoarded dry-erase markers, and they tried to hide whiteboards on wheels in the library, so they could retrieve them later. My groups would spend hours in front of a whiteboard for days at a time, just planning.

Now at work, you literally can't find an office that doesn't have a whiteboard. And every one is filled to the brim with notes and sketches.

Definitely worth the investment. I need one at home. So do you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
It's hard for me to "think in an organized manner" (for lack of a better term). That's basically what I need help with.


As others have suggested, being away from the keyboard does have it's benefits. However, I did run into a tool called freemind which helps me organize my thoughts if I were at a keyboard.

I actually have an older laptop and purposely did not put a compiler on it and use freemind when I head off to the coffee shop, bookstore, school, etc. to do some AFK thinking.

Share this post


Link to post
Share on other sites
Quote:
Original post by BTownTKD
With regards to sitting away from the computer:

Invest a little bit of money on a whiteboard and some dry-erase markers.


That is such an amazingly simple suggestion I can't believe I never thought of it! Thanks, might just have to get myself one.

Share this post


Link to post
Share on other sites
Try working backwards. You have a few assignments already done so sit down with one and try to work back to the starting point. Write up some pseudo code for it then work back to a solution description. This will at least give you an idea of where to start next time. Second try harder problems, if the answer requires little thought then there is little gained from the design thought process stage of things. It is only when the answer requires thought that a thought process makes sense. Also it depends on the pseudo code you use, if it isn't very high level and abstract then it is a waste of time.

Share this post


Link to post
Share on other sites
Quote:
Original post by stonemetal
Try working backwards. You have a few assignments already done so sit down with one and try to work back to the starting point. Write up some pseudo code for it then work back to a solution description. This will at least give you an idea of where to start next time. Second try harder problems, if the answer requires little thought then there is little gained from the design thought process stage of things. It is only when the answer requires thought that a thought process makes sense. Also it depends on the pseudo code you use, if it isn't very high level and abstract then it is a waste of time.


Those are some excellent suggestions! I will definitely try them out.

As for thinking away from a computer, I agree. I do this sometimes, but I'll try to do it more often.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
One of the first things they teach you at Intro to CS courses is that when you have a problem to solve, you should not jump straight into the code.

First you should list the basic steps required to solve the problem, then you should write pseudo-code and only then translate that into code.



As an old codger who started his programming career on the Sinclair ZX81 and ZX Spectrum computers, I think by far the best way to discipline yourself is to write directly on the target platform. It helps if said target platform only supports loading and saving to cassettes, has 48Kb or RAM and takes 3-5 minutes just to reload the IDE (in this case, HiSoft's Z80 assembler package), after which you then need to swap the tape for the one with your source code on it.

After a few crashes that require a system reset and complete reload cycle, you soon learn not to write bugs in the first place! :P

(How I miss my Atari STFM + HiSoft GenST + 48Mb hard drive combo!)

[Edited by - stimarco on August 18, 2008 7:34:41 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this