Sometimes we run into commandments or historical events that don't make sense in light of our gospel knowledge. We think, why would God ever have allowed polygamy? Commanded the Israelites to massacre another group of people? Allowed animal sacrifice? Told Abraham to sacrifice Isaac?
Today I was doing math preparation for the GRE test, and I ran into a similar problem. Skip this paragraph if you're not into math. It simply bothered me that the Addition Rule in probability worked the same way for both independent events and mutually exclusive events. P(E and F) is 0 for mutually exclusive events, but not for independent events. So, if E and F both have a probability of .25, the probability of at least one happening is actually HIGHER if they are mutually exclusive. (It's .5 for mutually exclusive and .4375 for independent events.) This bothered me until I realized that mutually exclusive events have a cap on their sum possibility. If E and F are mutually exclusive, than P(E) + P(F) is less than or equal to 1. This is not true if E and F are independent events.
In other words, there were extenuating circumstances. There was another law, another rule, another principle, at play, that I couldn't understand at first. It took me several minutes of thought to realize why it could make sense.
The same thing happens sometimes with the gospel of Jesus Christ. God will do or say something that we think is morally wrong, self-contradictory, and confusing. But there is always, always an explanation. There is always a reason. There may be another law at work that we don't understand. There may be another principle that we haven't been told about.
The best scriptural example is when Nephi explains why God told the Israelites to utterly destroy a few other cultures in the Old Testament. Nephi told his brethren, "Behold, the Lord esteemeth all flesh in one; he that is righteous is favored of God. But behold, this people had rejected every word of God, and they were ripe in iniquity; and the fulness of the wrath of God was upon them; and the Lord did curse the land against them, and bless it unto our fathers; yea, he did curse it against them unto their destruction, and he did bless it unto our fathers unto their obtaining power over it" (1 Ne. 17:35). In other words, God only commanded the Israelites to destroy peoples who were totally evil. I think of Molech-worship in particular, in which parents sacrificed their infants to an idol in an open flame.
Another scriptural example is when the Spirit told Nephi to kill Laban--which, at first, seems to contradict the Ten Commandments. But under the law of Moses, God had a stipulation that seems to be written especially for Nephi (Ex 21:12-13). There was another law at work that specified Nephi's innocence--and God provided a place to which Nephi could flee, as promised.
Conclusion: Whatever God commands is right--if it's really from God. According to the writings of Joseph Smith, our best certainty is in the majority of the Twelve and the records of the Church. As we grow in obedience, sincerity, faith, and love, we will be better able to determine good from evil, or, in other words, we will be more able to distinguish between that which testifies of Christ and that which does not.