Verses 21-31 [20-30], function as a distinct unit within the Psalm. Many commentators would view it as the closing portion of the first half—or the first of the two original poems that make up Ps 18 / 2 Sam 22—and this would seem to be correct (cf. below). It is also possible to view these verses as intermediary, forming a bridge between the poem of deliverance (discussed in Parts 1 and 2) and what follows in verses 32-46, as a hymn giving thanks for (military) victory. The worthiness of the Psalmist is emphasized in vv. 21-31, from two thematic standpoints: (1) a judicial setting, stressing action that is in accord with justice, and (2) the idea of covenant loyalty, i.e. between a vassal and his sovereign. Both of these aspects are frequent in the Psalms, and reflect, to varying degrees, their royal theological (and ritual) background. The royal background is especially strong in the discernibly older poems, including those (such as Psalm 18) which may genuinely go back to the time of David.
It is possible again to divide verses 21-31 [20-30] into two parts, with vv. 21-25 [20-24] as they stand forming an inclusio, v. 25 essentially repeating the declaration in v. 21. The middle three couplets (vv. 22-24) most clearly evoke the judicial setting, as the Psalmist demonstrates his claim from verse 21/25.
Verses 21 
“YHWH (has) dealt with me according to my justice,
according to (the) cleanness of my hands he returned (it) to me.”
In the context of the preceding verses, the Psalmist here states the reason why YHWH has heard his cry for help and acted to rescue him. It was because of “my justice” (yq!d=x!), that is, because the Psalmist has acted in a just and upright manner. The parallel in line 2 is the “cleanness [rb)] of my hands”. The basic idea of the root rrb seems to that of a substance (such as metal) that is clean and shining, free from impurities, etc. Clearly it is used here in an ethical sense—i.e., to be free from sin and guilt, with the idiom of “clean hands” being natural (drawn from the idea of ritual purity), and attested variously in the Scriptures, especially within the Wisdom traditions (e.g., Psalm 24:4; 73:13; Job 17:9; 22:30, etc).
Metrically, yq!d=x!K= is to be preferred over yt!q*d=x!K= in 2 Sam 22, which has the more abstract noun hq*d*x= instead of qd#x#. The rhythm of the 3+3 couplet is better preserved here in Ps 18.
Verses 22-24 [21-23]
“For I have guarded the ways of YHWH,
and have not done wrong (against) my Mighty [One];
(in) that all His judgments are th(ere) in front of me,
and His inscribed (decree)s I did not turn (away) from me;
I have been complete(ly straight) with Him,
and guarded myself from (any) crookedness (with) Him.”
In this trio of 3+3 couplets, the Psalmist, as in a judicial setting before YHWH, demonstrates his claim to justice in v. 21. It involves three instances of synonymous parallelism—one in each couplet—by which his loyalty and faithfulness to YHWH is affirmed. Here, we are dealing more properly with the idea of covenant loyalty, and this is expressed three ways, corresponding to the three couplets.
The first couplet generally refers to the “ways of YHWH”, a kind of blanket reference to God as a sovereign exercising authority over a vast domain (the Er#d# can refer fundamentally to a territory). The Psalmist, as a subordinate (vassal), declares his faithfulness with the dual-aspect motif of “guarding” (vb rm^v*, repeated in v. 24b) the covenant bond, along with avoiding the opposite (i.e. doing wrong against his sovereign). The syntax of the second line is a bit uncertain, as the MT reads yh*ýa$m@ yT!u=v^r* aýw+ (“and I have not done wrong from my Mighty [One]”). However, the use of the preposition /m! (“from”) after the verb uv^r* is somewhat awkward and unexpected. Cross and Freedman (p. 27) propose that the text be emended to an original þm ytuvp, reading the verb uv^P* instead of the more general uv^r*. This is an attractive solution, as the verb uv^P*, which can distinctly connote the breaking of a covenant relationship, even to the point of a rebellion/revolt, can also be used with the preposition /m! (cf. 2 Kings 8:20, 22), i.e., “break (away) from”, “rebel from (the authority of)”.
In the second couplet, the focus is on the royal decisions and decrees of YHWH. The noun fP*v=m! (“judgment”), preserves the overall judicial context of the passage, but also refers specifically here to the idea of the sovereign as ultimate lawgiver and adjudicator. The noun hQ*j%, provides the parallel, emphasizing the written (authoritative) decrees of the sovereign. For the people of Israel generally, this refers to the decrees and regulations inscribed in the Torah, the written document(s) that preserve the terms of the binding agreement (covenant) with YHWH. The ruler (and the ruling family/dynasty) has his own related binding agreement, as a vassal king under YHWH, the ultimate sovereign. Here the Psalmist’s conduct has to do with not turning (i.e. setting) aside (vb rWs) the very terms of the covenant that are right there in front (dgn) of him; the implication in the first line is that he has kept God’s decrees in front of him, an indication of his faithfulness. The expression “from me” (yn]M#m!) is parallel to “from my Mighty One [i.e. God]” in the first couplet, and would seem to confirm that the prefixed –m there is indeed the preposition (and correct, contra Dahood, p. 111).
The 3+3 meter in the third couplet is less secure, in the text as it stands (the second line has two beats). The longer form of the initial verb in 2 Sam is more likely to be original (with or without the w-conjunction, cf. Cross and Freedman, p. 28). There is also a difference in the preposition in the first line: ol (“to[ward] him” 2 Sam) vs. oMu! (“with him” Psa). There is presumably little difference in meaning, since both would refer to the covenant bond between the Psalmist and YHWH, but the reading in Ps 18 (oMu!) should perhaps be preferred on metrical grounds. While it is possible that a word has dropped out of the second line, I tentatively follow Dahood (p. 112) in reading yn]ou&m@ as preserving an archaic 3rd person singular (object) suffix, “perversion/crookedness [i.e. acting crookedly] (with) Him”. This establishes the parallel with the first line—the Psalmist’s complete (<mt) loyalty and integrity means that he never bends or twists, so as to act crookedly toward his Sovereign.
Verse 25 
“And (so) YHWH has returned to me according to my justice,
according to the cleanness of my hands in front of His eyes.”
This effectively restates the declaration in v. 21, having been proven and affirmed by the evidence presented in vv. 22-24. The Psalmist’s covenant loyalty has been confirmed before God in this judicial setting. The verb in this regard, in both vv. 21 and 25, is bWv (“[re]turn”)—since the Psalmist has proven himself just, YHWH returns justice to him, based on the covenant bond, acting to rescue and protect him in his time of need.
The second portion functions as a hymn of praise to YHWH, for His faithfulness and justice. Even the Psalmist declared it on his own behalf in vv. 21-25, now he affirms the same of His Sovereign. It is thus a fitting conclusion to the first half of the Psalm, and prepares the way for the hymn of victory that follows in vv. 32-46.
Verses 26-27 [25-26]
“With (the) loyal, you (yourself) are loyal,
with (the) complete, you (yourself) are complete;
with (the) pure, you (yourself) are pure,
but with (the) crooked, you (yourself) are twisted!”
These two couplets form a gnomic (proverbial) pair, which affirms the covenant faithfulness of YHWH, in terms of the extent to which the other party (i.e. the vassal) has been faithful. This reflects a perfect kind of justice, since God’s response mirrors that of His human vassal (the lex talionis principle). The first three statements are synonymous, with the goodness (loyalty, ds#j#) of the faithful servant expressed by two other characteristics previously mentioned in vv. 21-25 (cf. above)—complete integrity (<mt) and purity (rb), both of intention and conduct. The fourth line indicates the opposite, the other possibility—i.e., disloyalty and lack of integrity—and the initial w-conjunction should be read in an adversative sense (“but…”). The idea of crookedness, of bending away from covenant loyalty, was previously expressed by the noun /ou*, but here by the adjective vQ@u!, indicating something that has been distorted. Here, too, YHWH responds in kind; if the person is crooked, distorting the covenant bond, then He also will be twisted toward him. The verb used is lt^P*, which can connote the twisting that occurs as one grapples/wrestles with another.
Verse 28 
“For you, you save (the) people bent down,
but (the) eyes raised high you bring (down) low!”
This couplet continues the motifs from v. 27, including the image of bending, here expressed in terms of the result of the crookedness/perversion of the disloyal (wicked), who oppress the populace. Built into this is the same contrast from v. 27—God is faithful to the righteous, but opposes the wicked. In each instance, there is a reversal of fortune—the oppressed are raised (saved/rescued), while those with worldly ambition (“eyes raised high”) are humbled. The reading of this verse in Ps 18 more accurately reflects the original (cf. the discussion in Cross and Freedman, p. 28).
Verses 29-30 [28-29]
“For you are my (shining) light, O YHWH,
my Mighty [One] makes bright my darkness;
for with you I run strong of limb,
and with my Mighty [One] I (can) leap a wall.”
These two 3+3 couplets, like that of v. 28 , begin with the particle -yK!. The implication seems to be that the promise of salvation for the faithful is here being applied personally by the Psalmist. I.e., what you do for those loyal to you, YHWH, when they are oppressed, may you do (now) for me. This theme of deliverance, so central to the earlier sections of the Psalm (cf. Parts 1 and 2), is here expressed through two different motifs: (1) light to see by, and (2) strength of limb (for battle, etc). The first motif is more general, common to many different religious and wisdom traditions. The reading of Ps 18 in this line appears to be conflate, with the variant readings(?) rn@ and ra) combined (2 Sam has only rn). Both words essentially mean “light”, though rn@ more properly indicates a shining/burning light (or “lamp”). Otherwise, the reading of Ps 18 is to be preferred over 2 Sam.
The motif in the second couplet involves physical strength, which foreshadows the military imagery in vv. 32-46 (to be discussed in the next study). Somewhat difficult is the exact meaning of the word dwdg; I tentatively follow Dahood (p. 114) in relating it to the root dyG, generally denoting strong limbs (i.e. muscles, sinews). Elsewhere, the noun dWdG+, presumably derived from ddg (“cut [through]”), seems to refer to an attacking military force. That may also be the sense here; the parallelism could be: “I rush through an army // I leap over a wall”.
Verse 31 
“The Mighty (One)—His way is complete,
the word of YHWH is (as) pure (metal),
He is protection for the (one)s taking shelter in Him.”
The final verse is a 3+3+3 tricolon, the first line of which echoes early poetic language such as in Deut 32:4, while the latter two lines seem to reflect (later) Wisdom tradition (being very close in wording to Prov 30:5). As I have already mentioned several times in these studies, many Psalms, in their closing portions, appear to have been influenced by Wisdom traditions. This was a natural by-product of the adaptation and use of earlier poems for a wider audience, giving to the ritual and royal-theological setting a wisdom application for the people as a whole. A possible explanation for the tricolon form of v. 31 is that an early couplet was modified in light of Prov 30:5 (or a similar Wisdom tradition), the second line being ‘replaced’ by an additional wisdom-couplet which would close the poem (cf. Cross and Freedman, p. 29). If so, it would confirm that verse 31 marks the end of the first of two poems that eventually came to make up the Psalm, and may have circulated independently for a time, before being joined with the second poem (vv. 32ff).
References marked “Dahood” above are to Mitchell Dahood, S. J., Psalms I: 1-50, Anchor Bible [AB] vol. 16 (1965).
“Cross and Freedman” refers to the study by F. M. Cross and D. N. Freedman Cross and Freedman, “A Royal Song of Thanksgiving: II Samuel 22 = Psalm 18”, originally published in the Journal of Biblical Literature [JBL] 72/1 March 1953, pp. 15-34.