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)  
  • SteleTrovilo@beehaw.org
    link
    fedilink
    arrow-up
    13
    ·
    2 days ago

    The match statement is cleaner.

    With the sequential if statements, it might seem on a casual read that both if statements could pass - you’d catch both warnings and errors. Only by looking at the conditions do you realize that only up-to-1 of them will run.

    With the match statement, it’s very immediately obvious that only one of the commands will run.

  • refalo@programming.dev
    link
    fedilink
    arrow-up
    10
    ·
    2 days ago

    If status is modified in another thread you could have a race condition with method 1 because you’re checking it twice. The two methods are not exactly the same code due to that.

    • MousePotatoDoesStuff@piefed.socialOP
      link
      fedilink
      English
      arrow-up
      4
      ·
      2 days ago

      Not to worry, the variable is local and the function is single-threaded.

      The elif is a good idea, though - although I’ll likely go with a switch statement in the end.

  • darthelmet@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    2 days ago

    Naively: Isn’t the second one slightly better because the first one will always check both variables twice but the second one only checks one of them once then the other variable once when inside?

    • MousePotatoDoesStuff@piefed.socialOP
      link
      fedilink
      English
      arrow-up
      3
      arrow-down
      1
      ·
      21 hours ago

      You’re right, it is slightly better. Not really a concern here since this code runs only once per run for each unit test (and only for debugging, it’s not meant to be in the game release) so it shouldn’t be a performance issue, but still.

  • lbfgs@programming.dev
    link
    fedilink
    arrow-up
    7
    ·
    2 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
      ·
      2 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>
      	<...>
      
  • copygirl@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    5
    ·
    2 days ago

    Are 1 and 2 magic values in this case?
    You could use consts or even use an enum to further improve readability:

    const STATUS_ERROR   := 1
    const STATUS_WARNING := 2
    # or alternatively:
    enum Status { FINE, ERROR, WARNING }
    

    In the latter case, you’d change status to be the new enum type Status.

    edit: Wait, nevermind. I think you’re already using enums in the actual code (not screenshot).

    • Holla@feddit.org
      link
      fedilink
      arrow-up
      1
      ·
      2 days ago

      Imo result is more descriptive than even Java-esque variable names, because it tells you that this is the variable that will actually be returned, rather than just some intermediate representation

        • Holla@feddit.org
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          1 day ago

          Yea, basically. Like, the users shouldn’t have to look at your implementation to find that out what it will return. My example here would be Square.area(). Maybe they would have to look at the method’s docs though to find out what unit the result is in