How do I get an uploaded image URL or the default for use in a template (code improvement)
You don't need to load all the fields, you can just load the image field:

$uuid = $vars['node']-&gt;get('field_hero')-&gt;getFieldDefinition()-&gt;getSetting('default_image')['uuid'];
$file = \Drupal::service('entity.manager')-&gt;loadEntityByUuid('file', $uuid);


On a different note, the first line of your code assumes that $vars['node'] exists, which it won't on non-node pages, so you should change it to:

if (isset($vars['node'], $vars['node']-&gt;field_hero))