God knows what will happen in each situation. He sets limits so we learn to stay on that straight and narrow path.
Of course, we all mess up. The Ten Commandments were given because God wanted to demonstrate that not everyone can be good all the time, because of our sin nature, and that people needed to trust in God for forgiveness and mercy.
However, there was another reason, too. All sin against others hurts other people. If there were no law against stealing, anyone could take anything of yours and you would have no recourse. Of course, sometimes thieves aren’t caught in this life. However, God will always make sure that a person’s sin will find him or her out. The things people do in the dark, God sees and judges. He knows our innermost thoughts.
He sets limits on us to protest us, and those limits are for everyone to follow. He hates having to discipline. So should any good leader.
He knows, however, that there are always different circumstances, things which might change the judgment He renders. That is part of showing Godly wisdom.