When one transgresses and then turns to Allah in repentance, one becomes completely concerned about this sin. The tongue does not stop asking Allah for forgiveness; the heart is troubled; the soul is sorrowful; and one becomes wholly afraid of Allah’s punishment. Worried by the sin, one becomes preoccupied with showing regret and turning to Allah in repentance, and devotes oneself to reform. When one behaves thus, the sin committed becomes better than the act of obedience, which may cause conceit and hypocrisy.
Ibn Al-Qayyim wrote about this in his book Madarij As-Salikin: A wrongdoing may be useful to the wrongdoer than doing many acts of obedience when such sin is followed by penitence. This is what one of the Salaf (one of the righteous successors of the Companions) meant when he said, “A person might do a sin which causes him to enter Paradise and, on the contrary, he might commit a sin which may lead him to enter the Hellfire.” Some people asked him, “How could this happen?” He replied, “He may commit a sin and then keeps remembering it wherever he goes, which makes him feel ashamed, regretful and, consequently, leads him to repent and ask Allah for forgiveness. Thus, this very sin can cause his salvation from Allah’s punishment. On the other hand, a person may do a good deed and he does not stop thinking about it wherever he goes, and so it brings on conceit, arrogance, and much remembrance of the favour he has done. This good deed, therefore, leads to his ruin.”
In this way, the sin may be followed by many acts of obedience, good deeds, feelings such as fear of Allah and shame before Him, and acts such as showing regret, crying with fear, and begging Him for pardon.
Any one of such consequences of committing sins is more useful to a person than an act of obedience that may cause him to be haughty and disdainful of people, feeling that he is far above them. Undoubtedly, in this case, such a sin is better in Allah’s sight and much closer to deliverance and success than that good deed whose doer is vain about it and feels that he has done a favor to Allah and His servants. Even if such a person says something different from what he really feels, Allah is All-Aware of what is really in his heart. This kind of person may show enmity toward people if they do not extol him, raise him in rank, and show submissiveness to him, and he even may feel hatred toward whoever does not treat him that way. If such a man examines himself carefully, he will find these defects hidden.

The late prominent Saudi scholar Ibn `Uthaymin (may Allah shower mercy on his soul) says: Many are the times in which one of us may be better after committing a sin than before doing it. Sometimes a person commits a sin, then turns in submission and repentance to Allah. He always remembers his sin and asks for Allah’s pardon. In contrast, a person may regard himself as an obedient person, and, accordingly, feel haughty, arrogant, and in no need of repentance; such an attitude results in spoiling his religion. Sometimes, man is afflicted with a sin to reform him in the same way that He causes man to suffer from hunger to become healthy.

A good example of this is Adam, who was chosen by Allah after Adam had repented of his sin. This is what is stated in the verse which reads “Then his Lord chose him, and relented toward him, and guided him.” (Taha: 122) The verse indicates that Allah chose, forgave, and guided Adam after he had repented. The same meaning was stated in the story of those three persons who did not join the battle of Tabuk. They rose in faith, rank, and degree much higher than they were before, and all of that happened because they turned to Allah in repentance after sinning. Had they not done this, no verse would have been revealed concerning their issue, and which will continue to be recited until the Day of Resurrection. (See At-Tawbah: 118)