views:

725

answers:

22

In exams and interview situations, it's common to need to either write or review code that's not on a screen.

Whether it's on paper or a whiteboard, what tips can you offer for both the writing and reviewing/debugging processes?

+23  A: 

Leave space between your lines in case you have to insert more code.

Evan Meagher
There's a reason BASIC numbered lines were generally 10,20,30... :-)
Alister Bulman
This sounded overly simple before I started writing. Turned out to be very insightful. Cheers.
Tom Wright
+3  A: 

After writing each line, go over it TWICE - to ensure there aren't any bugs. It's a lot harder to catch them on paper rather than on a computer.

JasonV
+3  A: 

If you are being asked to do it, for example, in a job interview, please know what you are doing, and have a clue how to actually program.

True story: I asked an interviewee to write a simple program (it was FizzBuzz), and he had forgotten how to do a for loop.... I didn't bother with the rest of the interview.

Alister Bulman
We used to give candidates (raw graduates) a computer, any language or tooling they wanted, and access to the internet. We made clear that we didn't care how bad the solution was or how they got to it.It was a useful filter - about half failed to produce a solution.
Alun Harford
It always surprises me how dumb people can be. Then I talk to the next guy....
Alister Bulman
+8  A: 
  • use a pencil
  • start with your first line indented in case you later need to wrap it in something
  • do a quick pseudocode mockup of your control flow before you get started, to avoid obvious mistakes
  • avoid goto :)
RedFilter
+4  A: 

Think like a computer. This may seem obvious, but it truly helps more than anything for the reviewing/debugging process.

To do this, start reading from the top, all the way down. Have a separate sheet of scratch paper so you can do any necessary calculations that the computer would do, or write out any complex array formations.

Don't do any complicated calculations in your head, or else you will not as easily pick up on mistakes in your code — write it all out.

James Skidmore
Thinking like a computer is sort of a given... I mean, you're programming... a computer. If you're not thinking like a computer, your code will suck, whether it's in an IDE or on a sheet of paper.
William Brendel
I'm talking about more in the reviewing process. When you're reviewing your code, don't look for typos (write-os?) or other obvious bugs, but literally go through your code like a computer would.
James Skidmore
A: 

Generally, don't. In an interview situation, if somebody wants you to write non-trivial code without a compiler, they don't live in the same world you and I do (where compilers tend to be available in a professional development environment).

That said, manually compiling the code in your head is a good trick if you know the language spec well enough.

Alun Harford
I would hope that any interview situation (in a company worth working for) would be looking for generally demonstrating you know how to do it if they ask on paper, not for compile-perfect code on paper. Exams are different, though.
Yishai
I'm not really sure how you could compiler code in your head...
Zifre
"That said, manually compiling the code in your head is a good trick if you know the language spec well enough." I always ask if they mind if I compile to 68000. That chip is friggin' easy to compile to.
Nosredna
Every decent job interview I've been on, they ask me to write code without a compiler.
Frank Schwieterman
+2  A: 

After you finish coding, run through several "expected" cases (examples).

Then, switch hats and think about the code you've written as a tester looking at a black box - looking and outputs only. What kind of crazy inputs can you provide? Write them all out, without looking at your code. Then, after you're done brainstorming, think about what your code would do in all of those cases.

Alex
+3  A: 

Bring your own whiteboard pen, it will make you look like a pro.

kotlinski
I once saw someone use a non-whiteboard pen by mistake. They looked so far from pro...
Tom Wright
+10  A: 

Practice! Practice! Practice!

Write complete code for at least 3 simple algoritms on a whiteboard (or paper). Here's the challenge for you:

  • implement quicksort
  • implement a single-linked list, with insert/remove functions
  • implement atoi function

No cheating! You have to do it on a whiteboard, not on your computer. The point of the exercise is to test yourself in an unusual environment, not just test your coding skills. Also, in the process you will find the best way to use whiteboard, paper, pencil/pen, etc.

After you're done, either have someone you trust check your code, or type it on a computer / try to compile and test. Fix the problems on white board first, and test the fixes. Once you're done with this exercise, you will be psychologically prepared for the exam and will most likely pass it. Good luck!

Igor Krivokon
or maybe you can write the code at home? i think they all ask the same paper questions, so imagine how impress would they be if you "write it" in 1s.
01
+1  A: 

My suggestion is to remember to have a few sections to the paper or board that you using to hold the pseudo-code:

1) Data Structures - This may be useful if you want to look at how objects are connected such as in trees, linked lists, or arrays.

2) Complexities - What is the big-O of your program in terms of time and space?

3) Tracing room - If you work through a couple examples of the program you may want a space for where to put temporary variable values and output to the screen or errors.

4) Question details - What were you asked to implement with what qualifiers and is this clear enough to start writing out a solution? For example are there time or space qualifiers such as an in-place sort or sort in O(n log n) time. What is the important complexity in solving this: Time, space or are they equal? For example, some problems may use a lot of space as a way to quickly store the data, e.g. how many 1 bits are there in each of the different bytes combinations that needs to have a constant time solution.

If you work through a few sample problems like Igor Krivokon's answer has with this kind of structure you may find it easier to be able to present what you did, why you did it that way, and that it does work within the bounds stated.

JB King
A: 

Whenever you put an open brace, put blank space and then a closing brace. It's better to erase and move your closing brace than to forget it.

Iceman
A: 

Good grief, what will they think of next ?
Why would you want to write actual code on paper?
Why not option for chiseling it in stone instead?!

Don't get me wrong, I even did something like that in the past, but that was 10-15 years ago, and I had printed code where only small adjustments were made. And that was only because I was more comfortable lying on the couch in the living room than sitting by the desk. Completely different.

What you've got here is this scenario - instead of writing computer code, which is an abstraction per definition, on computer, and writing for example mathematical symbols on paper (which is another abstraction; the symbols, not the paper), you're writing actual code on paper. What good is that for ? What usage will come out of that, except showing off and similar uselessness ? (No, being able to do it does not make you smarter - some autistic people can do amazing things, but that doesn't make them smart).

Debugging ?!! Think about it ... the economy of the world is in enough bad shape without these idiocracies. And we're not helping it with this kind of thinking.

I sometimes think people come up with this kind of ideas because they obviously don't have anything useful to do at the moment. Having too much free time can be very dangerous in the hands of intelligent people.

ldigas
There are some reasons to believe that ability to write good code on a whiteboard correlates well with other qualities required from a software engineer. Hence, it's reasonable to ask interviewees to do this, to improve the effectiveness of hiring process.
Igor Krivokon
That is the same school of thinking that suggests that, for example, a civil engineer must be able to do short and long division and square roots on paper (like he learned in junior school), "for you never know when you're gonna be without a calculator near by". In reality that never happens. Companies don't hire cs engineers because they know how to write <language> code by heart - they hire them because they know how to solve problems. Using whatever tools necessary. The syntax of the language's gonna change in 10 years anyway - but you'll still be required to solve problems - so
ldigas
don't write code, but use paper to find a way to solve a problem (notes, scribbles, duddles ...). Syntax's just a finishing necessity.
ldigas
So do you think that an engineer who doesn't remember how to do long division is likely to be an intellectually curious guy, or one who's good with numbers?
mquander
@mquander - You're mixing terms. I don't think engineers nowadays need to remember how to do long division. Or be good with numbers. They need to learn that at junior school, learn the principle how it's done, and forget it afterwards. Because we have computers which do number crunching for us (that's what they've been made for in the first place). What I do believe engineers must be is intellectually curious and be able to solve problems that they've been given using that curiosity. A mind is like an attic, you keep the tools you need for your work, and throw out (forget) the rest. If you
ldigas
continually think about everything, you won't have any space for new ideas.
ldigas
Intellectual curiosity has nothing to do with doing mundane tasks - and long division is a mundane task. Just like writing syntax. If you know how to solve the problem, writing the syntax is the least of your problems.
ldigas
@Idigas: "...hire them because they know how to solve problems". Exactly. And whiteboard is sufficient to explain a solution.
Igor Krivokon
@Igor Krivokon - I thought we probably misunderstood each other the first time. I have nothing against solving problems on paper, just the opposite, I encourage it. What I'm opposing is 'doing the actual work' (in this case, syntax-wise) on paper. When it should be done on screen instead.What I was trying to say earlier, solving problems is one thing (important), and knowing how to write syntax by heart (unimportant) is another, and is very little related to actual problem solving.
ldigas
My mind is an gallery more than an attic. I cherish my understanding of numbers and abstraction because I built it piece-by-piece up to what it is, and I don't think it's wise to let the foundations rot. I certainly have to divide things in my head every day, many times. Most of these times I'm estimating something relevant to the task at hand, and if I were uncomfortable doing so, I would either have to get out a calculator and lose my train of thought, or I would just not estimate it and work around my lack of knowledge. Neither path represents precision or expertise.
mquander
I don't know whether engineers do this too, because I'm not an engineer, but my point is that my comfort with what I've learned helps me accomplish via mental calculation, estimation, and intuition what might take someone else a long time with a reference book and a calculator. Knowing how to look something up is a very poor substitute for knowing how to do something.
mquander
I don't think you got my metaphor in the sense I was trying to converse it, but let's leave it at that for now. )****( You do long division in your head ? I doubt it. People usually divide integer-wise with some approximations. Estimates, at least in my case, have very little to do with calculating - if I'm doing the numbers I'm calculating it. What I consider under the term "estimates" is a result of my experience and ... , i.e. I know in what range the result's gonna be without actually calculating it.
ldigas
)****( Do you know grammar rules which you were taught in junior school ? Probably not, but still, you use them every day. (simplest exaple of what I was trying to explain in the first point).
ldigas
NOTE: Very late in my part of the world (4am); if I don't keep answering means I've gone to sleep. But, you keep 'em coming, I'll check the comments tomorrow.
ldigas
+1  A: 

Especially in an exam situation, it's pretty important to choose the shortest function / variable names as possible, while still being descriptive enough to explain their reason for existence.

Longish variable names are fine when you are typing, but if you are trying to write out a heap of code with long variable names, there is sadly a significant time penalty.

marcush
A: 

Use a typewriter.

Joe Philllips
+4  A: 

if you are in a situation where someone ask you to solve a problem writing code in an interview, sometimes they are not only looking at the code itself but also the logic you use to solve the problem. You can answer with an excellent code, that compiles with no error/warning but you use a wrong approach or inefficient one, and then comes a not so good coder but with a better elegant solution and gets the job. For exams the thing its different, you should try to do the best solution with the right code at once, as someone else said practice a lot with exercises, i used to do test cases, draw a table where I put the variables, assign initial values and change those values as i walk through the code looking if the desired value is the one i get from the code (as a debug)

JoeOk
The logic and basic constructs are more important than having every semicolon.
y0mbo
A: 

This usually only applies in an academic setting, so I'll assume that's the constraint--

First, use really clear names. Treat this more like you're writing an essay, since the instructor is hoping to see the quality of your thinking. Try to avoid single letter variables.

Re-read your own code when you're done and try to correct small errors. Missing semi-colons, mis-use of core language features, etc. are an easy "-1" for a grader, so try not to give them that chance. Grammar is enormously important when programming.

Comment your code (use your language's comment syntax) to explain what you're thinking. Again, even if the answer is a little off, if the instructor can see what you intended then you're more likely to gain points.

Lastly, try spending a little time writing small, real programs to test out simple ideas. You'll do better if you can shake out small mistakes from your program while you have the assistance of a real compiler and execution environment. One of the reasons instructors have you write programs on paper is to test how well you've learned the lessons of working in a live environment.

AHelps
Luckily, Prolog allows the definition of operators which means the source can look more like an essay than you probably meant! :)
Tom Wright
+10  A: 
none
Fun is allowed, but you should mark your question as "community wiki" for you should not get reputation points for jokes [at least until the "fun" badge is released]
OscarRyz
Thanks for your comments, actually I am pretty familiar with punch cards and the corresponding machinery, having had access to a legacy government device for quite some time. Unfortunately, my "kid times" have long been a thing of the past ...Besides, I didn't realize that there's the possibility to actually have individual responses become a community wiki... The 'fun' badge does however sound like a reasonable idea, especially given that similar ratings are also used by sites such as slashdot or digg ...
none
+1  A: 

When I interview someone I give bonus points if they talk through a couple different scenarios before they start to fling code (on paper or otherwise). I would suggest talking through a couple scenarios. Then maybe sketch out a flow chat (really simple) or something.

There are a number of other good tips here too. I wouldn't want to repeat them.

Jim McKeeth
A: 

I keep a Pink Pearl in my wallet for emergencies.

Nosredna
+3  A: 

For school situations, find out what the prof wants. From my experience (professors differ), I've never had a prof ding me on little syntax mistakes when writing out code. For the most part, they wanted to know how I solved the problem and paid much less attention annoying little errors.

nikudesu
+2  A: 

If your code involves manipulating linked-lists, trees, and other reference/pointer based data structures, then always always always draw a picture. If you're like me, this will help you more than you might expect. I've seen people struggle quite a bit at a whiteboard trying to reason through things from first principles, but a picture can help keep track of things, and demonstrates to an interviewer (if that's the context) that you know what's going on.

PeterAllenWebb
A: 

If you're using a whiteboard, make sure the cap the marker between questions! Otherwise it dries up and you'll have to recap and wait awhile before it'll write again.

Han