Therefore the LORD hath recompensed me according to my righteousness; according to my cleanness in his eye sight.
Therefore the Lord's recompense in this verse belongs to a song David composed while Saul was hunting and pursuing him to take his life and kill him. It reflects David's trust in God and confidence through severe trials. The song recounts his dangers and deliverances. It praises God as his merciful preserver for preserving him. The verse means that the Lord recompensed and rewarded David according to his righteousness and integrity and according to his cleanness and purity of heart and conduct in God's sight. The surrounding lines define that integrity as keeping the Lord's way, not turning wickedly from God, not departing from His judgments and statutes, and keeping himself from his iniquity. In this it reflects the integrity and purity of heart characteristic of David's songs from this earlier period.
The song containing this declaration was composed after deliverance from Saul and after establishment as king, apparently before the great sin, and 2 Samuel 22 records it in that historical setting. It belongs to David's triumphant psalm of thanksgiving and praise in 2 Samuel 22, a song of praise and a psalm of thanksgiving for that deliverance. It is the same song as Psalm 18, found there with very slight changes and differing somewhat in wording while strongly resembling it. In the verse David declares that the Lord repaid him according to his righteousness and according to his cleanness in God's sight. In the song all victory is ascribed to God's goodness and power rather than to David's own prowess in war. David is a type of Christ, so his victory over enemies represents Christ's victory for and in believers over sin and spiritual foes, pointing beyond earthly battles.