#28858: Remove 'else' after 'return' or 'raise'
-------------------------------------+-------------------------------------
     Reporter:  Дилян Палаузов       |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Core (Other)         |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Adam (Chainz) Johnson):

 So I'm very slightly in favour of removing the useless 'elses', I also
 consider it a tiny bit more readable, and amazingly cpython *does*
 generate less code in this case (it's no faster though, it's just removing
 an extra two opcodes from a redundant return None):

 {{{
 In [6]: def foo():
    ...:     if foo:
    ...:         return 1
    ...:     else:
    ...:         return 2
    ...:

 In [7]: def bar():
    ...:     if bar:
    ...:         return 1
    ...:     return 2
    ...:

 In [8]: dis.dis(foo)
   2           0 LOAD_GLOBAL              0 (foo)
               2 POP_JUMP_IF_FALSE        8

   3           4 LOAD_CONST               1 (1)
               6 RETURN_VALUE

   5     >>    8 LOAD_CONST               2 (2)
              10 RETURN_VALUE
              12 LOAD_CONST               0 (None)
              14 RETURN_VALUE

 In [9]: dis.dis(bar)
   2           0 LOAD_GLOBAL              0 (bar)
               2 POP_JUMP_IF_FALSE        8

   3           4 LOAD_CONST               1 (1)
               6 RETURN_VALUE

   4     >>    8 LOAD_CONST               2 (2)
              10 RETURN_VALUE
 }}}

 However I doubt it's worth anyone's time to go through Django removing
 them. Perhaps we can add it to the style guide now, close this ticket, and
 fix it as code gets modified in the future.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28858#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.b1db93c286eeef04c06332632a934bb9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to