Ibn al-Qayyim (may Allaah have mercy on him) said:
“Sin may be more beneficial for a person, if it leads him to repent, than doing a lot of acts of worship. This is what is meant by the words of one of the salaf: ‘A person may commit a sin and enter Paradise because of it, or he may do an act of worship and enter Hell because of it.’ They said: ‘How is that?’ He said: ‘He may commit a sin and continues to think about it, and when he stands or sits or walks he remembers his sin, so he feels ashamed and repents and seeks forgiveness and regrets it, so that will be the means of his salvation. And he may do a good deed and continue to think about it, and when he stands or sits or walks he remembers it and it fills him with self-admiration and pride, so it is the cause of his doom. So the sin may be the factor that leads him to do acts of worship and good deeds and to change his attitude so that he fears Allaah and feels shy before Him and feels humiliated before Him, hanging his head in shame and weeping with regret, seeking he forgiveness of his Lord. Each of these effects is better for a person than an act of worship that makes him feel proud and show off and look down on people. Undoubtedly this sin is better before Allaah and is more likely to bring salvation than one who admires himself and looks down on others, and who thinks that he is doing Allaah a favour. Even if he says words that indicate something other than that, Allaah is the Witness over what is in his heart. Such a person may feel hatred towards people if they do not hold him in high esteem and humiliate themselves before him. If he were to examine himself honestly, he would see that clearly.” Madaarij al-Saalikeen, 1/299
Shaykh Ibn ‘Uthaymeen said:
“How often is it the case that one of us will be better after committing sin than he was before. In many instances a person errs and falls into sin, then he feels ashamed in his heart before Allaah and he turns to Him and repents to Him, so that he thinks of that sin all the time and continually regrets it and seeks forgiveness. But another person may think that he is obedient (towards Allaah) and that he is one of the people who obey and worship Him, so he starts to admire himself and does not turn to Allaah, which adversely affects his religious commitment. Allaah is Wise and may test a person with sin in order to set him straight, just as He may test a person with hunger in order to improve his health. Adam was only chosen after he had committed sin and repented therefrom.
As Allaah says (interpretation of the meaning):
“Then his Lord chose him, and turned to him with forgiveness, and gave him guidance” [Ta-Ha 20:122] i.e., after he had sinned and repented, his Lord chose him and accepted his repentance and guided him. Looks at those who stayed behind from the campaign of Tabook – what happened to them? Undoubtedly their faith increased and they attained a higher status than they had before. Could the verses concerning them that will be recited until the Day of Resurrection have been revealed if they had not done that then repented to Allaah?”
al-Sharh al-Mumti’, 3/66
>>
Taken from this QnA