How do you write code on a whiteboard or a shared document without the safety net of an editor?
You write bigger shapes and smaller shapes than you would in an editor. And you say what you are doing the whole way through.
def reverse_words(text):
words = text.split()
return " ".join(word[::-1] for word in words)
print(reverse_words("hello world"))
So what are the practical differences from an editor? Here they are.
the difference what to do about it
------------------ ---------------------------------------------
no autocomplete write the names out in full
no error messages say the types out loud as you go
no scrolling keep the code small and say what you are skipping
no syntax highlighting use spacing and blank lines instead
no undo be careful, and check as you go
And there is one habit that saves the most time. Before you write anything, say the shape of the solution out loud. "I am going to write a function that takes a list and gives back a list, I will use a dictionary to count things, and I will return the biggest count." Then you write exactly that, and the code comes out right the first time because you did not have to think about it while writing.
And there is a second habit. Write the function signature and the loop skeleton first, and leave a blank line for the body. Then you fill in the body. That way the structure of the code is visible on the board and you never lose your place.
the skeleton to write first
---------------------------
def solve(input):
result = []
for item in input:
# the work goes here
pass
return result
And there is a third thing that is worth knowing about. When you have to erase something, erase the smallest amount that you can. Interviewers can see that you changed your mind, and that is fine and normal. But erasing half the board to start again makes them wonder whether you knew what you were doing.
So the rule to remember is this. Say the shape first, write the skeleton second, and fill in the body last.
The next two questions in this topic are about debugging live and about what to do before you start.