my $target_type = (defined($item->{target_type}) && $item->{target_type})
? $item->{target_type}
: "";
my $target_id = (defined($item->{target_id}) && $item->{target_id})
? $item->{target_id}
: "";
Why did the developer use && with almost the same conditions?
Apparently he’s the belt and suspenders type.
(defined($x) && $x)is almost equivalent to just$x, becauseundefis false. (The difference is that if$xisundefthen(defined($x) && $x)will be Perl’s special false value, which is defined, but in this case that makes no difference.) Possibly there used to be a different condition (like$x > 0) and thedefinedtest was necessary to avoid a warning about an uninitialized value. Then someone removed that condition and didn’t think to remove the now-unnecessarydefined.Another reason for using
(defined($x) && $x)(as onon15 points out) is to emphasize to future maintenance programmers that$xmight not be defined. Personally, I would use a comment for that, but TIMTOWTDI.