Option 1:

func set_default_value(section:String,key:String,value:Variant)->Variant:  
	if !self.has_section_key(section,key):  
		self.set_value(section,key,value)  
	return self.get_value(section,key)  

Option 2:

func set_default_value(section:String,key:String,value:Variant)->Variant:  
	value=self.get_value(section,key,value)  
	self.set_value(section,key,value)  
	return value  
  • dumbgamelab@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    3 days ago

    Option 1 behaves closer to Python’s setdefault, since it only writes when the key is actually missing. Option 2 calls set_value on every read, which is harmless, but anyone reading a call site later will assume something changed

  • neco arc@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    7 days ago

    personally id go with option 1 since I have the preference to not set the config value if I dont have to

  • one_old_coder@piefed.social
    link
    fedilink
    English
    arrow-up
    6
    ·
    9 days ago

    Not specific to Godot but I prefer the second one. The if breaks the flow of reading IMHO. You could add a comment to explain the whole thing though.

    No judgement but why is it not called set_value instead of set_default_value? And why is it returning a value? I’m a noob at Godot.