Working with legacy code. How?

Where I work, I have been using Limesurvey for over 2 months now. It was here before I joined the company, and my main task is to maintain it, fix bugs, add features etc.

The initial problem was that it was using the Yii framework. So I bought a book and studied the hell out of Yii. So, the first task I was given was to change the sessions from memory to database sessions. I was thinking hell, this'll be easy. How wrong I was.

Turns out that the developers, and I am not saying they are bad developers at all, but they bastardised the Yii framework. They had a non-standard place for their controllers, weird routing rules and just spaghetti code. This is a prime example of non-tested code. Well, I say that as I can't find any tests, but they may have them somewhere else.

So after a couple of frustrating hours I finally managed to switch to database sessions. Lesson learnt - look at the program before you buy a book on what it is based on.

Now, after 2 months, I pretty much know how everything works. Don't get me wrong, it is an amazing piece of software, extremely powerful and robust. Just, it is very sporadic.

So back on topic - working with legacy code. How do I work with it? First, study the hell out of the program and try to understand the programmers mind, even if you think they must've been drunk the entire time. Second, try to understand the reason they did it like that. Why would a sane person do such a thing? Thirdly, get really annoyed at bad programming practices. Forth, ignore all the problems that you have been dying to fix and give in. Write the best code you can possible write, given such a plethora of messy code. Fifth, the most important part, congratulate yourself on actually achieving something with the program.