Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaner console code suggestion #34

Closed
Wycott opened this issue May 21, 2022 · 3 comments
Closed

Leaner console code suggestion #34

Wycott opened this issue May 21, 2022 · 3 comments

Comments

@Wycott
Copy link
Contributor

Wycott commented May 21, 2022

If your code makes heavy use of the Console class (which I suspect will be most games here, if not all), you can make method calls a little leaner by including:

using static System.Console;

This would reduce code like this:

Console.WriteLine("Dice Game");
Console.WriteLine();
Console.WriteLine("In this game you and a computer Rival will play 10 rounds");
Console.WriteLine("where you will each roll a 6-sided dice, and the player");
Console.WriteLine("with the highest dice value will win the round. The player");
Console.WriteLine("who wins the most rounds wins the game. Good luck!");
Console.WriteLine();
Console.Write("Press any key to start...");
Console.ReadKey(true);
Console.WriteLine();
Console.WriteLine();

To:

WriteLine("Dice Game");
WriteLine();
WriteLine("In this game you and a computer Rival will play 10 rounds");
WriteLine("where you will each roll a 6-sided dice, and the player");
WriteLine("with the highest dice value will win the round. The player");
WriteLine("who wins the most rounds wins the game. Good luck!");
WriteLine();
Write("Press any key to start...");
ReadKey(true);
WriteLine();
WriteLine();
@ZacharyPatten
Copy link
Collaborator

ZacharyPatten commented May 21, 2022

1. Easier To Port Code To Website If "Console" Exists

I actually had using static System.Console; previously. I ended up removing it for a couple of reasons. One of the main reasons at the moment is because the blazor versions of the games in the Website project use a field...
https://github.com/ZacharyPatten/dotnet-console-games/blob/6cb5187bcdca5b3c53d036ca7cf7673848830514/Projects/Website/Games/Dice%20Game/Dice%20Game.cs#L8
... to redirect "Console" references to use the Website.BlazorConsole field rather than System.Console since fields have priority over static types. So when I port the code to the web, I mainly just copy the code, add the field, and then add await before most "Console" references. If I had to add "Console" back into the code that wouldn't be too hard... just take me another couple of minutes to port games to the website.

I want to eventually make a code generator that will automatically port the code to the website #23 but I haven't had time to tackle that project yet.

2. Alternatives To using static Console = System.Console;

I'm a big fan of simplifying code. :) Yes using static simplifies that example, but there are ways to simplify it even further. For example...

Before

Console.WriteLine("Dice Game");
Console.WriteLine();
Console.WriteLine("In this game you and a computer Rival will play 10 rounds");
Console.WriteLine("where you will each roll a 6-sided dice, and the player");
Console.WriteLine("with the highest dice value will win the round. The player");
Console.WriteLine("who wins the most rounds wins the game. Good luck!");
Console.WriteLine();
Console.Write("Press any key to start...");
Console.ReadKey(true);

After

Console.WriteLine(@"Dice Game

In this game you and a computer Rival will play 10 rounds
where you will each roll a 6-sided dice, and the player
with the highest dice value will win the round. The player
who wins the most rounds wins the game. Good luck!

Press any key to start...");
Console.ReadKey(true);

The only thing I don't like about this is the indentation, but we are in luck! They are planning on adding a new feature in the next version of C# that will fix this: dotnet/csharplang#4304 "Raw String Literals".

Once "Raw String Literals" are added to C# then many of the games should start using them.

@Wycott
Copy link
Contributor Author

Wycott commented May 22, 2022

Ah, OK - that makes sense then. Thanks for taking the time to explain. 👍

@ZacharyPatten
Copy link
Collaborator

I'll close this issue for now. Feel free to reopen if you feel strongly about it.

If anyone contributes a game that has using static System.Console; I probably won't change it. :)

And just to clarify, we may add using static System.Console; back in the current games in the future, but I probably just want to clean up the process of porting to Blazor before we do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants