WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Day 5 (3. Returning values)(optional)

Forums > 100 Days of Swift

can someone explain to me how these three line become one line

let isAdmin = true let isOwner = false let isEditingEnabled = false

isOwner == true && isEditingEnabled || isAdmin == true

1      

Please use the code format tags when you post code examples. It's MUCH easier to read! Plus it's easier to cut and paste into Playgrounds.

Abdallah asks for clarification

let isAdmin = true 
let isOwner = false 
let isEditingEnabled = false

// Don't do this! This is hard to understand!
isOwner == true && isEditingEnabled || isAdmin == true

This point of this article (see: Checking Multiple Conditions) is that the last line of this example is HARD to understand.

Developers can interpret these conditional statements a few ways. However, Swift has a defined order. But that's not the point. The point is, if it's hard to understand, it is a BAD practice. Don't do this!

In fact, don't try to understand it! Please add () to your statement to make it clear to you and to futureYou!

// Bad! Don't write this:
if isOwner == true && isEditingEnabled || isAdmin == true {
     // perform amazing functions
}

// Better, do this! Make your code clear!
if (isOwner == true && isEditingEnabled) || isAdmin == true {
    // perform amazing functions
}

// Even better! Do this. Make it read cleanly.
if (isOwner && isEditingEnabled)  || isAdmin {
    // perform this code
    // if the user is the OWNER of this post AND EDITING is enabled.
    // OR
    // if the user is a site Administrator.
}

Because isAdmin, isOwner, and isEditingEnabled are all booleans, they can only be true or false.

// DON'T WRITE
if isEditingEnabled == true {
    // perform amazing functions
}

// Instead, write:
if isEditingEnabled {
    // perform amazing functions
}

1      

thank you mr @Obelix for clarifing that for me, next time i will use code format tags

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.