Here are some thoughts on this seemingly contradictory statement, it is important to consider the context:
Ecclesiastes 7:15-18
‘In this meaningless life of mine I have seen both of these: the righteous perishing in their righteousness, and the wicked living long in their wickedness.
Do not be overrighteous, neither be overwise—why destroy yourself?
Do not be overwicked, and do not be a fool—why die before your time?
It is good to grasp the one and not let go of the other. Whoever fears God will avoid all extremes.’ (NIV)
The Teacher now goes on to compare righteousness and wickedness and what wisdom we need to learn from both of these. First he declares that he sees his own life as meaningless (hevel - literally vapor or smoke, sometimes means an enigma), which in the context most likely means he cannot make sense of even some of the things in his own life, not just the general ways in the world, for he along with all of us partake in and are affected by the paradoxes and injustices of the world. Many righteous people suffer and even perish, and this sometimes because of their righteousness. Yet many wicked people live long and are allowed to continue in their wickedness and sometimes have a more comfortable life than the righteous. So regarding these things, the Teacher gives us wisdom what we should avoid and then what is good for us to grasp:
‘Do not be overrighteous (7:16)’. The Teacher isn’t suggesting to avoid being righteous or that there is a limit to how righteous we ought to be, Jesus said it’s actually a blessing to be persecuted for righteousness (Matthew 5:10). Rather, we should not be self-righteous, thinking we are not sinful and don’t need forgiveness, or even if we acknowledge we are sinful, we should avoid thinking too highly of ourselves and thus look down on others. By doing so we can become overwise, too opinionated, always having an answer for everything, this has been difficult for me. ‘Do not think of yourself more highly than you ought, but rather think of yourself with sober judgement, in accordance with the faith God has distributed to each of you (Romans 12:3).’ Don’t be putting others down but be willing to acknowledge their gifts and serve God humbly. By being self-righteous we destroy our reputation and our own peace, for we will cause others to be against us. It is also possible to be overrighteous in thinking you must earn God’s favor and thus follow the Law, but many have destroyed themselves through excessive self-denial and brought themselves to despair because they cannot fullfil the Law. And it is also possible to be overrighteous, when you see how messed up the world is, and feel you must address and try to fix every injustice and every problem that you encounter. But you will quickly wear yourself out and bring yourself to despair when you realize you cannot fix even very much and it is not healthy to burden yourself with many concerns. Neither can you be wise enough to reconcile and understand all the enigmas in the world. Only God can set all things right and only he has perfect wisdom.
‘Do not be overwicked (7:17)’. The Teacher isn’t suggesting here that some wickedness is ok. Rather, we must not think we are too bad to seek mercy and by forgiven. God is willing to forgive all who come to him in true repentance, no one is too bad. It is also overwicked to act out many, or severe, wicked deeds, going out of one’s way to be wicked. Most people, even if they are unrepentant sinners, generally try to live a descent life and don’t go out of their way to hurt others and do evil, but it is certainly overwicked when people do not care about God’s forgiveness and do not care what suffering they cause. In particular those who live a life of violence risk ending up dead themselves because they make so many enemies. Those who make it their goal to carry out a massacre until they themselves are killed, that is great wickedness and foolishness!
Another translation of 7:18 reads: ‘It is good that you grasp one thing and also not let go of the other; for the one who fears God comes forth with both of them.’ This helps to make sure we understand this verse correctly. Certainly, overrighteousness and overwicedness are two extremes. So in consideration of both of these, we are called to fear God, to take hold of both overrighteousness and overwickedness. From a worldly point of view, you should avoid any righteousness or wickedness that will cause you trouble, but as we saw, it is good to be as righteous as possible, but perhaps we sometimes lack wisdom as to how we show righteous in love. But this can’t be the Teacher’s point because overrighteousness and overwickedness relating to 7:15 wouldn’t cause him to give the outcomes in the next 2 verse.
To grasp both ‘overrighteousness and overwickeness’ seems like a contradiction but it simply means the middle ground, to keep both in mind so we do not sway to the other. As C.S.Lewis says in Mere Christianity: ‘[the enemy] always sends errors into the world in pairs-pairs of opposites. And he always encourages us to spend a lot of time thinking which is worst. You see why, of course? He relies on your extra dislike of the one error to draw you gradually into the opposite one. But do not let us be fooled. We have to keep our eyes on the goal and go straight through between both errors. We have no other concern than that with either of them.’
So in light of these things and the general theme of the book, it seems most likely the Teacher’s main point in these 4 verses is: To see ourselves as God sees us, not thinking too highly or too lowly of ourselves. Both of these views can lead us to make it our responsibility to fix the 'hevel' in the world or to simply do nothing. It would be easy to just go with the flow, but we also need to see the world from God’s perspective; when the righteous perish and the wicked prosper we have to remember what is most important; to fear God and keep his commands (12:13). For it is by fearing God that we are best able to have peace of mind when injustice happens, for God will bring every deed into judgement (12:14). In light of those 2 verses, let us seek righteousness and remember the end of the wicked. Even if we should suffer for being righteous, let us fear God.
We avoid all extremes only by first fearing God, and thus walk steadfastly down his straight and narrow path, which means we are not to deviate from God’s way, or in any way try to live by both extremes based on what may seem to benefit us at the time, for that’s just hypocrisy.