Post: My first program C# :)
05-16-2013, 01:37 PM #1
Dr. Tiga
Little One
(adsbygoogle = window.adsbygoogle || []).push({}); Hello NGU,
So today i bought C# for dummies, and with my previous java knowledge i made a little program. Im posting it here to hear some feedback on my coding style, and/or if you like the program.

Text:
You must login or register to view this content.

.Exe:
You must login or register to view this content.

~IcedTiga

The following 2 users say thank you to Dr. Tiga for this useful post:

Pichu, slim355

The following user groaned Dr. Tiga for this awful post:

Dhaax.
05-18-2013, 11:03 AM #11
slim355
You talkin to me?
Edsger Dijkstra is turning in his grave.
05-19-2013, 01:17 AM #12
Complete Speed
Do a barrel roll!
i enjoy this program it's nice the people talking about goto have some really good points but i would say pichu is looking at it the right way using goto in winforms would be a waste of time but using it for console apps can be a great time saver. but a little homework for the OP is to find ways to do it without using go to or something like that.
05-19-2013, 04:15 AM #13
Master Ro
I make food
Originally posted by Complete
i enjoy this program it's nice the people talking about goto have some really good points but i would say pichu is looking at it the right way using goto in winforms would be a waste of time but using it for console apps can be a great time saver. but a little homework for the OP is to find ways to do it without using go to or something like that.


Using goto shouldn't be looked upon differently when using it in a Windows Forms Application or a Console Application. It's really the same principle and it all comes back to the same question, "Should I use this?" Everything in C# can be used for some reason...but you can have different ways of doing one thing. I'm not saying goto tags are bad, because nothing is really bad in C#. It just creates bad habits in my opinion. If you have a block of code that you wanted to look back at in different situations, you could encapsulate it in a function and call it multiple times rather than having a chunk of code in the same function and going and referring back to it. Is having the code you want to look back on in the same function bad? No, of course not. It just makes the code less readable which in my opinion is one of the determining factors when judging a good programmer.

"A good programmer not only writes code machines can read, but writes code humans can read."
-Unknown

Winky Winky
05-19-2013, 05:53 AM #14
Pichu
RIP PICHU.
Originally posted by another user
If you have a block of code that you wanted to look back at in different situations, you could encapsulate it in a function and call it multiple times rather than having a chunk of code in the same function and going and referring back to it. Is having the code you want to look back on in the same function bad


I disagree, the goto can also be used to go back towards a specific point within a method while what you are saying, you would have to call the entire method.

Trust me, it is readable; I use it, a lot of people use it. Readable code is important however, it is very so much readable. If it is not that legible to you, then you need to spend some more time working with it.

You will find that if you turn every little thing into a function; your code will get messy and you will find that working with what you have is going to be a living hell.
05-19-2013, 03:27 PM #15
Master Ro
I make food
Originally posted by Pichu View Post
You will find that if you turn every little thing into a function; your code will get messy and you will find that working with what you have is going to be a living hell.


I never said you should turn every little thing into a function. Only the code you look back on multiple times. Even if it's one line of code that you are repeating, if you use it multiple times throughout your project, you should encapsulate it in a function. You can look at good, neat, open-source projects and see how their use of goto is extremely minimal if even there.
05-19-2013, 06:16 PM #16
Pichu
RIP PICHU.
Originally posted by Master
I never said you should turn every little thing into a function. Only the code you look back on multiple times. Even if it's one line of code that you are repeating, if you use it multiple times throughout your project, you should encapsulate it in a function. You can look at good, neat, open-source projects and see how their use of goto is extremely minimal if even there.


Re-read this:

I disagree, the goto can also be used to go back towards a specific point within a method while what you are saying, you would have to call the entire method.

Yes it may be a good idea but it depends upon what you are doing.
05-19-2013, 07:13 PM #17
Master Ro
I make food
Originally posted by Pichu View Post
Re-read this:

I disagree, the goto can also be used to go back towards a specific point within a method while what you are saying, you would have to call the entire method.

Yes it may be a good idea but it depends upon what you are doing.


I don't think you understood what I said in my reply.

