basic-computer-games icon indicating copy to clipboard operation
basic-computer-games copied to clipboard

Use top-level statements for C#

Open zspitz opened this issue 4 years ago • 5 comments

For instance, this:

using System;
using System.Threading;

namespace AceyDucey
{
    /// <summary>
    /// The application's entry point
    /// </summary>
    class Program
    {
        /// <summary>
        /// This function will be called automatically when the application begins
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Create an instance of our main Game class
            Game game = new Game();

            // Call its GameLoop function. This will play the game endlessly in a loop until the player chooses to quit.
            game.GameLoop();
        }
   }
}

could be reduced to this:

// Create an instance of our main Game class
Game game = new Game();

// Call its GameLoop function. This will play the game endlessly in a loop until the player chooses to quit.
game.GameLoop();

On the other hand, using top-level statements obscures the basic "entry-point" nature of C# and .NET, which may not be a good idea from an educational standpoint.

zspitz avatar Jan 03 '22 12:01 zspitz

Well, you would also have to add this at the top: using AceyDucey;

And I'm not so sure it would be more readable. But I'm not big fan of top-level statements to be honest.

pczajkowski avatar Jan 03 '22 13:01 pczajkowski

From the docs:

Top-level statements let you write simple programs for small utilities such as Azure Functions and GitHub Actions. They also make it simpler for new C# programmers to get started learning and writing code.

While the projects here are simple, if my understanding is correct, they're supposed to emulate the conventions of larger projects. So, I would not use top-level statements for this reason.

Edit: On the other hand, if my understanding is correct, the code isn't meant to be understood by total beginners. We're utilizing OOP and subroutines, which means from an educational point of view, we're assuming the readers understand the basics such as Main, and we might be doing a favor by introducing features such as top-level statements so that more programmers are aware of it.

amrojjeh avatar Jan 03 '22 19:01 amrojjeh

Hmm. I would not say we're trying to emulate large projects here. We are emulating SMALL projects, so make choices appropriate for small projects.

Also the code should be understandable, not necessarily for total beginners, but I'd say.. intermediate level. Not expert!

coding-horror avatar Jan 03 '22 19:01 coding-horror

Got it. If that's the case, then I think it makes sense to use top level statements.

amrojjeh avatar Jan 03 '22 19:01 amrojjeh

Thanks @amrojjeh, sounds good!

journich avatar Jan 12 '22 02:01 journich