How can I use the visual editor without ruining code samples?

When pages have code samples, toggling back and forth between the Visual and Code editors either removes or corrupts the code.

Because of this, I want to disable the Visual editor on my profile. However, the Visual editor is helpful when I want to do a quick formatting of the content, or when I want to review non-code samples.

Is there a way to make it so the visual editor doesn't munge my code samples?

If not, in the code editor, is there a pretty-format type of button that will take and clean up the nesting and spacing of all the html?