Some Christians assume that the law was given as a temporary way of righteousness until Christ came. The problem with this view is that at least three thousand years had gone when God gave the law. If a temporary system of righteousness was needed, why did God wait for so long to give it. This does not make sense.
Abraham understood righteousness through faith in God 400 years before the law was given (Rom 4:9). Moses was also accepted by faith, before God gave him the law (Heb 11:24, 25). If righteousness through faith was already available, why would God give a system of righteousness by law? Why would God replace a superior righteousness with something inferior? This does not make sense.
The answer is that God gave the law just when it was needed. Up until the time of Abraham, there was plenty of room in the world, so people did not need live in close proximity. When people had disputes, they could just move away from each other. This is why Lot and Abraham separated. By the time of Jacob, people were starting to live closer together and disputes over property were becoming more prevalent (Jacob and Esau, Jacob and Laban). A system for resolving disputes was needed, but then Jacob went down to Egypt to live.
While they were slaves, they did not need a system of justice. They had no choice but to accept the Egyptian system. The situation changed when they escaped the Promised Land. A million people would be living together in a small country, so disputes would be bound to occur.
God gave the law to deal with the problem. He gave it just when it was needed. The law was a system of justice for dealing with disputes over property and violence towards people.
God gave the law to restrain crime. That was his purpose for the law. It never had any other purpose and that purpose has not changed. It was given when it was needed. God’s timing was perfect.
A method of restraining wickedness is still needed. Everyone understands this. Even nations that reject God have laws against theft and violence. They might mess up God's law by adding human accretions, but they generally understand the need for laws against these crimes. People understand that law is essential for a peaceful society.