UPDATE: The consensus seems to be overwhelmingly in favour of the match variant. And not to worry, I have replaced the magic numbers with an enum. Will try to remember to merge the branch tomorrow

Does an if-statement block or a switch statement fit better here? For context (and advertisement), this is part of my all-purpose utility plugin ( Codeberg link)

The code:

		# Method 1 (Yandere Dev Technique)  
		if self.throw_errors and status==MpupTest.TESTSTATUS.ERROR:  
			push_error(result)  
		if self.throw_warnings and status==MpupTest.TESTSTATUS.WARNING:  
			push_warning(result)  
		
		# Method 2 (Pirate Software Technique)  
		match status:  
			MpupTest.TESTSTATUS.ERROR:  
				if self.throw_errors:  
					push_error(result)  
			MpupTest.TESTSTATUS.WARNING:  
				if self.throw_warnings:  
					push_warning(result)  
  • lbfgs@programming.dev
    link
    fedilink
    arrow-up
    7
    ·
    3 days ago

    Not sure about how to do this with Godot, but a common pattern for this in other languages is creating something like a 32 bit value where the lower bits are the enum and the upper bits are the status code (assuming they are <=16bit each) and then doing a switch on the resulting packed value.

    • sp3ctr4l@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      5
      arrow-down
      1
      ·
      3 days ago

      Both these devs are far too full of themselves to think of using an enum table or anything clever with datatypes themselves.

      Because that would mean the way they’ve been doing things was potentially grossly inefficient at runtime… and they’ve been doing that for years…

      But a narcissist never makes a mistake, so instead, here’s a bunch of reasons why actually writing optimized code is stupid, first among them being ‘well it didn’t occur to me, therefore doing it this way would reduce code readability for this code base that only I and no else needs to read’.

      I am guessing the concept of a sentinel value or an overloadable function would make their minds implode.

      But anyway, ‘switch’ is ‘match’ in gdscript:

      https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#match

      match <test value>:
      	<pattern(s)>:
      		<block>
      	<pattern(s)> when <pattern guard>:
      		<block>
      	<...>