Try an figure out an alt route, maybe loop around.
If you do find yourself in dubious neighbourhoods, don't stop. If you really have to stop, look around before stopping, the usual personal security stuff you would use. Stay away from the curb and keep in a lowish gear so you have some acceleration.
Try and find 3 or more regular routes and vary them.
Fit some tyres with very good puncture protection such as Schwalbe Marathon Plus.
Keep phone, wallet and keys on your person not the bike.