Is use of "level1Passwords.Length" versus hardcoding your number just for extensibility?

I wanted to make sure I understood why Ben used “level1Password.Length” as the other parameter. I assumed that this is so you could change the number of level 1 passwords and not have to change a hardcoded number. Is this a correct assumption?

Thanks,
Trey

void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                int index = Random.Range(0, level1Passwords.Length);
                password = level1Passwords[0];
                break;
1 Like

You’ve got it exactly! We like to avoid what are often called “magic numbers”. Last evening Rob made a post about it, and I’m sure he explains better than I could. By using the built-in .Length, then if the list size changes, the code adapts without any special updates needed to every function using the value.

2 Likes

Privacy & Terms