refactor: extract function writeMeasurementCode