TheLike in D7 the main property of a reference field in D8 is still the target id
:
$node->field_code_used_by->target_id = $user_id;
The entity
property is a computed value. If you put an object in this value, drupal will convert it back to the id before saving it to the dababase.
Multivalue fields
Add an item to a multivalue field:
$node->field_code_used_by[] = ['target_id' => $user_id];
The property target_id
is not necessary, because it is the main property and used by default. So for most fields you can add a value like in a simple php array, which is easy to remember:
$node->field_code_used_by[] = $user_id;