Re: [PATCHv5 03/17] gitweb/lib - Very simple file based cache

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you very much for those comments on code.

On Thu, 7 Oct 2010, Thomas Adam wrote:
> On 6 October 2010 23:01, Jakub Narebski <jnareb@xxxxxxxxx> wrote:

> > +# creates get_depth() and set_depth($depth) etc. methods
> > +foreach my $i (qw(depth root namespace)) {
> > + Â Â Â my $field = $i;
> > + Â Â Â no strict 'refs';
> 
> For each item, you'll set "no strict refs"?  This might be better off
> outside the loop.  It's still scoped appropriately inside the
> subroutine.

On the other hand this way scope where "no strict 'refs';" is active
is limited... but I guess having "no strict 'refs';" outside loop would
be better.

> 
> > + Â Â Â my $file = $self->path_to_key($key);
> > + Â Â Â return undef unless (defined $file && -f $file);
> 
> PBP (Perl Best Practises) will tell you that explicitly returning
> undef is discouraged -- "undef" should be reserved for those errors
> you cannot handle, not ones you don't want to.

Well, Perl Best Practices are practices; sometimes there is good reason
to not take them into account (though probably not in this case).

Explicitly returning undef is discouraged because in list context the
returned undef (or rather 1-element list with 'undef' as sole element)
is true-ish.

On the other hand

> > +# $cache->set($key, $data);
> > +#
> > +# Associates $data with $key in the cache, overwriting any existing entry.
> > +# Returns $data.
> > +sub set {
> > +       my ($self, $key, $data) = @_;
> > +
> > +       return unless (defined $key && defined $data);
> 
> return what?
 
as you can see 'return' statement with no value looks rather cryptic
with statement modifier (conditional).

I should really have run gitweb, caching modules and tests through 
perlcritic...

> > + Â Â Â Â Â Â Â last if $read_cnt == 0;
> > + Â Â Â Â Â Â Â $size_left -= $read_cnt;
> > + Â Â Â Â Â Â Â #last if $size_left <= 0;
> > + Â Â Â }
> > +
> > + Â Â Â close $read_fh
> > + Â Â Â Â Â Â Â or die "Couldn't close file '$file' opened for reading: $!";
> > + Â Â Â return $buf;
> > +}
> 
> "use Carp;" would be more useful here, and hence croak() and confess().

For a web application we usually do not want to have too detailed error
message present to client (to web browser) to avoid leaking of sensitive
information.
 
> > + Â Â Â # ensure that directory leading to cache file exists
> > + Â Â Â if (!-d $dir) {
> > + Â Â Â Â Â Â Â eval { mkpath($dir, 0, 0777); 1 }
> > + Â Â Â Â Â Â Â Â Â Â Â or die "Couldn't create leading directory '$dir' (mkpath): $!";
> > + Â Â Â }
> 
> Why is this eval()ed?  It will still return false and set $! appropriately.

IIRC mkpath *dies on error*, rather than returning false.  For better
error handling we would need to use make_path, but File::Path 2.0+
is in [stable] core only since Perl 5.10.
 
[...]
> > +# $data = $cache->compute($key, $code);
> > +#
> > +# Combines the get and set operations in a single call. ÂAttempts to
> > +# get $key; if successful, returns the value. ÂOtherwise, calls $code
> > +# and uses the return value as the new value for $key, which is then
> > +# returned.
> > +sub compute {
> > + Â Â Â my ($self, $key, $code) = @_;
> > +
> > + Â Â Â my $data = $self->get($key);
> > + Â Â Â if (!defined $data) {
> > + Â Â Â Â Â Â Â $data = $code->($self, $key);
> > + Â Â Â Â Â Â Â $self->set($key, $data);
> > + Â Â Â }
> 
> Can you guarantee $code here?

I don't want to code too defensively, but perhaps check for this is in
order... though what we should do if $code is not code reference?

> 
> unless( defined $code and ref $code eq 'CODE' )

"ref($code) eq 'CODE'" would be enough; 'undef' is not reference, and
ref(undef) returns "".

> {
>         ....
> }
> 
> Wouldn't it be easier to eval{} this and check $@?

So the answer is no.


Thanks again for your comments.
-- 
Jakub Narebski
Poland
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]