Ibn al-Qayyim (rahimahullah) 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 pious people of the past: ‘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 Allah 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 Allah and is more likely to bring salvation than one who admires himself and looks down on others, and who thinks that he is doing Allah a favour. Even if he says words that indicate something other than that, Allah 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.” -Madarij as-Salikeen, 1/299 [Source: AbdurRahman.org]