question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it correct to emulate basics features

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
DLottscommented, Mar 5, 2022

The tab© 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.

1reaction
Timo-Weikecommented, Mar 5, 2022

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));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Simulator or Emulator? What is the difference?
In short: an emulator is designed to copy some features of the orginial and can even replace it in the real environment. A...
Read more >
What is emulation? | Definition from TechTarget
Emulation, in a software context, is the use of an application program or device to imitate the behavior of another program or device....
Read more >
Emulator - Wikipedia
Emulation refers to the ability of a computer program in an electronic device to emulate (or imitate) another program or device. DOSBox emulates...
Read more >
The Pros and Cons of Playing Video Games on an Emulator
Emulating video games has several benefits, but it's not perfect. Let's look at the pros and cons of game emulation.
Read more >
What Quality of Life features should more emulators ...
Dolphin actually has both, though the first feature is not very easy to find. You have to right click on the input config...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found