The Torah states (Devarim 4:15) "guard your souls" and elsewhere (Devarim 22:8) commands us to "make a guardrail on your roof".
It is a positive commandment for an owner to build a guardrail around the flat roof (pporch, veranda, stairs, landing etc.), as Deuteronomy 22:8 says: "And you shall make a guardrail for your roof." This applies with regard to a building used as a dwelling. But for a warehouse or a cattle barn and the like, there is no necessity. Similarly, any building that is not four cubits by four cubits (six feet by six feet) does not require a guardrail.
If so, why does the verse state "your roof"? To exclude synagogues and houses of study, for they are not intended to serve as dwellings.
The height of a guardrail should not be any less than ten handbreadths (between 36 and 42 inches), so that a person who might fall will not fall from it. A guardrail must be a partition strong enough to enable a person to lean on it without falling. Anyone who leaves his roof open without a guardrail negates the observance of a positive commandment and violates a negative commandment, as Deuteronomy 22:8 states: "Do not cause blood to be spilled in your home."
This requirement applies to a roof, and similarly, to any place that might present a danger and cause a person to stumble and die. For example, if a person has a well or a cistern in his courtyard, he must erect a sand wall ten handbreadths high around them or make a cover for them, so that a person will not fall in and die.
Similarly, it is a positive mitzvah to remove any obstacle that could pose a danger to life, and to be very careful regarding these matters, as Deuteronomy 4:9 states: "Beware for yourself; and guard your soul." If a person leaves a dangerous obstacle and does not remove it, he negates the observance of a positive commandment, and violates the negative commandment: "Do not cause blood to be spilled."
Our Sages forbade many matters because they involve a threat to life. They include: A person should not place his mouth over a conduit through which water flows and drink. Nor should he drink at night from rivers and lakes, lest he swallow a leech without seeing. (Rambam Hil. Shmirat Hanefesh Chapter 10)