For Charles Dickens to write, he also had to walk.

For large parts of his life, he walked around London with a manic focus, especially at night, and you can see the consequences in his novels: little episodes that might have been lifted straight from the streets. Nell Trent out in the dark in The Old Curiosity Shop. Bill Sykes pursued through the blackness at the end of Oliver Twist. Lady Dedlock, dead by the cemetery gates in Bleak House.

But Dickens's night walks were more than just a search for creative inspiration - there was something more intense and personal at work too. According to Matthew Beaumont, author of Nightwalking, a guidebook to the night in literature, Dickens walked compulsively as a way of dealing with emotional trauma, first over the death of his father and second over the deterioration of his marriage to his wife Catherine. "If I couldn't walk fast and far," Dickens once said, "I should just explode and perish."

Beaumont's theory is that the great man also walked at night to explore and revisit his personal history. In the early 1840s, for example, it appears that Dickens returned, at night, to the site of the infamous blacking factory where he worked as a 12-year-old while his father served his prison sentence for debt. "I often forget in my dreams that I have a dear wife and children," Dickens said of the episode, "even that I am a man and wander desolately back to that time of my life."

Quite why Dickens preferred to walk at night rather than during the day is not entirely clear, although he was certainly not alone, as Beaumont points out. So keen was Samuel Johnson on the practice of walking at night that he even invented, or promoted, a word for it: noctivagation, which he defined as the act of rambling or wandering in the night. It was an occasionally dangerous form of slumming-it that Johnson relished, although his biographer James Boswell said he rarely ran into any trouble. "He walked the street at all hours," wrote Boswell, "and said he was never robbed, for the rogues knew he had little money, nor had the appearance of having much."

And risk was part of the attraction anyway because there was, and is, an exciting moral ambiguity about walking at night. Why are you doing it? Shouldn't you be at home, like decent people? Are you a criminal? Are you homeless? Are you dangerous? Beaumont homes right in on this aspect of the night walk in his book and it is the most entertaining theme. He quotes Dickens again who said that there were two phases of night: the social night (in his words: the night of lights and pleasure) and then, a little later, the antisocial night, what Dickens called the night of guilt. If you're still out in that second phase, you may be a bad person.

Beaumont explores where these attitudes came from, and how they have persisted. So profound was the prejudice against anyone walking at night in the 1300s that a law was passed that permitted private citizens to arrest any strangers at night, and by the 17th century, the term nightwalker was interchangeable with the term prostitute. Magistrates even adopted a more punitive approach to people apprehended at night, as if a crime in the dark was somehow more deviant than one committed in the purifying light.

Beaumont's history of these attitudes ends in the 19th century (presumably because street lighting became ubiquitous and we became used to being out and about at night) but his story doesn't feel old or distant. In fact, it feels like there's something we can learn from it, which is to walk in a different way.

Take Johnson again. In 1729, he would regularly walk from Lichfield to Birmingham and back, a distance of about 32 miles. It was partly an attempt to beat his depression, as if he could sense hundreds of years before we know it to be so that physical exercise is good for your mental health.

But Johnson was doing something else as well. He was, according to Beaumont, a militant pedestrian. He believed that you could walk to get from somewhere to somewhere else, but that a much better form of walking was to walk for the sake of it. For him, walking mostly meant observing, thinking, looking, and how many of us do that now? Perhaps at night, we don't do it because we're afraid to, but during the day it's probably because we are, or believe ourselves to be, too busy.