Instead of the code that Ben uses, I wrote my own was it a good choice?

Hi, I handled the challenge in this video differently than Ben, I feel like my code is simpler and easier to read, and therefore “good code”. Can anyone who knows more than me let me know if I am correct in my thinking?

Ben’s Code:

private void CalculatePath()
    {
        LoadBlocks();
        BreadthFirstSearch();
        CreatePath();
    }

    private void CreatePath()
    {
        SetAsPath(endWaypoint);

        Waypoint previous = endWaypoint.exploredFrom;
        while (previous != startWaypoint)
        {
            previous = previous.exploredFrom;
            SetAsPath(previous);
        }

        SetAsPath(startWaypoint);
        path.Reverse();
    }

    private void SetAsPath(Waypoint waypoint)
    {
        path.Add(waypoint);
        waypoint.isPlaceable = false;
    }

This is the code I wrote before seeing Ben’s solution:

 private void CreatePath()
    {
        LoadBlocks();
        BreadthFirstSearch();
        CalculatePath();
        MarkUnplaceableBlocks();
    }

    private void MarkUnplaceableBlocks()
    {
        foreach (Waypoint waypoint in path)
        {
            waypoint.isPlaceable = false; // all waypoints in path cant have towers placed
        }
    }

I am BRAND new to programming and for the first time felt like I did a decent job and wrote clean working code on my own. Could anyone please confirm that this was good, or enlighten me on why doing it Ben’s way was better? I know that if they both work there is no wrong answer, so is there any glaring flaws in the way I handled this challenge? Thanks for any feedback!

1 Like

Your code is good and it works, you should be proud, I can see two slight issues but one is a “maybe”, a very big maybe, and the other is so absurd it’s not even worth mentioning, also, your code is indeed more readable, with Ben’s code it might be hard to see where the Waypoint is set as Unplaceable. So, great job!

1 Like

I agree with Yee. Always go for the extra mile and don’t hesitate to push yourself, that’s what separates you from the rest of the pack.

1 Like

Seriously thanks for the compliment. I appreciate the feedback! You’re awesome.

1 Like

Privacy & Terms