git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/3] send-email: use "return;" not "return undef;" on error codepaths

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 2, 2013, 14:49 UTC
Message-ID
<7vsj39gf7k.fsf@alter.siamese.dyndns.org>
In-Reply-To
<CALkWK0muF2Nx=ykD6jt4pvpmp9c3R2KhXhQMjLAyMWiHu_6iPA@mail.gmail.com>
Ramkumar Ramachandra <artagnon@gmail.com> writes:
Show 13 quoted lines
> Junio C Hamano wrote:
>> Note that we leave "return undef;" in validate_address on purpose,
>> even though Perlcritic may complain.  The primary "return" site of
>> the function returns whatever is in the scaler variable $address, so
>> it is pointless to change only the other "return undef;" to "return".
>> The caller must be prepared to see an array with a single undef as
>> the return value from this subroutine anyway.
>
> The way I understood it:
> All callsites only ever call validate_address via
> validate_address_list, which in turn maps a list of addresses by
> calling validate_address on each address.  Therefore, validate_address
> returning an undef in one codepath is correct.
I think we are in agreement then.

The implication of what you just said is that anybody that calls this subroutine _must_ be (and indeed is) prepared to see a single 'undef' returned from it, hence the use pattern,

	@foo = validate_address(...);
        if (@foo) { ... we got a valid one ... } else { ... we did not ... }

cannot be used for the subroutine _anyway_. @foo may get an invalid address, even with the "don't say 'return undef;' say 'return;'" wisdom is applied to the other return site in the subroutine.

Now, the subroutine's body essentially is:
	while (!extract_valid($address)) {
		...
                if (...) {
                	return undef; # or just "return;"
		}
	}
	return $address;

You can prove that the "return $address" on the last line will never return 'undef' by proving that extract_valid() never returns true for 'undef' input. With that, we can safely change the error return to do "return;" in the loop.

When it is done, the subroutine's new interface becomes "Single address goes in, either a single valid address comes out and that will never be an undef, or nothing (not even an undef) comes out upon error".

Which means the sole caller of the subroutine needs to be updated, which currently does this:

	return grep { defined $_ } map { validate_address($_) } @_;

As the subroutine never returns an undef as "no this is not valid", but acts more like a filter to cull bad ones from the input, the outer grep { defined $_ } becomes unnecessary.

And I think that change logically belongs to the same patch as the one that inspects how validate_address behaves and updates its "sometimes we return undef to signal a bad input" to return nothing.

That is why validate_adress was excluded from [1/3] which deals with other simpler cases.

Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 20 of 21 in “git-send-email.perl: implement suggestions made by perlcritic”
  1. git-send-email.perl: implement suggestions made by perlcriticRamkumar Ramachandra, Mar 21, 2013
  2. Junio C HamanoMar 21, 2013
  3. Ramkumar RamachandraMar 21, 2013
  4. Ramkumar RamachandraMar 27, 2013
  5. Junio C HamanoMar 27, 2013
  6. git-send-email.perl: implement suggestions made by perlcriticRamkumar Ramachandra, Mar 28, 2013
  7. Jonathan NiederMar 28, 2013
  8. Junio C HamanoMar 28, 2013
  9. Ramkumar RamachandraMar 31, 2013
  10. Junio C HamanoApr 1, 2013
  11. 1/3 send-email: use "return;" not "return undef;" on error codepathsJunio C Hamano, Apr 1, 2013
  12. 2/3 send-email: drop misleading function prototypeJunio C Hamano, Apr 1, 2013
  13. Jonathan NiederApr 1, 2013
  14. Eric SunshineApr 1, 2013
  15. 3/3 send-email: use the three-arg form of open in recipients_cmdJunio C Hamano, Apr 1, 2013
  16. Jonathan NiederApr 1, 2013
  17. Ramkumar RamachandraApr 2, 2013
  18. Eric SunshineApr 1, 2013
  19. Ramkumar RamachandraApr 2, 2013
  20. Junio C HamanoApr 2, 2013
  21. Ramkumar RamachandraApr 2, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.