HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

C# beginner's exercise in calling methods

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
methodsbeginnercallingexercise

Problem

This is one of my first experiences with C#. I'm trying to learn and as can be seen, I need some obvious help. I know it's only an example but I'd like to start with best practices from the beginning. This was my first experience with public and private methods.

What can I do to improve my code and why is the change better? What about it makes it preferred or a best practice?

using System;

namespace CallMethodsWithinSameClass
{
    class Program
    {
        static void Main()
        {
            bool I_Has_Energy = true;
            OtherClass TestClass = new OtherClass();
            I_Has_Energy = TestClass.NoIDont;
            Console.WriteLine("Do I have energy? " + I_Has_Energy);
            Console.WriteLine("Getting a public method " + TestClass.GiveMeAString());
            Console.WriteLine("Grabbing the privates " + TestClass.GrabThemPrivates());
            Console.ReadKey();
        }
    }

    class OtherClass
    {
        public bool YesIDo = true;
        public bool NoIDont = false;    
        public string SimpleString = "This is only a test.";
        public string GiveMeAString()
        {
            return "Here's your sign, I mean string.";
        }
        private string KeepItToYourself()
        {
            return "Make sure you'll be gentle with them... *Wink*";
        }
        public string GrabThemPrivates()
        {
            return KeepItToYourself();
        }
    }
}

Solution

I have a few tips for your code:

-
Keep variable names consistent. Doing this makes it easier to remember (or guess) what you named them in the future. One good way of doing this is capitalizing all variable names the same way. Most programmers use what's known as camel case.

Example:

int camelCaseRules;


This will help you A LOT in the future when coming up with coherent variable names, and since most programmers use camel case, it will help you in team projects.

-
Another thing about variables is to make sure that you can understand it. E.g. don't name a variable NID (Standing for noIDont). When naming variables, you should make sure that you can get a basic understanding of what they do/are used for without digging in the code to figure it out.

-
Comment, comment, and comment. Keeping up good commenting habits early on in programming will help you tremendously in the future. This will heavily increase your code readability and make it so that when you come back to the code in a week, you can understand why you did something the way you did it. Just putting in those two forward slashes makes a huge difference. Something to keep in mind when commenting your code however (which was brought to my attention by Hosch250), is to not comment on what the code does, but what it is used for. Basically, well written code should explain what it does without comments, and comments should explain what it is used for in the program.

// Comment your code!


-
Finally, only make this public if they have to be (e.g. your YesIDo variable in the OtherClass class. I would explain why, but I think this article does a much better job of doing so.

Also, a good YouTube video I would check out is here.

Code Snippets

int camelCaseRules;
// Comment your code!

Context

StackExchange Code Review Q#158164, answer score: 5

Revisions (0)

No revisions yet.