Refactor generate_func_code