Sometimes, it helps to just put in the comments why you did something that you know is not optimal.
Often you'll write "bad" code just because it makes sense. I had to update something last week where the requirements changed dramatically from when they were written. It's nobody's fault. When the requirements were written, we did really understand the problem. After the customer got the code in their hand, they wanted to extend it in a way that wasn't obvious to us or them. I could have done a multi-month refactoring to make it clean and pretty, but that didn't make any business sense, so I did some surgical changes. To avoid someone coming back next year and thinking WTF, I added this to the comment (most of the details left out for anonymity)
//...Not how I would design it from scratch, but a nice evolutionary change.
Now next year when someone else looks at it (or even next-year me), we won't waste time thinking if we should refactor it, because it's obvious that we already looked at the pros and cons and decided not to.