What do we mean by law? It is a system of rules created to govern humanity. Law is derived from the notion of good and evil. What is good? And What is Evil? Giving pleasure is Good and giving pain is Evil. It is immaterial whether giver suffers pain or pleasure doing so.
When people started living together they had to find ways to live in harmony. Earlier codified laws were derived from religious beliefs. Clear laws were needed to settle disputes. Some disputes were settled by trial by combat or trial by ordeal. The general use of trials by ordeal was abolished under English King Henry III. Trial by combat was abolished in UK in 1819. Some Laws were bizarre. In the 9th-century Codex Lambacensis, a manuscript of church rules, involved dropping the accused into a pond, lake, or river. If he sank, he was innocent.
Laws extend to property or commerce as well. These laws are present since ancient times and steadily adapted. Sadly, human slaves were regarded as property and later laws were created to abolish slavery.
Law is not perfect and it must be regularly refined.