multiple line - forceUnwrap https://forceunwrap.com Lift your Swift skills & career to the next level Tue, 28 Feb 2023 20:28:13 +0000 en-US hourly 1 https://wordpress.org/?v=7.0 https://forceunwrap.com/wp-content/uploads/2023/03/cropped-ForceUnwrap-32x32.jpg multiple line - forceUnwrap https://forceunwrap.com 32 32 Line breaks – multiple guard statements https://forceunwrap.com/line-breaks-multiple-guard/ https://forceunwrap.com/line-breaks-multiple-guard/#respond Fri, 17 Jun 2022 08:55:12 +0000 https://forceunwrap.com/?p=171 Proper line breaking makes a difference. Don’t put everything in one line. The usual character limit is 120 symbols per line. Put each statement in new line. else could be on a separate line.

Pro tip:
I prefer to put in on a new line for better readability. In my experience I have noticed, that putting else on a new line, helps spoting start and end of guard without reading the context inside the guard.

// Don't
guard
		let clientID = Bundle.main.object(forInfoDictionaryKey: "ApiClientID") as? String,
		let serverClientID = Bundle.main.object(forInfoDictionaryKey: "ApiServerClientID") as? String
else {
		return
}

// Don't
guard let clientID = Bundle.main.object(forInfoDictionaryKey: "ApiClientID") as? String, let serverClientID = Bundle.main.object(forInfoDictionaryKey: "GoogleServerClientID") as? String else { return }

// Do
guard let clientId = Bundle.main.object(forInfoDictionaryKey: "ApiClientID") as? String,
			let serverClientId = Bundle.main.object(forInfoDictionaryKey: "ApiServerClientID") as? String
else {
		return
}

How do you line break guard with multiple statements? Share in the comments below.

The post Line breaks – multiple guard statements first appeared on forceUnwrap.

]]>
https://forceunwrap.com/line-breaks-multiple-guard/feed/ 0