In my city it's legal to ride on either, but laws vary based on location and riding on the sidewalk can be against the law depending on your location. I personally ride where I feel it is safest. If the road has no shoulder and 50mph traffic, I'm going to ride the sidewalk. If it's a back road without too much traffic, or the road has a good shoulder, then I'll ride on the road.