If you want to look back at a specific line of piece of code in the same function, go ahead. All I'm saying is that when you want to refer back to something multiple times, you should create a new function. Let's look at two examples:

    
static void Main(string[] args)
{
int answer;
Console.WriteLine("Enter two numbers separated by a space");


string[] str_inputs = Console.ReadLine().Split(' 'Winky Winky;


int[] inputs = new int[2];


for (int i = 0; i < inputs.Length; ++i)
inputs[i] = Convert.ToInt32(str_inputs[i]);


Console.WriteLine("Tell me what type of operation you would like to use");
string operation = Console.ReadLine();
if (operation == "Addition")
{
AddTwoNumbers(out answer, inputs[0], inputs[1]);
}
else
{
if (operation == "Add")
{
AddTwoNumbers(out answer, inputs[0], inputs[1]);
}
if (operation == "Sum")
{
AddTwoNumbers(out answer, inputs[0], inputs[1]);
}
}


Console.WriteLine(answer);
Console.ReadKey();
}


static void AddTwoNumbers(out int answer, int num1, int num2)
{
answer = (num1 + num2);
}


    
static void Main(string[] args)
{
int answer;
Console.WriteLine("Enter two numbers separated by a space");


string[] str_inputs = Console.ReadLine().Split(' 'Winky Winky;


int[] inputs = new int[2];


for (int i = 0; i < inputs.Length; ++i)
inputs[i] = Convert.ToInt32(str_inputs[i]);


Console.WriteLine("Tell me what type of operation you would like to use");
string operation = Console.ReadLine();


if (operation == "Addition")
{
goto AddingBlock;
}
else
{
if (operation == "Add")
{
goto AddingBlock;
}
if (operation == "Sum")
{
goto AddingBlock;
}
}


AddingBlock:
answer = (inputs[0] + inputs[1]);


Console.WriteLine(answer);
Console.ReadKey();
}



Now, of course it doesn't really make a difference in this case because these are really small functions that don't do much. But in the long run, when you script more advanced systems that other programmers look at, then yes, it will certainly make a difference in terms of which code is more readable.
05-19-2013, 08:41 PM #18
Pichu
RIP PICHU.
Originally posted by Master
I don't think you understood what I said in my reply.

If you want to look back at a specific line of piece of code in the same function, go ahead. All I'm saying is that when you want to refer back to something multiple times, you should create a new function. Let's look at two examples:

    
static void Main(string[] args)
{
int answer;
Console.WriteLine("Enter two numbers separated by a space");


string[] str_inputs = Console.ReadLine().Split(' 'Winky Winky;


int[] inputs = new int[2];


for (int i = 0; i < inputs.Length; ++i)
inputs[i] = Convert.ToInt32(str_inputs[i]);


Console.WriteLine("Tell me what type of operation you would like to use");
string operation = Console.ReadLine();
if (operation == "Addition")
{
AddTwoNumbers(out answer, inputs[0], inputs[1]);
}
else
{
if (operation == "Add")
{
AddTwoNumbers(out answer, inputs[0], inputs[1]);
}
if (operation == "Sum")
{
AddTwoNumbers(out answer, inputs[0], inputs[1]);
}
}


Console.WriteLine(answer);
Console.ReadKey();
}


static void AddTwoNumbers(out int answer, int num1, int num2)
{
answer = (num1 + num2);
}


    
static void Main(string[] args)
{
int answer;
Console.WriteLine("Enter two numbers separated by a space");


string[] str_inputs = Console.ReadLine().Split(' 'Winky Winky;


int[] inputs = new int[2];


for (int i = 0; i < inputs.Length; ++i)
inputs[i] = Convert.ToInt32(str_inputs[i]);


Console.WriteLine("Tell me what type of operation you would like to use");
string operation = Console.ReadLine();


if (operation == "Addition")
{
goto AddingBlock;
}
else
{
if (operation == "Add")
{
goto AddingBlock;
}
if (operation == "Sum")
{
goto AddingBlock;
}
}


AddingBlock:
answer = (inputs[0] + inputs[1]);


Console.WriteLine(answer);
Console.ReadKey();
}



Now, of course it doesn't really make a difference in this case because these are really small functions that don't do much. But in the long run, when you script more advanced systems that other programmers look at, then yes, it will certainly make a difference in terms of which code is more readable.


I also agreed with the point of functions, they are great. It all depends upon what you are doing.

As programmers, we all have our own style, as long as we follow the basic rule of thumb when programming regarding proper programming etiquette, our code regardless of what we like to use should be readable. Please note, commenting is huge. I can hand you a program with 100,000 lines using both functions and goto methods, none of which is commented, the attempt to read it would be a bit of a bitch.

What got me trying to explain to you is that it is useful and is not spaghetti code.
Originally posted by another user
Using goto tags can create spaghetti code. I recommend to never use them


Overall, I use functions a whole lot more than goto methods, but I tend to prefer them when using the console window. I'm telling you though, once you get further into C#, you will find that goto methods are very nice to use and depending upon what you are doing, better than methods.
05-19-2013, 09:09 PM #19
Nice, Keep it up!

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo