Re: if statement confusion by Anonymous Monk on Jun 11, 2012 at 14:13 UTC |
| [reply] [d/l] |
Re: if statement confusion by toolic (Chancellor) on Jun 11, 2012 at 14:15 UTC |
perl -MO=Deparse -e '6 < 8 ? $ans = "true" : $ans = "false";'
($ans = 'true') = 'false';
Try:
$ans = 6 < 8 ? "true" : "false";
Conditional Operator | [reply] [d/l] [select] |
Re: if statement confusion by Anonymous Monk on Jun 11, 2012 at 14:56 UTC |
| [reply] |
Re: if statement confusion by davido (Bishop) on Jun 11, 2012 at 17:31 UTC |
$ans = 6 < 8 ? 'true' : 'false';
update: Aack! I didn't notice that toolic had already given this suggestion. Sorry for creating an echo in the Monastery. :)
| [reply] [d/l] |
Re: if statement confusion by sundialsvc4 (Monsignor) on Jun 11, 2012 at 22:05 UTC |
I’m afraid that I would come fairly unglued if I saw such an “economical” attempt to write an if statement in the fewest number of characters. The difference makes absolutely no difference at all to the Perl compiler, but it has the very human difference of being utterly un-maintainable. To the point that, of course, it perhaps-compiled but did not produce the correct outcome. At this point in time, digital computers now being so blazingly fast as they are, we don’t need to economize on such things ... but we do need to be able to consistently write easily maintainable code that can be correctly understood at first glance. Mistakes in these areas can quite literally cost us a fortune.
| [reply] |
|
| [reply] |
|
Honestly, sir, you sometimes pick an argument just for the sake of an argument. The need to “write easily maintainable code that can correctly be understood at first glance” truly is self-evident ... to the point that nothing further need be said.
| [reply] |
|
|
You make a very valid point, but I think you carry the logic too far.
Yes, I'd always want an expert to be the one handling my contracts, medical diagnosis, etc. But does that mean that they should be made as complex as possible so that ONLY an expert can understand it?
I'm sure that there are a lot of very good coders out there who can look at a bit of compact code they wrote 2 years ago and see it's purpose and complexities at a glance, however most of us humble mortals need time to figure these things out ... and for us clearly written easily maintainable code is far more important than shaving a femosecond off a CPU operation.
Hats off to you gurus ... we all want to end up like you ... but we're not there yet, so please don't discourage us by insisting that only compact, complex code is acceptable.
| [reply] |
|
|
|
|
|
$ans = something ? 'true' : 'false' is the equivalent not of the deeds for a house, but of a single clause, or a single sentence. Each sentence in the deeds should be easy to understand.
| [reply] [d/l] |
|
|
I think you are creating a problem where none exists. The point does not seem to be about whether a reader merely glances at something and proclaims understanding; rather, it is about whether the writer strives to make things clear to the reader. Thus, it is not about a lawyer who merely glances at a contract then asks a client to sign on the dotted line, but rather about someone writing a contract to be clear rather than to be opaque and full of gotchas.
Would you trust a lawyer who wrote contracts as though paid by the letter rather than as though paid for agreeable results? Would you, likewise, trust a lawyer who argued your case in court as though paid by how long he could make your trial last rather than as though he would only be paid if you were acquitted of all charges? What about a doctor who was paid based on how many different pharmaceutical companies' products he injected into you, rather than based on positive benefit to your health?
I prefer a programmer who writes clear code over a programmer who either plays golf with production code or bases his or her self-worth as a programmer on the number of clever tricks crammed into a simple function.
| print substr("Just another Perl hacker", 0, -2); |
|
- apotheon
Licensed OWL by Chad Perrin |
| [reply] |
|
|
|
|
This is perfectly legitimate and readable code to me, and I use this technique frequently. In fact, the less typing a coder does, reduces the chances for bugs.
Just out of curiosity, how would *you* code this statement?
| [reply] |
|
I’m afraid that I would come fairly unglued if I saw such an “economical” attempt to write an if statement in the fewest number of characters. The difference makes absolutely no difference at all to the Perl compiler, but it has the very human difference of being utterly un-maintainable.
Lets discuss your claims.
use strict;
use warnings;
use Data::Dumper;
use B::Concise;
sub mysub {
my $val;
$val = rand(2) > 1 ? "high" : "low";
return $val;
}
sub mysub2 {
my $val;
if(rand(2) > 1){
$val = "high";
}
else{
$val = "low";
}
return $val;
}
my $walker = B::Concise::compile('-src','-exec',*mysub{CODE});
print "now mysub\n";
$walker->();
$walker = B::Concise::compile('-src','-exec',*mysub2{CODE});
print "\n\n\n\nnow mysub2\n";
$walker->();
exit;
C:\Documents and Settings\Owner\Desktop>perl n13.pl
now mysub
B::Concise::compile(CODE(0x82a6dc))
# 9: my $val;
1 <;> nextstate(main -8 n13.pl:9) v:*,&,$
2 <0> padsv[$val:-8,-7] vM/LVINTRO
# 10: $val = rand(2) > 1 ? "high" : "low";
3 <;> nextstate(main -7 n13.pl:10) v:*,&,$
4 <$> const[IV 2] s
5 <1> rand[t2] sK/1
6 <$> const[IV 1] s
7 <2> gt sK/2
8 <|> cond_expr(other->9) sK/1
9 <$> const[PV "high"] s
goto a
h <$> const[PV "low"] s
a <0> padsv[$val:-8,-7] sRM*
b <2> sassign vKS/2
# 11: return $val;
c <;> nextstate(main -7 n13.pl:11) v:*,&,{,$
d <0> pushmark s
e <0> padsv[$val:-8,-7]
f <@> return K
g <1> leavesub[1 ref] K/REFC,1
now mysub2
B::Concise::compile(CODE(0x9a1c44))
# 14: my $val;
i <;> nextstate(main -6 n13.pl:14) v:*,&,$
j <0> padsv[$val:-6,-2] vM/LVINTRO
# 15: if(rand(2) > 1){
k <;> nextstate(main -2 n13.pl:15) v:*,&,$
l <$> const[IV 2] s
m <1> rand[t2] sK/1
n <$> const[IV 1] s
o <2> gt sK/2
p <|> cond_expr(other->q) vK/1
q <$> const[PV "high"] s
r <0> padsv[$val:-6,-2] sRM*
s <2> sassign vKS/2
goto t
y <0> enter v
# 19: $val = "low";
z <;> nextstate(main -4 n13.pl:19) v:*,&,$
10 <$> const[PV "low"] s
11 <0> padsv[$val:-6,-2] sRM*
12 <2> sassign vKS/2
13 <@> leave vKP
# 21: return $val;
t <;> nextstate(main -2 n13.pl:21) v:*,&,{,$
u <0> pushmark s
v <0> padsv[$val:-6,-2]
w <@> return K
x <1> leavesub[1 ref] K/REFC,1
C:\Documents and Settings\Owner\Desktop>
Using conditional operator, the sub 17 ops long. Using if else blocks, the sub was 22 ops long. Have a nice day. | [reply] [d/l] [select] |
|
I'm afraid that I would come fairly unglued
if I saw such an "economical" attempt to
write an if statement in the fewest
number of characters.
The difference makes absolutely no difference
at all to the Perl compiler, but it has the
very human difference of being utterly un-maintainable.
Ironically, the original code:
6 < 8 ? $ans = "true" : $ans = "false";
is "un-maintainable" not because it is too short but
because it is not short enough! Writing it like this:
$ans = 6 < 8 ? "true" : "false";
is perfectly clear
and, by eliminating the (error-prone) repeated use of $ans,
maintainability is improved.
Since you suggested an if statement, I certainly hope
you're not suggesting replacing it with:
if (6 < 8) {
$ans = "true";
}
else {
$ans = "false";
}
This version has the same dubious violation of
of DRY
as the original, and so is less "maintainable"
than the improved simple ternary version IMHO.
| [reply] [d/l] [select] |
Re: if statement confusion by eyepopslikeamosquito (Canon) on Jun 12, 2012 at 12:28 UTC |
| [reply] |
Reaped: Re: if statement confusion by NodeReaper (Curate) on Jun 12, 2012 at 12:52 UTC |
| [reply] |