If you want to keep your dwarfs outside and safe from invaders, I find that there are three effective options.
build a tower
X is either a meeting area/statue garden/zoo and it it is surrounded by walls to deny hostile archers line of sight (LOS) to your civilian/ off duty dwarfs one thing to be aware of is if you have a steep cliff that rises above you tower nearby (about a screen and a half or so i think by default), than archers may still have LOS to your civilians.
2 city walls.
City walls can make invasions more difficult and secure large outdoor areas from ambush. what you do is build a solid rectangle of wall around your fort or region. The only opening you leave should ideally be the sparring grounds (weapon/ armor racks can be placed outside,as can archery targets) for your military, but at a minimum I recommend that you keep spotter animals to catch thieves and ambushes if you use a city wall. if you want to be cheap, than build a trap corridor as a gateway to the outside world. When your walls need to cover hills, make sure that ambuhers/invaders cannot path to the top of your walls
3. the ditch.
If you lack time/stone than a simple ditch accomplish the same task as a city wall with far less effort. there are some problems that a ditch has that a city wall does not. don't build it too close to the edge or animals and liaisons may have difficulty finding a path to your fort. A ditch also does not impede LOS, so hostile archers may be able to harass your dwarfs with impunity if you have a melee based military.
As a final note, beware that walls, towers and ditches do nothing against flying hostiles. some players also consider the use of city walls and ditches cheap, but that is a matter of personal preference for you to decide. maybe as the army arc develops, the necessity/ utility of siege engines and sappers will mitigate the benefits of walls, but a good city wall can render almost any army in vanilla df unable to do any real harm to your dwarfs.