coding-horror / basic-computer-games

An updated version of the classic "Basic Computer Games" book, with well-written examples in a variety of common MEMORY SAFE, SCRIPTING programming languages. See https://coding-horror.github.io/basic-computer-games/
The Unlicense
10.85k stars 1.33k forks source link

Is it correct to emulate basics features #590

Closed Timo-Weike closed 2 years ago

Timo-Weike commented 2 years ago

In the C# implementation for the Calendar program, the author emulated the basic function TAB, but if I understood it correctly, this repository should build up a reference for learning the various langauges and therefor emulate the behaviour of the original program. While using the native features of the language.

So I think this should be replaced with a simple call to just new string(' ', n).

And if one would want to show how can build up a string incrementally, then the implementation should be

var strBuilder = new StringBuilder(n);

for (int i = 0; i < n; i++)
{
    strBuilder.Append(' ');
}

because using + on strings in a loop is an anti-pattern which no one should learn.

https://github.com/coding-horror/basic-computer-games/blob/d3aeb35ed708a13db7397ce56d48962e3b5383da/21_Calendar/csharp/Program.cs#L12-L25

coding-horror commented 2 years ago

Sure, I agree, feel free to send through a PR on that!

DLotts commented 2 years ago

The tab(c) moves to the next column c, independent of the length of previous thing that was printed. This is actually a useful thing, and not equivalent to: "new string(' ', n)".

I've been wanting to dive into this tab(x) thing. Here is what I found:

Other languages can't do this with a simple function because it does not know the current position on the line. Languages that I know do this with a format string to pad items with spaces, so the next thing starts at the same column as the line above it. This is Python, and almost identical to Rust using index numbers to choose the values and alignment > and the padding:

print('{0:<30} {1:>35} {2:>35} {3:>35} {4:>20} {5:>20}'.format(a,b,c,d,e,f) )

Similar in C# Console.WriteLine("{0,10}{1,10}{2,10}", x, y, z);

Oh cool, C# let's you embed the variables in the {} braces instead of indexes. Start the format string with a $" --You'd be a coding hero if you showed how to do that!

Also, I wonder if the tab character works? print(x+"\t"+y+"\t"+z) david.

Timo-Weike commented 2 years ago

Other languages can't do this with a simple function because it does not know the current position on the line.

You can actually do this in c#. At the class Console we have properties and methods that allow us (on the supported OSs) to read and set the cursor position.

I'm also now unsure how many spaces the basic code would produce for the PRINT TAB(32);"CALENDAR"

I would also assume that the purpose of the tabing is to center the text in the console window. So one could argue that we should determin the width of the console with Console.BufferWidth and do a proper centering based on this width.

Regarding the tabbing, in gerneral I see 4 ways to do it that would be common and easy to unterstand

// format string with right-aligned min-field-width
// formating is okay; one would need to research string interpolation to understand that
Console.WriteLine("{0,40}", "CALENDAR");
Console.WriteLine("{0,55}", "CREATE COMPUTING  MORRISTOWN, NEW JERSEY");

Console.WriteLine();

// interpolated string with right-aligned min-field-width
// formating (IMO) less good; same as aboth
Console.WriteLine($"{"CALENDAR",40}");
Console.WriteLine($"{"CREATE COMPUTING  MORRISTOWN, NEW JERSEY",55}");

Console.WriteLine();

// naive print with prepended spaces
// formating okay; easy to understand
Console.WriteLine(new string(' ', 32) + "CALENDAR");
Console.WriteLine(new string(' ', 15) + "CREATE COMPUTING  MORRISTOWN, NEW JERSEY");

Console.WriteLine();

// left padded strings
// formating okay; would also need to know that the number must include the length of the string
// would in general in a non print context use this over 2
// but on in the print context I would use 1 over this
Console.WriteLine("CALENDAR".PadLeft(40));
Console.WriteLine("CREATE COMPUTING  MORRISTOWN, NEW JERSEY".PadLeft(55));
coding-horror commented 2 years ago

I believe https://github.com/coding-horror/basic-computer-games/tree/main/00_Common is an attempt at this?

coding-horror commented 2 years ago

Gonna close this out because I believe we have a folder / pattern for it now, thank you!