Rather than starting from scratch yourself, I would recommend using existing libraries or snippets which can be found online. The most extensive one I know is the one written by Andrew FawcettAndrew Fawcett and can be found on github.
The ReadMe contains quite some references to blog posts which illustrate how these apex classes can be used.