What to make of David's claim of righteousness
What are we to do we do with David’s claim in Psalm 18:20-24?
 The LORD dealt with me according to my righteousness; according to the cleanness of my hands he rewarded me.
 For I have kept the ways of the LORD, and have not wickedly departed from my God.
 For all his rules were before me, and his statutes I did not put away from me.
 I was blameless before him, and I kept myself from my guilt.
 So the LORD has rewarded me according to my righteousness, according to the cleanness of my hands in his sight.
This boasting of self-righteousness does not seem to align well with the truths of the gospel, Specifically, it seems to contradict the truth that all mankind is sinful and guilty when held to the standard of God’s Law. “None is righteous, no, not one; no one understands; no one seeks for God. All have turned aside; together they have become worthless; no one does good, not even one.” (Romans 3:10-12)
So, what is going on with David? Why is he offering up this type of justification? And, more importantly, does this passage imply that there is a self-righteousness that influences God to act in our favor?
Let’s address David’s motivation first. I see three possible reasons, although I’m sure there are more.
- David is offering a classic retort to circumstances of personal injustice - In short, David is saying, “Look, I didn’t do anything wrong here!” We’ve all said this in moments where we feel mistreated for no good reason. However, theologically speaking, the standard of righteousness before God is perpetual holy perfection. David was hunted by Saul and other enemies, but by God’s grace and help, he was eventually made king and for a time enjoyed prosperity. God defended him as he was mistreated, and David is speaking from a place of joy.
- David is leaning on an Old Testament covenant view of the Law - In OT Jewish culture, righteous men were those that consistently and sincerely sought to keep God’s Law. It was not that they kept the Law perfectly, but that their lives reflected a Godward orientation. Similarly, we might say, “Clark is a good man.” True, Clark is a good man, but he is also a sinner in need of grace like the rest of us. Spurgeon writes that "It is not at all in opposition to the doctrine of salvation by grace, and no sort of evidence of a pharisaic spirit, when a gracious man, having been slandered, stoutly maintains his integrity, and vigorously defends his character.”
- David is resting in an atonement view - We know David committed adultery and murder, so we know he is far from innocent. Just a few chapters later, David will cry out to God to take away his sin. Regardless, there is a means of purification of sins that David could employ. David may have repented, offered sacrifices, and therefore seen himself in this moment as free from guilt when he composed this Psalm.
Whatever David may have to offer in terms of his personal effort to attain righteousness, it is merely a fig leaf (http://www.christholdfast.org/blog/fig-leaf-righteousness). The same is true for you and me. We are more guilty than we are willing to admit but more loved than we can possibly imagine.
I believe the rest of Psalms showcases that David actually believed that “no one is righteous, no, not one. No one understands; no one seeks after God. All have turned aside, together they have become worthless. No one does good, not even one.” In fact, these words are not just from Paul in Romans 3:10-12, but also from David in Psalm 14.
God is not into Karma. We cannot earn God’s favor. As those who are in Christ, we don’t have to. His favor is already poured out for us in Christ. “If God is for us (and he already is because of the work of Jesus), who can be against us?” (Romans 8:31)
David knows that God “does not deal with us according to our sins, nor repay us according to our iniquities.” Psalm 103:10 He is a God of mercy (Psalm 18:25).
Will we reap what we sow? Yes. Sin has consequences, as does righteous living. Does God owe us for any righteousness we think we’ve pulled off? No. God is not obligated, nor does he operate on terms where our attempts at righteousness gain for us extra blessings or favor from God.
The blessing of Christ is the blessing of imputed righteousness. Because of the cross, we who believe have all the righteousness of God added to our account, though we don’t deserve it one bit.