1

I recently inherited a codebase and have just found this code in it:

[] = method_that_empties_a_queue_and_returns_the_contents() # the method isn't called this in real life, but you get the idea

In my real-life code it seems to be used as a way of throwing away a bunch of stuff, a bit like piping to /dev/null

It seems strange to me that something would be assigned to an empty list. Its valid code (i.e. all the tests are passing) but it just seems...weird. [] returns an empty list, but to assign something to it... I don't get it. Anyone care to comment?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
jamiet
  • 10,501
  • 14
  • 80
  • 159
  • 1
    It seems there are more concise ways to write a test for this; related: https://stackoverflow.com/q/3114252/10197418. If you need a "throw-away variable", you could assign to `_` to clarify the intention of the assignment. – FObersteiner Apr 29 '22 at 08:13
  • @FObersteiner What more concise ways? (I don't see any, at least not in the answers to that other question.) – Kelly Bundy Apr 29 '22 at 08:25
  • @KellyBundy my comment was refering to jonrsharpe's comment - which was removed for whatever reason. IIUC, that code snippet the OP presents is just some line of a codebase, not an explicit test. By concise I mean something should be asserted in an obvious manner; the code shouldn't rely on some clever assignment to work for that. – FObersteiner Apr 29 '22 at 10:12

0 Answers0