What you have mentioned – that Allaah may test a person with sin in order to bring him back to Him, and so that he will not admire himself – has been mentioned by some of the scholars. 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
Moreover it should be noted that this habit is haraam according to sharee’ah, as is indicated by the Book of Allaah and the Sunnah of His Prophet (peace and blessings of Allaah be upon him). We have already quoted the evidence in detail in the answer to question no. 329. This habit is also something that is regarded as repulsive according to man’s instincts and reason, and it is not befitting for a Muslim to lower himself to the level of doing that.
It should also be noted that sin has a negative effect on a person in this world and in the Hereafter, if he does not repent and if Allaah does not bestow His mercy upon him. We have already discussed this in detail in the following questions: 23425 and 8861.
There are causes for this habit, so keep away from them so that you will be able to give it up. You should do the following things:
1-Strive to keep company with good, righteous, pious people, and learn from them and their experiences.
2-Always remember Allaah and recite Qur’aan, and adopt a daily wird (collection of du’aa’s and dhikrs) that you recite every day without fail.
3-Set yourself a program for learning Islamic knowledge etc.
4-Set yourself an exercise program, or join a sports club
5-Do a lot of naafil acts of worship, especially voluntary fasts, for this is an important means of resisting temptation and reducing desire.
6-Strive in making du’aa’ and ask Allaah to rid you of this forbidden bad habit and to strengthen your resolve.
7-Remember that the harms caused by the secret habit are innumerable; it weakens the body, saps one’s strength, and increases the distance between a person and his Lord. It is a major factor in depression and feelings of sin.
8-Avoid being alone as much as possible, because this habit is one of the effects of being alone.
9-Try to pray in the mosque, and pray qiyaam al-layl, because this will bring tranquility to the heart.
10-Finally, you have to repent continually, weep with fear of Allaah, humble yourself before Him, and ask Him to forgive you. If you resolve firmly each time not to go back to that habit, then your nafs threatens to overwhelm you, then resist it. “But as for him who feared standing before his Lord, and restrained himself from impure evil desires and lusts, Verily, Paradise will be his abode” [al-Naazi’aat 79:40 – interpretation of the meaning]. If you give in, then repent anew, and renew your promise (not to do it again). Do not despair of the mercy of Allaah, and do a great deal of naafil acts of worship and righteous deeds. “And perform As-Salaat (Iqaamat-as- Salaat), at the two ends of the day and in some hours of the night [i.e. the five compulsory Salaat (prayers)]. Verily, the good deeds remove the evil deeds (i.e. small sins). That is a reminder (an advice) for the mindful (those who accept advice)” [Hood 11:114].
May Allaah help you to do all that is good.