create account

RE: How to write better code with Guard Clauses by nuthman

View this thread on: hive.blogpeakd.comecency.com

Viewing a response to: @themarkymark/how-to-write-better-code-with-guard-clauses

· @nuthman ·
Funny, I never knew this was called a *guard clause*. I seem to use them more often now than ever in this new era of love for weakly-typed languages. 

stuff like
~~~~
function whatever(value){
	if (isNaN(value))
		return false;
	...
	...
	return true;
}
~~~~

I wish Javascript were more like this (at least optionally):

~~~~
private function whatever(value:Number):Boolean{
	// Throws an error if passed value is not of type Number
	...
	return true;
}
~~~~

With languages like JS I spend most of my time trying to debug where the hell the issue is even coming from. I suppose some people like the flexibility of not declaring types, though. I know you can use something like TypeScript but it would be nice if one could [optionally static type](https://esdiscuss.org/topic/es8-proposal-optional-static-typing) and run it natively in the browser. 

But I digress... Nice to know what it's called a guard clause!
👍  
properties (23)
authornuthman
permlinkre-themarkymark-q9s2aa
categorytechnology
json_metadata{"tags":["technology"],"app":"peakd/2020.04.5"}
created2020-05-03 22:54:12
last_update2020-05-03 22:54:12
depth1
children1
last_payout2020-05-10 22:54:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length915
author_reputation344,453,626,754,867
root_title"How to write better code with Guard Clauses"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,168,527
net_rshares0
author_curate_reward""
vote details (1)
@souris ·
I have heard it termed as the happy path. Where as you work your way down the function you return out the "un-happy" steps. 
properties (22)
authorsouris
permlinkre-nuthman-qdmfyl
categorytechnology
json_metadata{"tags":["technology"],"app":"peakd/2020.07.1"}
created2020-07-17 16:18:24
last_update2020-07-17 16:18:24
depth2
children0
last_payout2020-07-24 16:18:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length124
author_reputation22,834,205,494
root_title"How to write better code with Guard Clauses"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,572,797
net_rshares0