I've been bothered by this, were government officials just okay with people building their houses wherever? Didn't a bunch of houses and people living next to walls make it harder to access them in case of battle? Were there no city zoninglaws in the distant past?
If you are looking at a town with construction pressed right up against the old city walls, you can bet with near-certainty that those constructions are actually fairly recent. I wrote about medieval urban density in a number of answers, most recently here.