|
Guard clauses cause clustering complexity collapse. Claus that's the way they roll!
:rimshot: - followed by deathly silence.
|
|
|
|
|
It's brief and recommends putting guards in their own if statements. It will trigger those who cleave to the shibboleth that a function should only have one return statement.
|
|
|
|
|
I cleave to that shibboleth.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Agreed.
Software Zen: delete this;
|
|
|
|
|
Like PC-Lint and MISRA standards.
GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
Quis custodes custodit?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
A completely stupid idea. It adds a thrown exception that functions solely as a goto .
Software Zen: delete this;
|
|
|
|
|
At least with a GOTO you can get back to where you were - much more difficult with exceptions.
TTFN - Kent
|
|
|
|
|
True, but then I'm a purist. I haven't used a goto statement in over 20 years. I will admit I use break and continue , but they are 'structured' to my way of thinking.
Software Zen: delete this;
|
|
|
|
|
Did I miss something?
An exception is more than a goto , because it can jump outside of the current scope. This is very useful for aborting work that has run into a serious error, when it is appropriate to exit a chain of function calls and recover from a known point well down the stack. That's actually the only thing I use exceptions for. In most cases, such as a bad argument, the function should simply return a failure result instead.
EDIT: It looks like the code could well be overusing exceptions when returning a failure result would be more appropriate, so that's probably what I missed.
|
|
|
|
|
The 'guard' code from the article was this:
fun guard() {
if (!condition1) {
throw Exception1()
}
doThings1()
if (!condition2) {
throw Exception2()
}
doThings2()
if (!condition3) {
throw Exception3()
}
doThings3()
} This call needs to be surrounded by a try...catch in order to exactly reproduce the functionality of the original. I'm not sure which language is used for the original code; I think it may be Javascript, but the fun keyword is odd.
In any case, throwing an exception in lieu of nested conditionals or a goto is overly complicated and error-inducing. Imagine debugging this sort of nonsense when the catch misses one of the exceptions thrown.
Software Zen: delete this;
|
|
|
|
|
It's so sad that other programmers that I may end up working with at some point read that drivel and adopt it.
|
|
|
|
|
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Hi Marc, Perhaps my memory is flawed, but, I seem to recall eyeballing code of yours that used preventive filters in this way.
I'd appreciate your comments on my post below !
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 30-Nov-21 0:13am.
|
|
|
|
|
I believe in anticipating/handling errors in parameter data before you get to the juicy parts, and I think guard clauses are a good example of a strategy for that. I also think liberal use of Try/Catch for things like file-ops is a good thing.
Guard clauses' "virtual isolation" at the start of a method is, imho, "separation of concerns." You can easily comment them out, remove them, as conditions/state management require.
However, there are other strategies.
What I'd like to see is a radical extension of Attributes in C# that do AOP-like transformations, and, internally generate the equivalent of guard clauses: there's already an example of that in the semantics of the Nullable attributes: [^].
A "wedding" of unit-tests and attributes
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
BillWoodruff wrote: What I'd like to see is a radical extension of Attributes in C# You always did strike me as a radical!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The breakthrough compresses light into a nano-scale spot. "Mama's got a squeeze box, Daddy never sleeps at night"
|
|
|
|
|
The Joy of Cryptography is a free undergraduate textbook that introduces students to the fundamentals of provable security. One of these things is not like the other
|
|
|
|
|
"Joy" and "Cryptography" in the same sentence?
|
|
|
|
|
Australia will introduce legislation to make social media giants provide details of users who post defamatory comments, Prime Minister Scott Morrison said on Sunday. This legislation was proposed by three billy goats gruff
|
|
|
|
|
Banning weapons;
Using overwhelming force against unarmed people to enforce government policy;
Checking all communications for unapproved opinions.
Alas, the Soapbox is no more.
GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
Better get all your insults of Kent in now.
|
|
|
|
|
Imagine being able to disconnect or redirect Internet traffic destined for some of the world’s biggest companies — just by spoofing an email. And we're almost out of spit
|
|
|
|
|
And this is new how? Every system is held together by bubble gum and duct tape, and we are all out of bubble gum.
GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
In the 1970s my dad's Volkswagen was held together by rust.
|
|
|
|