Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: I love assert Date: Thu, 13 Nov 2014 01:23:08 +0200 Organization: A noiseless patient Spider Lines: 30 Message-ID: <878ujgovfn.fsf@elektro.pacujo.net> References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> <87a93wqeq3.fsf@elektro.pacujo.net> <87y4rgozgd.fsf@elektro.pacujo.net> <0cdc67f4-d9d5-4a73-91fe-7558161246af@googlegroups.com> <28e159ff-3d64-4793-9082-05cf1553c7c2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="20480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oaFK0oGnz8go7JCKlpFlH" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:3fawSfoucUUfa1enrDwl2iFSgW4= sha1:kSvQ9tTda00x95cb6iTzyGsp5yY= Xref: csiph.com comp.lang.python:80659 Chris Angelico : > Or, in as many cases as possible, raise an implicit KeyError and save > yourself a lot of typing. Any time these kinds of elif trees can be > turned into dict lookups, you get non-assert error checking for free. I agree that you shouldn't sprinkle asserts around the code. But consider this example: count = next_size / len(choices) The casual reader of the code might be left wondering if "choices" could be zero-length and if the programmer had overlooked the possibility, leading to a lengthy analysis and bewilderment. However, the programmer could have played this frustration out already in his head and written: assert len(choices) > 0 count = next_size / len(choices) or, equivalently: # here len(choices) > 0 count = next_size / len(choices) and the casual maintainer would be able to read on. Marko