CLAIM: David says, “For all His ordinances were before me, and I did not put away His statutes from me. 23 I was also blameless with Him, and I kept myself from my iniquity. 24 Therefore the Lord has recompensed me according to my righteousness, according to the cleanness of my hands in His eyes” (Ps. 18:22-24). How can he write this, when he committed murder and adultery?
RESPONSE: If we read the superscription of the psalm, we discover that this was written before David committed adultery and murder. Therefore, it is anachronistic to claim that he was guilty at this time. Moreover, in context, David writes that he was considered righteous because he was “humble” (v.27 NIV). He also admits that God “turns [his] darkness into light” (v.28). He later writes that he is considered righteous because of God (v.32), not himself. David admits his sin elsewhere:
(Ps. 25:6-7) Remember, O Lord, your great mercy and love, for they are from of old. 7 Remember not the sins of my youth and my rebellious ways; according to your love remember me, for you are good, O Lord.
(Ps. 25:11) For the sake of your name, O Lord, forgive my iniquity, though it is great.
(Ps. 25:18) Look upon my affliction and my distress and take away all my sins.
(Ps. 143:2) do not enter into judgment with Your servant, for in Your sight no man living is righteous.
David also admits that he needs God to be his savior (v.46). But this is, of course, a savior from his enemies—not God’s wrath (v.48). Since the context here is not righteousness before God, but righteousness before the wicked men in battle, this passage doesn’t contradict Paul’s statement that “all have sinned and fallen short of the glory of God” (Rom. 3:23).
Regarding a similar difficulty in Psalm 5:4-6, Derek Kidner argues that David is pleading relative innocence in relation to his enemies—not in relation to God. He writes,
This emerges openly in Psalm 143:2, where David the plaintiff (as he is in the present psalm) pauses to acknowledge that if God were to try his character instead of his case, he would be undone. This is taken for granted in the psalmists’ protestations of innocence. They know that they are in the right vis-à-vis their opponents, as disputants in, so to speak, a civil court; and in general relation to God and his law their heart is ‘perfect’: they are totally committed. To press their language beyond this, in view of, e.g., Psalms 19:12; 32:1–5; 130:3; etc., would be the equivalent of (in NT terms) pressing such a passage as 1 John 3:4ff. into contradicting 1 John 1:8ff.
When a person goes into court, he isn’t claiming to be perfectly righteous, but righteous in regards to the issue on trial. Likewise, David is arguing his righteousness compared to his enemies in this dispute. For further reading, see C.S. Lewis, Reflections on the Psalms (New York: Harvest Book; Houghton Mifflin, 1958), “Chapter Two: ‘Judgement’ in the Psalms.’” He compares this to two little boys fighting over a pencil. When the teacher judges this situation, he doesn’t ask, “Which is the nicer little boy?” but rather, “Whose pencil is it?” Similarly, argues Lewis, these psalms of judgment are related to who is in the right, rather than who is completely righteous.
 Kidner, D. (1973). Psalms 1–72: an introduction and commentary (Vol. 15, p. 75). Downers Grove, IL: InterVarsity Press.