How to make this simple code look better

Ben Finney ben+python at benfinney.id.au
Tue Oct 27 21:15:08 EDT 2015


Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:

> 	Actually, if 0 is success, and you are testing for a failure, it
> should probably just be
>
> 	if ret:
> 		#do error

That obscures the intent; there is nothing about “is ‘ret’ false?” that
tells me what the meaning of that test is.

It happens to be the case that “success” is a false-y value, but that's
not clear from reading a simple “if ret” test.

Instead, I advise making it clear *why* we're checking zero in
particular: because in this case zero means success. That's not obvious,
and should be explicit.

    EXIT_CODE_SUCCESS = 0

    # …

    (stdout_content, stderr_content) = proc.communicate()
    if proc.returncode != EXIT_CODE_SUCCESS:
        handle_the_failure(proc)

-- 
 \      “The process by which banks create money is so simple that the |
  `\     mind is repelled.” —John Kenneth Galbraith, _Money: Whence It |
_o__)                                       Came, Where It Went_, 1975 |
Ben Finney




More information about the Python-list mailing list