It's dirty isn't it?

I find it challenging to keep my code clean and understandable,
and to write good methods with good clear names to simplify and compile.
I think my code here is dirty and improvised.
And I think that if I would have wanted to develop this game
I would have difficulties working like that in a bigger scale and in the future.

I want to learn how to make my coding style and methods
to be more efficient, simple, clear and organized.

1 Like

@Lior_Bourla

Try using a switch statement instead of the if else if statement this option is very scaleable , simple, clear and organized

Lloyd Risper

1 Like

If you have to use more than 2 or 3 strings in the if statement, I would put them in a List or Array in the beginning of the class and use the Enumberable method ‘Contains()’ to query through it.
Something like this:

private string[] levelOneSolution = new ["Atlichna","Popka","Yablaka","Ura","Fcusna"];
[...]
if (levelOneSolution.Contains(input)) {
    Terminal.WriteLine("You're in! :)");
}else{
    Terminal.WriteLine("Incorrect password");
}

Also, if you compare to a string, that is fully case-insensitive, you can do that with

if(input.Equals("menu", StringComparison.OrdinalIgnoreCase))

in your example, it’s just the first letter that can be written differently, so you could use a Regex statement instead like this:

if(Regex.Match(input, @"^(m|M)enu§").Success){ ... }

You can test your regex statements easily for example on this page: https://regexr.com/

At last, like @Lloyd_Risper already mentioned, use a switch statement inside the second else if for the level integer. If possible, refactor your outputs in their own small private methods with easily readable names. That makes it so much easier to read through your own code and helps to keep methods small and compact.

} else if (currentScreen == Screen.Password) {
    switch (level) {
        case 1: CompareInputForLevelOne();
             break;
        case 2: CompareInputForLevelTwo();
              break;
        case 3: CompareInputForLevelThree();
              break;
    }
}

private void CompareInputForLevelOne() { ... }

private void CompareInputForLevelTwo() { ... }

private void CompareInputForLevelThree() { ... }

For clean code in general I recommend you ‘Clean Code’ from Robert C. Martin.
It may already be a little bit older, but the design principiles for easily readable and accessible code are still appliable to every code.

1 Like

Thanks a lot :slight_smile:
Some I understand. other I will understand when more experienced.

Thanks a lot :slight_smile:

Privacy & Terms