It’s been a while since I’ve had to hand code custom fields. The one exception is when building plugins.
If you find yourself in a position where you need to hand code fields, there is a good blog post on it from Dave Clements over on WPExplorer. He references a really helpful script for creating custom fields called, Custom Metaboxes and Fields for WordPress.
One important note though, don’t add custom fields directly into a theme!!!