130 points · by lizmat · 6y ago · 110 comments
This is an excellent point, and one that not enough programmers pay attention to (well, outside of the APL family of languages, anyway!)
}
} // not
} // a single
} // fucking else
// implicit else
// return nullptr
return result;
}
A deeply-indented scope should mean "shit is complicated, think deeply about it," not "there were five times I tried something and elected to punt and return null if it failed." The LLVM style guide has the right idea; if you're gonna punt, early-return.As a side-note, I really wish the C++ community would take a page out of Lisp's book and stop giving closing braces their own lines. But that'll never happen.
I don't know what it is but I once interviewed for an embedded C position. During the interview, I wrote out some code samples. The only issue the interviewer found was that I had "returned early". He said there could basically only be one return in a function and it had to be at the end. I told him I'd never heard of that. He only said it was a "standard" that had to be followed because of the crypo involved in the code base.
Sadly, even though I didn't get the job, that stupid statement of only one return call in a function has stuck with me and lead to many mental arguments.
There are two main advantages:
- it's obvious when the function ends, no hidden early exits in the middle of nowhere
- if you need to do something before returning, there is only one place to do it (e.g. trim the output, e.g. log performances), and it make it easier to move this kind of last mile logic to a different function (if it's of any interest)
About your interview, don't sweat it, is not for that you didn't pass (you could do a great job and yet lose to a more fit candidate)
Before:
if (a.works()) {
// some code
if (b.works()) {
// more code
}
}
After: if (!a.works()) {
// error handling code, if necessary
return;
}
// some code
if (!b.works()) {
// error handling code, if necessary
return;
}
// more codeBail early and you wont have spaghetti code, plus you wont even allocate things to memory you're not even going to use, cause you're going to bail.
> if you're gonna punt, early-return
Oh for the love of God, please don't! That would make things even more unreadable.
Also, the closing braces are often accompanied by error handling and cleaning up code.
You might like the golang approach better, which is really the Linux kernel approach when I think about it....
More generally, you can specify KEEP and UNDO phasers in a block, that will fire whether the block is left successfully or not. So a typical usage would be `UNDO $dbh.rollback`.
Or are you saying that there are some lines of code at the end checking the post conditions? I'm not exactly sure what that would look like, it sounds a bit insane, especially if you do it for every function. Could you give an example?
(all I can think of is something where you could handle it with a finally-type clause)
maybe a fn shouldn't have post conditions?
(Aside from the attraction in small and simple code, each screen in forth is traditionally allocated a designated spot on the hard-drive so it's partially also for space and HDD usage efficiency too)
I haven't seen many developers do this: get a computer screen that can stand in the portrait position.
My setup currently is: laptop monitor + 27" screen in portrait. I never have the trouble of scrolling back up and down again when it comes to reading code.
This is also why I'm not as enthusiastic about CPAN as I could be. It's great that the modules are available for when you want to write real applications, but that's not what I do with Perl and one-off scripts really don't seem like it's worth pulling in external dependencies for.
Like an ELI5 for someone not into neither Perl or Raku and haven’t met the units library before.
Mainly because you can add things to the existing parser instead. (See postfix and infix below.)
role Physics::Datum {
has Real $.value is required;
}
role Physics::Distance {
also does Physics::Datum;
}
role Physics::Volume {
also does Physics::Datum;
}
class Physics::Volume::Gallon {
also does Physics::Volume;
}
class Physics::Distance::Mile {
also does Physics::Distance;
}
role Physics::Per[ ::N-Type, ::D-Type ] {
has N-Type $.nu is required;
has D-Type $.de is required;
# could have a submethod TWEAK to reduce the fraction
}
constant gallon = Physics::Volume::Gallon.new( value => 1 );
sub postfix:<miles> ( Real $value ){
Physics::Distance::Meter.new( :$value )
}
sub infix:<per> ( ::NT Physics::Datum $nu, ::DT Physics::Datum $de ){
Physics::Per[NT,DT].new( :$nu, :$de )
}
my $mpg = 5miles per gallon;
Which would result in the following data structure. Physics::Per[ Physics::Distance::Mile, Physics::Volume::Gallon ].new(
nu => Physics::Distance::Mile.new( value => 5 ),
de => Physics::Volume::Gallon.new( value => 1 ),
)
I think the main reason it was written that way was it was copied from Perl.
A lot of the above would be more difficult or slower in Perl.(This was mostly a quick throwaway example of how it could be done.)
weight = unit("10 kg")
speed = unit("2.5 furlong per week") Unit(float, unit_as_string)
The free text version is pretty cool thoughFor example,
import week, custom, si, type from stdlib.units
weight = 10 * si.kg
speed = 2.5 * (custom("furlong", type.LENGTH) / week)http://blogs.perl.org/users/ovid/2019/08/is-perl-6-being-ren...
http://blogs.perl.org/users/ovid/2019/10/larry-has-approved-...
So both of these lines would be equivalent.
use v5.32;
use Perl v2020;I think that would only add to the confusion, since Perl 6 was already a thing, albeit totally different from Perl 5
Raku has some pretty amazing things going for it though. It is unique compared to any programming language with its grammars and such.
What made the author write this in the first place? He could use one of the many many parsing libraries, and then at least the comparison with Raku grammars would be honest and fair.
The dependency could break compatibility or add a bug a year from now.
That's what https://metacpan.org/pod/Regexp::Grammars is for, indeed.
EDIT: Oh, that's in the Perl (5) code. Nevermind then.
In C-like it kind of makes sense – it's 1 character shorter, and on some ancient crappy compilers it may actually run faster than while(1) since it doesn't have to check the condition every time ;)
Try reading some of this: https://github.com/Raku/problem-solving/issues/81
In the end it felt better to stop wasting all those emocycles and suggest a rename.
What is in question was it worth making the already extremely messy name situation even more confusing?
Raku, Rakudo, Rakudo Star, it's like they went out of their way to make it confusing. Which, given the Perl culture of whimsy, isn't unlikely.
Not at all.
Haskell, GHC, hackage/cabal/whatever -- intentionally confusing? No, they are different things.
Same in the Raku case: Raku is the language, Rakudo a compiler, and Rakudo Star a distribution (with compiler, docs, module installer etc.)
Giving three different things the same name would be much more confusing.
Working in the perl sphere, I assure you clients do not know this. Also http://blogs.perl.org/users/ovid/2019/08/is-perl-6-being-ren...
Everyone who have been following the evolution of the language the last decade. But most people have not, and new people are born every day. So the name change will avoid confusion going forward and make it easier for both the Perl 5 and Raku communities.
Now after the rename, I guess every HN discussion will have a subthread discussing how the rename was terrible, and it should've stayed Perl 6.
There's just no pleasing the Internet crowd.
This can also come up in political contexts when some of your adversaries say something, but you don’t track which of them said it, and instead assign the opinion to [democrats, republicans, libertarians] as a whole. Next thing you know you think they’re all dirty hypocrites arguing in bad faith, and it takes an effort to see otherwise.
I don't see any evidence for this claim. Besides those on the inside, I have yet to see any support for naming a different language as logical next version of another.