John Gill's Exposition of the Whole Bible
Psalms 18:21
For I have kept the ways of the Lord,.... Not those which the Lord himself walks in, his ways of providence, or of grace; though these are and should be taken notice of and observed by good men, as the word y used will bear to be rendered; but the ways which he has prescribed and directed men to walk in, the ways of his commandments, in which they should go; these were, in some measure, kept by David, who often, in the hundred nineteenth psalm speaks of his keeping the testimonies and statutes, and commandments of the Lord; as they are by good men, with some degree of pleasure, they take delight to walk in them; and with some degree of constancy, they keep walking in them, without turning to the right hand or the left, though solicited to it; but yet not perfectly, for they have many a slip and fall in them; wherefore this cannot be a reason of their being rewarded according to their righteousness: in strict justice, the words better agree with Christ, who kept the law of God perfectly, did his will completely; he came from heaven to do it; it was his meat and drink to accomplish it; and he always did the things which pleased his father, wherefore he rewarded him;
and have not wickedly departed from my God; which was, in some sense, true of David; not as by disbelieving the power and providence, the promises, truth, and faithfulness of God, and his covenant interest in him; which to do would have been a wicked departure from God; see Hebrews 3:12; nor by forsaking the house and worship of God; though he was driven from thence by wicked men, yet sore against his will, and which during his exile he frequently laments and complains of; nor by sinning wilfully and presumptuously, only through error, inadvertency, infirmity, and temptation: but when it is observed, how much unbelief, which is a partial departing from the living God, and how many there are that neglect private and public worship, and what a proneness there is to sin and wickedness, and how much there is of the will in sinful actions, in the best of men; it is right and best to understand this of Christ, who never was guilty of sin, nor committed any wickedness in departing from God in the least: as man, God was his God, and he always believed his interest in him, and claimed it even when he forsook him on the cross; nor did he quit his service, desert his cause, nor depart from the work and business he enjoined him, till it was finished.
y שמרתי "observaveram", Tigurine version, Vatablus; "observo", Junius Tremellius "observavi", Gejerus, Michaelis; so Ainsworth.