Site for Code Discussion

Started by
15 comments, last by Lactose 9 years, 2 months ago

Hi,

I have often wanted to ask opinion on the basic exercises I do as part of me learning C# but I don't really feel such basic questions fit on this site so I was wondering if there were any other good places for such a thing.

For example, I wrote a simple program to reverse a name, it works but I think it could be done tidier and was looking for feedback but as this site is mainly game dev (obv) I don't feel it has such a place.

If I'm wrong of course please let me know and I'll happily ask away!

Advertisement

This site is a pretty reasonable place to ask for feedback on your code.

The Code Review StackExchange site may be appropriate for certain things as well (read the help center though).

Ok great, well I made a simple console program in C# to ask your name and repeat it or reverse it. Although it achieves what I want it to (after a few tries) I was wondering if anything is glaringly bad or could be made in a lot less code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NameReverser
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter your name");
            string name = Console.ReadLine();
            Console.WriteLine("Would you like your name repeated(1) or reversed(2)");
            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.WriteLine("How many times to repeat?");
                string numberofTimes = Console.ReadLine();
                int times = int.Parse(numberofTimes);

                for (int i = 1; i <= times; i++)
                  Console.WriteLine(i + " - " + name);                         
            }

            else if (choice == "2")
            {
                char[] array = name.ToArray();
                Array.Reverse(array);
                Console.WriteLine(array);                
            }
            else
            {
                Console.WriteLine("I do not understand, exiting...");
            }

            Console.ReadLine();            
        }
    }
}

About the only thing I think you could improve on is using a "switch" statement instead of a nest of if and else. It might make sense to use routines. Have one for repeating and one for reversing and call those from the switch.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

Forgot to add my clause that I haven't got to switch statements yet (although I'm aware of what they are used for). I was actually a bit frustrated at just how difficult I found making such a simple program. The main issue I had was forgetting how to do the parse bit. I'm happy if there is nothing awful there anyway!

Looks pretty good to me.

Changing the if/elseif to a switch statement in this case is unnessary. It would totally be a case of premature optimization and only make the code slightly less readable.

The only thing I'd say... for the sake of learning code, avoid using the Array.Reverse() method and try to do it manually.

Also: what happens if someone wants to print their names -1 times?

Ah well I suppose I would add a conditional to make sure that times >=1. As to the other thing I'm not sure where I would even start with that, I know I have the name stored in the array and I know I can find the size of it but that's a challenge for tomorrow I think :)

Incidentally, I'm fairly sure when I was learning years ago I was told that a string was just a char array so I was surprised that I had to convert a string to an array in the first place. I may have remembered this wrong of course (was reading C & C++)

Incidentally, I'm fairly sure when I was learning years ago I was told that a string was just a char array so I was surprised that I had to convert a string to an array in the first place. I may have remembered this wrong of course (was reading C & C++)

C doesn't have strings, it has arrays of characters. An array of characters terminated with a zero character is used to represent a "string" and there are a bunch of standard library functions that work with character arrays conforming the that definition that make up C's "string library."
C++ inherits the same convention and also adds a std::string standard library type.
Strings in other languages are usually built-in, first-class types, as they are in C#. They may conceptually be arrays-of-characters but are not trivially interchangeable with them.

Ah well I suppose I would add a conditional to make sure that times >=1.

There's quite a lot you can do to your code to harden it against errors and bad input, and a few places where you are doing slightly under-performing operations (C# strings are immutable, it's generally better to get into the habit of using string.Format or the StringBuilder class to format them, as stuff like "string" + thing + "string" potentially creates a lot of garbage, for example).

Ah well I suppose I would add a conditional to make sure that times >=1. As to the other thing I'm not sure where I would even start with that, I know I have the name stored in the array and I know I can find the size of it but that's a challenge for tomorrow I think smile.png

You already have the chars put in an array. I think from there it should be obvious on how to go through it backwards wink.png

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

Thanks for all the replies, the patience of people on here is something else! CC Ricers, am I thinking it's something to do with a for loop?

This topic is closed to new replies.

Advertisement