git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
@ 2017-11-21 16:09 Christian Couder
  2017-11-21 16:09 ` [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless' Christian Couder
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Christian Couder @ 2017-11-21 16:09 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

The function calls itself "required", but it does not die when it
sees an unexpected EOF.
Let's rename it to "packet_key_val_read()".

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---

These 2 patches are a late follow up from:

https://public-inbox.org/git/CAP8UFD2vk4jV7jEBx3Axd-dhfcsGSJVFFt+pumdT1j8GD_oM_w@mail.gmail.com/

 perl/Git/Packet.pm      | 5 +++--
 t/t0021/rot13-filter.pl | 4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/perl/Git/Packet.pm b/perl/Git/Packet.pm
index 255b28c098..82da0cf0db 100644
--- a/perl/Git/Packet.pm
+++ b/perl/Git/Packet.pm
@@ -17,7 +17,7 @@ our @EXPORT = qw(
 			packet_compare_lists
 			packet_bin_read
 			packet_txt_read
-			packet_required_key_val_read
+			packet_key_val_read
 			packet_bin_write
 			packet_txt_write
 			packet_flush
@@ -83,7 +83,8 @@ sub packet_txt_read {
 	return ( $res, $buf );
 }
 
-sub packet_required_key_val_read {
+# Read a text line and check that it is in the form "key=value"
+sub packet_key_val_read {
 	my ( $key ) = @_;
 	my ( $res, $buf ) = packet_txt_read();
 	unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
diff --git a/t/t0021/rot13-filter.pl b/t/t0021/rot13-filter.pl
index 6fd7fa476b..f1678851de 100644
--- a/t/t0021/rot13-filter.pl
+++ b/t/t0021/rot13-filter.pl
@@ -70,7 +70,7 @@ print $debug "init handshake complete\n";
 $debug->flush();
 
 while (1) {
-	my ( $res, $command ) = packet_required_key_val_read("command");
+	my ( $res, $command ) = packet_key_val_read("command");
 	if ( $res == -1 ) {
 		print $debug "STOP\n";
 		exit();
@@ -106,7 +106,7 @@ while (1) {
 		packet_txt_write("status=success");
 		packet_flush();
 	} else {
-		my ( $res, $pathname ) = packet_required_key_val_read("pathname");
+		my ( $res, $pathname ) = packet_key_val_read("pathname");
 		if ( $res == -1 ) {
 			die "unexpected EOF while expecting pathname";
 		}
-- 
2.15.0.318.g4f69657937


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless'
  2017-11-21 16:09 [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Christian Couder
@ 2017-11-21 16:09 ` Christian Couder
  2017-11-21 19:24   ` Jonathan Nieder
  2017-11-21 19:19 ` [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Jonathan Nieder
  2017-11-22  3:48 ` Junio C Hamano
  2 siblings, 1 reply; 11+ messages in thread
From: Christian Couder @ 2017-11-21 16:09 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

The code is more understandable with 'if' instead of 'unless'.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 perl/Git/Packet.pm | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/perl/Git/Packet.pm b/perl/Git/Packet.pm
index 82da0cf0db..1682403ffc 100644
--- a/perl/Git/Packet.pm
+++ b/perl/Git/Packet.pm
@@ -68,16 +68,16 @@ sub packet_bin_read {
 
 sub remove_final_lf_or_die {
 	my $buf = shift;
-	unless ( $buf =~ s/\n$// ) {
-		die "A non-binary line MUST be terminated by an LF.\n"
-		    . "Received: '$buf'";
+	if ( $buf =~ s/\n$// ) {
+		return $buf;
 	}
-	return $buf;
+	die "A non-binary line MUST be terminated by an LF.\n"
+	    . "Received: '$buf'";
 }
 
 sub packet_txt_read {
 	my ( $res, $buf ) = packet_bin_read();
-	unless ( $res == -1 or $buf eq '' ) {
+	if ( $res != -1 and $buf ne '' ) {
 		$buf = remove_final_lf_or_die($buf);
 	}
 	return ( $res, $buf );
@@ -87,10 +87,10 @@ sub packet_txt_read {
 sub packet_key_val_read {
 	my ( $key ) = @_;
 	my ( $res, $buf ) = packet_txt_read();
-	unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
-		die "bad $key: '$buf'";
+	if ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
+		return ( $res, $buf );
 	}
-	return ( $res, $buf );
+	die "bad $key: '$buf'";
 }
 
 sub packet_bin_write {
-- 
2.15.0.318.g4f69657937


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-21 16:09 [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Christian Couder
  2017-11-21 16:09 ` [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless' Christian Couder
@ 2017-11-21 19:19 ` Jonathan Nieder
  2017-11-22  3:39   ` Junio C Hamano
  2017-11-22  3:48 ` Junio C Hamano
  2 siblings, 1 reply; 11+ messages in thread
From: Jonathan Nieder @ 2017-11-21 19:19 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Hi,

Christian Couder wrote:

> The function calls itself "required", but it does not die when it
> sees an unexpected EOF.
> Let's rename it to "packet_key_val_read()".
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---

nit: please wrap lines to a consistent width, to make the message
easier to read.  In the above, it looks like the line break is
intentional --- is it meant to be two paragraphs (i.e. is it missing
another newline)?

optional, just noticed while I'm nitpicking: the description 'rename
packet_required_key_val_read' doesn't tell why the function is being
renamed.  Maybe something like

	Git::Packet: clarify that packet_required_key_val_read allows EOF

would do the trick.

[...]
> +++ b/perl/Git/Packet.pm
> @@ -83,7 +83,8 @@ sub packet_txt_read {
>  	return ( $res, $buf );
>  }
>  
> -sub packet_required_key_val_read {
> +# Read a text line and check that it is in the form "key=value"
> +sub packet_key_val_read {

This comment doesn't tell me how to use the function.  How do I detect
whether it successfully read a line?  What do the return values
represent?  What happens if the line it read doesn't match the key?

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless'
  2017-11-21 16:09 ` [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless' Christian Couder
@ 2017-11-21 19:24   ` Jonathan Nieder
  2017-11-22  3:48     ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Nieder @ 2017-11-21 19:24 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Hi,

Christian Couder wrote:

> The code is more understandable with 'if' instead of 'unless'.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  perl/Git/Packet.pm | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)

I'm agnostic about that.  In some ways it seems like an odd change,
since it is changing from

	if (exceptional case) {
		die "explanation";
	}
	common case;

to

	if (!exceptional case) {
		common case;
	}
	die "explanation";

when we usually prefer the former (getting the exceptional case over
with early so the reader can concentrate on the common case).  In
perl, it might be even more idiomatic to do

	die "explanation" if exceptional case;
	common case;

but Documentation/CodingGuidelines appears to recommend not going that
far. :)

[...]
> --- a/perl/Git/Packet.pm
> +++ b/perl/Git/Packet.pm
> @@ -68,16 +68,16 @@ sub packet_bin_read {
>  
>  sub remove_final_lf_or_die {
>  	my $buf = shift;
> -	unless ( $buf =~ s/\n$// ) {

For readability, I find this whitespace within parens more distracting.

Documentation/CodingGuidelines covers that for C programs but not for
Perl.  Do we have a preferred style either way about it?

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-21 19:19 ` [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Jonathan Nieder
@ 2017-11-22  3:39   ` Junio C Hamano
  2017-11-22  5:10     ` Jonathan Nieder
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2017-11-22  3:39 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: Christian Couder, git, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Jonathan Nieder <jrnieder@gmail.com> writes:

> nit: please wrap lines to a consistent width, to make the message
> easier to read.  In the above, it looks like the line break is
> intentional --- is it meant to be two paragraphs (i.e. is it missing
> another newline)?

I'd think so; will add a missing LF while queuing..

> optional, just noticed while I'm nitpicking: the description 'rename
> packet_required_key_val_read' doesn't tell why the function is being
> renamed.  Maybe something like
>
> 	Git::Packet: clarify that packet_required_key_val_read allows EOF
>
> would do the trick.

Sounds good. 

>> +# Read a text line and check that it is in the form "key=value"
>> +sub packet_key_val_read {
>
> This comment doesn't tell me how to use the function.  How do I detect
> whether it successfully read a line?  What do the return values
> represent?  What happens if the line it read doesn't match the key?

Would this work for both of you?

# Read a text packet, expecting that it is in the form "key=value" for
# the given $key.  An EOF does not trigger any error and is reported
# back to the caller (like packet_txt_read() does).  Die if the "key"
# part of "key=value" does not match the given $key, or the value part
# is empty.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless'
  2017-11-21 19:24   ` Jonathan Nieder
@ 2017-11-22  3:48     ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2017-11-22  3:48 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: Christian Couder, git, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Jonathan Nieder <jrnieder@gmail.com> writes:

> [...]
>> --- a/perl/Git/Packet.pm
>> +++ b/perl/Git/Packet.pm
>> @@ -68,16 +68,16 @@ sub packet_bin_read {
>>  
>>  sub remove_final_lf_or_die {
>>  	my $buf = shift;
>> -	unless ( $buf =~ s/\n$// ) {
>
> For readability, I find this whitespace within parens more distracting.

I personally find them distracting, too.  This file seems full of
them so it is OK to be consistent with the existing practice in this
step, leaving the eventual clean-up (if we agree that it is a good
idea) to a later step that does it for the entire file contents.

Thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-21 16:09 [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Christian Couder
  2017-11-21 16:09 ` [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless' Christian Couder
  2017-11-21 19:19 ` [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Jonathan Nieder
@ 2017-11-22  3:48 ` Junio C Hamano
  2 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2017-11-22  3:48 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ben Peart, Jonathan Tan, Nguyen Thai Ngoc Duy,
	Mike Hommey, Lars Schneider, Eric Wong, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> The function calls itself "required", but it does not die when it
> sees an unexpected EOF.
> Let's rename it to "packet_key_val_read()".
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>
> These 2 patches are a late follow up from:
>
> https://public-inbox.org/git/CAP8UFD2vk4jV7jEBx3Axd-dhfcsGSJVFFt+pumdT1j8GD_oM_w@mail.gmail.com/

Thanks for tying the loose end.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-22  3:39   ` Junio C Hamano
@ 2017-11-22  5:10     ` Jonathan Nieder
  2017-11-22  6:28       ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Nieder @ 2017-11-22  5:10 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Christian Couder, git, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Junio C Hamano wrote:
> Jonathan Nieder <jrnieder@gmail.com> writes:
>> Christian Couder wrote:

>>> +# Read a text line and check that it is in the form "key=value"
>>> +sub packet_key_val_read {
>>
>> This comment doesn't tell me how to use the function.  How do I detect
>> whether it successfully read a line?  What do the return values
>> represent?  What happens if the line it read doesn't match the key?
>
> Would this work for both of you?
>
> # Read a text packet, expecting that it is in the form "key=value" for
> # the given $key.  An EOF does not trigger any error and is reported
> # back to the caller (like packet_txt_read() does).  Die if the "key"
> # part of "key=value" does not match the given $key, or the value part
> # is empty.

Yes, thank you.

Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-22  5:10     ` Jonathan Nieder
@ 2017-11-22  6:28       ` Junio C Hamano
  2017-11-22  7:07         ` Jonathan Nieder
  2017-11-22  7:14         ` Christian Couder
  0 siblings, 2 replies; 11+ messages in thread
From: Junio C Hamano @ 2017-11-22  6:28 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: Christian Couder, git, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Jonathan Nieder <jrnieder@gmail.com> writes:

>>> This comment doesn't tell me how to use the function.  How do I detect
>>> whether it successfully read a line?  What do the return values
>>> represent?  What happens if the line it read doesn't match the key?
>>
>> Would this work for both of you?
>>
>> # Read a text packet, expecting that it is in the form "key=value" for
>> # the given $key.  An EOF does not trigger any error and is reported
>> # back to the caller (like packet_txt_read() does).  Die if the "key"
>> # part of "key=value" does not match the given $key, or the value part
>> # is empty.
>
> Yes, thank you.

Heh.  I actually was expecting a different response: "that describes
what the reader can easily read out of the implementation and is
useless", though.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-22  6:28       ` Junio C Hamano
@ 2017-11-22  7:07         ` Jonathan Nieder
  2017-11-22  7:14         ` Christian Couder
  1 sibling, 0 replies; 11+ messages in thread
From: Jonathan Nieder @ 2017-11-22  7:07 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Christian Couder, git, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

Junio C Hamano wrote:
> Jonathan Nieder <jrnieder@gmail.com> writes:
>> Junio C Hamano wrote:
>>> Jonathan Nieder <jrnieder@gmail.com> writes:

>>>> This comment doesn't tell me how to use the function.  How do I detect
>>>> whether it successfully read a line?  What do the return values
>>>> represent?  What happens if the line it read doesn't match the key?
>>>
>>> Would this work for both of you?
>>>
>>> # Read a text packet, expecting that it is in the form "key=value" for
>>> # the given $key.  An EOF does not trigger any error and is reported
>>> # back to the caller (like packet_txt_read() does).  Die if the "key"
>>> # part of "key=value" does not match the given $key, or the value part
>>> # is empty.
>>
>> Yes, thank you.
>
> Heh.  I actually was expecting a different response: "that describes
> what the reader can easily read out of the implementation and is
> useless", though.

The main context that I'm missing and that this function comment
doesn't answer is what this function is for.  When would I want to
read a line and exit if it doesn't match "key" but not exit if I hit
EOF?  It seems very strange.

The function comment does successfully capture the strangeness,
though, and that already helps.  When looking at the implementation, I
had a bit of a double-take and wondered what I was missing.  This doc
comment says "you weren't missing anything --- that is actually the
contract that this function intends to fulfill".

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read()
  2017-11-22  6:28       ` Junio C Hamano
  2017-11-22  7:07         ` Jonathan Nieder
@ 2017-11-22  7:14         ` Christian Couder
  1 sibling, 0 replies; 11+ messages in thread
From: Christian Couder @ 2017-11-22  7:14 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Jonathan Nieder, git, Jeff King, Ben Peart, Jonathan Tan,
	Nguyen Thai Ngoc Duy, Mike Hommey, Lars Schneider, Eric Wong,
	Christian Couder

On Wed, Nov 22, 2017 at 7:28 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Jonathan Nieder <jrnieder@gmail.com> writes:
>
>>>> This comment doesn't tell me how to use the function.  How do I detect
>>>> whether it successfully read a line?  What do the return values
>>>> represent?  What happens if the line it read doesn't match the key?
>>>
>>> Would this work for both of you?
>>>
>>> # Read a text packet, expecting that it is in the form "key=value" for
>>> # the given $key.  An EOF does not trigger any error and is reported
>>> # back to the caller (like packet_txt_read() does).  Die if the "key"
>>> # part of "key=value" does not match the given $key, or the value part
>>> # is empty.
>>
>> Yes, thank you.
>
> Heh.  I actually was expecting a different response: "that describes
> what the reader can easily read out of the implementation and is
> useless", though.

I was going to resend without the comment after Jonathan's first
email, but I am ok with either your improved comment or without any
comment. Thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-11-22  7:14 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-21 16:09 [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Christian Couder
2017-11-21 16:09 ` [PATCH 2/2] Git/Packet.pm: use 'if' instead of 'unless' Christian Couder
2017-11-21 19:24   ` Jonathan Nieder
2017-11-22  3:48     ` Junio C Hamano
2017-11-21 19:19 ` [PATCH 1/2] Git/Packet.pm: rename packet_required_key_val_read() Jonathan Nieder
2017-11-22  3:39   ` Junio C Hamano
2017-11-22  5:10     ` Jonathan Nieder
2017-11-22  6:28       ` Junio C Hamano
2017-11-22  7:07         ` Jonathan Nieder
2017-11-22  7:14         ` Christian Couder
2017-11-22  3:48 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).