In the wild, before being domesticated, canines used to sleep in the open where they often shared the environment with other small critters. The habit of scratching the floor before lying down may therefore derive from the need to scare away any unwanted critters. No dog, after all, would want to share his bed with spiders or snakes.