My rule is to by the expansion when:
1) Im capable of building on its empty slots( if i have empty slots in my main base - then its too early )
2) If i have a decent army so that i can defend the expansion.
2) ... or if i see that my opponent is building expansion bases and i dont want to go on an offensive.
Many noob'is players build their second base before having any units at all - so they finish the base build and ... watch how it gets destroyed and can do nothing about it.