Best way to improve code?

I’m new to Swift and I’m using Playgrounds to learn. When I solve a puzzle I often feel like my solution was sloppy or inefficient. Is there a better place to post my code for feedback? I figured I would start here and take suggestions greatfully. Thanks in advance! Below is my solution for Learning to Code 2, Types, Setting the Right Portal. I feel like it could be better...


var gemCounter = 0

pinkPortal.isActive = false

func collectRoutine() {

collectGem()

gemCounter += 1

bluePortal.isActive = false

}

func navigatePart1 () {

while gemCounter < 2 {

moveForward()

if isBlocked {

turnLeft()

turnLeft()

}

if isOnGem {

collectRoutine()

}

}

}

func navigatePart2 () {

while gemCounter < 4 {

moveForward()

if isBlocked {

turnLeft()

turnLeft()

}

if isOnGem {

collectGem()

gemCounter += 1

}

if gemCounter == 3 {

pinkPortal.isActive = true

}

}

}

navigatePart1()

bluePortal.isActive = true

navigatePart2()

Replies

You should do more decomposing/composing.

Try to analyze the problem and find a solution following this rules:

  • Minimize at maximum code repetition.
  • Break the code in more little reusable functions that builds larger functions.
  • Try to use more specific names for your functiions, it helps in keeping track of your thoughts and build larger functions (routine or navigate is too generic). It also helps other to understand your code (foundamental for working in a team)
  • Be coherent with the function name when adding statements to the function.


For example, collectRoutine(). Calling it collectAndActivate() would be more intuitive. Or you can erase the portal part and call it collectAndCount(). It is also more reusable this way.

Try to be more adaptable in your problem solving thinking. Before coding focus on what you are dealing with. For example here in playgrounds you play with labirinths. So you have to main goals here. Move efficiently and do stuff while moving. Stop. Instead of thinking of moving and doing stuff for every section of the labirinth try to think of an efficient code to move around till the end and then add the "do stuff" part.

I don't think being more specific in what you should do (like posting solutions examples) would be a good help. There are a lot of different approaches and you should find your stile. As long as your code is efficient and readable any solution will do it.

Most of all try to have fun^^

Here is my solution to the puzzle. You will notice there is much less code and I didn't need to utilize functions. I will say that I don't like the fact the portals activate again once the character turns around to collect the last gem. Still... the blue portal deactivates because the gemCount variable == 3. The puzzle is solved... that last portal activation just irritates me.

pinkPortal.isActive = false

bluePortal.isActive = false

var gemCount = 0



while gemCount < 4 {

    moveForward()

    if isOnGem {

        collectGem()

        gemCount = gemCount + 1

    }

    if gemCount == 3 {

        bluePortal.isActive = false

    }

    if isBlocked {

        turnLeft();turnLeft()

        pinkPortal.isActive = true

        bluePortal.isActive = true

    }

}

Here is my solution without hardcode:

func portalsNotActive() {

    bluePortal.isActive = false

    pinkPortal.isActive = false

}

portalsNotActive()

var gem = 0

while (gem < 4) {

    if (isBlocked) {

        turnLeft()

        turnLeft()

        bluePortal.isActive = true

        pinkPortal.isActive = true

    } else if (isOnGem) {

        collectGem()

        portalsNotActive()

        gem = gem + 1

    } else {

        moveForward()

    }

}

crunchy

var collectedGems = 0

while collectedGems < 4 { moveForward() if isBlocked { turnLeft() turnLeft() bluePortal.isActive = false pinkPortal.isActive = true } if !isBlocked && isOnGem { bluePortal.isActive = true pinkPortal.isActive = true turnLeft() turnLeft() } if !isBlockedLeft && !isBlockedRight { pinkPortal.isActive = false } if isOnGem { collectGem() collectedGems += 1 } }

This lesson is before the Functions, so I didn't really learn anything about them and just used the stuff I was introduced to. It doesn't seem possible to me, to not have to activate the last Blue Portal, even when you have 3 gems already (which would mean, deactivate the portal and keep it that way until you reach to collect the final gem). With my code, the portal first activates, as it goes through the loop, and then deactivates after it checks the gemCount variable if its == 3.

Of course it is possible to achieve this, but it would take more code, but I wanted to keep it short. I wanted to be a little bit creative with this, and while I feel happy about it, I still think that there is a better solution out there.

Also wondering if similar code would end up being open for some exploits, since it opens up that portal when it's not necessary. It just feels like it opens the doors and welcomes the bugs 😅

var gemCount = 0

pinkPortal.isActive = false
bluePortal.isActive = false

while gemCount < 4 {
    moveForward()
    if isOnGem {
        collectGem()
        gemCount = gemCount + 1
    }
    if isBlocked && gemCount <= 3 {
        turnLeft()
        turnLeft()
        pinkPortal.isActive = true
        bluePortal.isActive = true
    } else if gemCount == 3 {
        bluePortal.isActive = false
        pinkPortal.isActive = false
    }
}