Thursday, February 14, 2019

Tic Tac Toe Sharp

Though I have many years of experience in Java and Ruby, I just got my first experience in building a C# project the other day, a small Tic Tac Toe game. It is a command-line program coded with the new Visual Studio Community for Mac.

The code has been posted over here: https://github.com/AndyObtiva/TicTacToeSharp



Pattern Applied: Model-View-Presenter.

TicTacToe is the game Model based on IGame interface, which houses all the logic for making moves, checking for win or draw, and maintaining the board status after every move.

TicTacToeUI is the Presenter of the game model, sending game output to the View and acting as a controller to process input from the user.

MyConsole based on IConsole interface and leveraging C#'s Console is the View, which renders output to the screen and receives input from the user

Noteworthy Details:

  • Starting development in C# with the mentality of Java development worked out quite well
  • Looking for Functional Programming features such as Lambda Expressions from Java 8 and block iterators from Ruby got me discovering LINQ pretty quickly
  • Dynamic typing was available too, which I toyed with initially given my attachment to it from Ruby, and then later reverted to static typing given I discovered the types I needed
  • Attribute getters and setters are on par with Ruby's and probably better than Java's non-existent support for attributes (without using some meta-programming annotations)
  • Operator overloading/indexers are on par with Ruby's and better than Java's lack of support for operator overloading
  • Interface based polymorphism of methods felt cumbersome compared to Java as I had to cast a class object into its interface to invoke interface methods
  • Package management was as good as Java's
  • xUnit testing was as good as Java's and Ruby's
  • Utility methods seemed to support me completely and be on par with utility methods in Ruby and perhaps slightly better than Java's (felt that way though could be just equal)
  • While Visual Studio Community for Mac includes some built-in refactoring features, such as class rename, it is definitely inferior to Eclipse, IntelliJ Idea, and IntelliJ RubyMine. It seems ReSharper, a tool offered by IntelliJ's company JetBrains to enhance Visual Studio is a must. I didn't get to toy with it, but I would imagine I'd do so on future C# projects.
Overall, I felt well supported by C#'s versatility and not lacking any programming features from Java and Ruby.

That has not been my experience with other languages that compare to Java or Ruby. For example, Rust, which I've looked into recently, has a very steep learning curve (albeit quite fun) to writing code that auto-manages its own memory without a garbage collector (Rust's highest selling point and greatest innovation). Also, Perl in its later incarnation seemed to be a clone of Perl-inspired Ruby, providing most of Ruby's features, but with alternative syntax, making me wonder why switch at all. Yet again, that means I get to have the option of consulting for Perl projects if need be (though no sane Rubyist would :P ). The same goes for C# after doing more learning in it. 

I probably did not exploit every C# feature in the course of writing this mini-project.

Comments are welcome, especially that I could be way off in this article due to being new to C#, and would like to be corrected if so.

Cheers and happy coding!

No comments: