Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

my code style

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was wondering if my code style was okey(like if you guys could read it if you need to read it about months or so:P). I made this code:
#include <iostream> // include the header for input and output
#include <string> // we need this header so we can use strings
using namespace std; // this is for using cout and cin

int main() // main function
{ // begin of the code

    int Inum; // use a integer variable
    
    string How_Are_You; // use a string for cin that is more then just one word
    
    How_Are_You = "how are you? "; // string with value text how are you?
    
    float a, b; // use a float variable for floating numbers
    
    char aChar; // use a char varaible for characters
    
    aChar = 'a'; // define aChar as a

    a = 3; // here we give the variable a a value of 3
    
    b = 45; // here we give the variable b a value of 45

    cout << "Hello world" << endl; // print the text hallo wereld!
    
    cout << "Enter a number: "; // ask user to enter a number
    
    cin >> Inum; // wait for user to enter a number and save the number in a variable
    
    cout << aChar; // print variable aChar that has the value a
    
    cout << "\n";
    
    cout << How_Are_You; // print the string with the value how are you
    
    system("pause"); // pauses the program and wait for user to press a button
    
    return 0; // end of the program
    
} // end of the code

Share this post


Link to post
Share on other sites
Advertisement
The coding style seems fine.

One thing i will criticize is your use of comments, your comments in most cases should be not what you are doing for each line but why you are doing those things. Most of your comments simply say in english what the line of code is, which to 99% of coders is going to be obvious just by looking at the code.

Other than that thought nice one!

ace

Share this post


Link to post
Share on other sites
yeah i know but im just learning to use comments so i thought it would be handy if i do it with every line of code but i know its not needed and ill change my comment style:P. thnx for giving me advice.

Share this post


Link to post
Share on other sites
Another nice thing to do is to group similar statements, instead of doublespacing everything.

cout << "stuff";
cout << "more stuff";
cout << "yet more stuff";

cin >> stringFullOfStuff;

cout << stringFullOfStuff;
cout << endl;

Share this post


Link to post
Share on other sites
As ace said, too many comments. :)

If the reader don't understand 'int Inum', then the comment '// use a integer variable' isn't going to help.

And for everyone, it makes it much much harder to find the *important* comments.
If I want to understand what your program does, I try to look at the comments. If I need to understand how something works, I look at the comments. And if the comments are cluttered with useless junk, I can't find what I'm looking for. [wink]

Of course, there's no golden rule about commenting, but the most popular version I've heard is to write clean code that's self-documenting as far as possible, and add comments to explain entire code chunks, or why something works, what it does, or in some cases, how it works.

But the goal should be to write your code so that you need as few comments as possible to understand it. And then *only* add comments to explain stuff that isn't immediately obvious.

Other than that, as Drakkcon said, group code together in logical chunks.
And most people prefer to use lowercase variable names (inum instead of Inum).
Uppercase is usually reserved for classes, and sometimes functions (depends on who you ask). And a lot of people dislike _'s in variable names as well.
Be careful how you name variables and functions. Calling a variable containing the string "how are you" How_Are_You (Wow, that's a pain to type out) isn't really very useful. It doesn't tell you what it's used for, and if you change the contents of it one day, you're going to have a really misleading name.

Anyway, here's a slightly cleaner version. Still haven't done everything I said above, but I think it's a lot easier to read already. You might add a few more comments, or remove some


#include <iostream>
#include <string>

using namespace std;

int main()
{
/* Declare local variables */
int Inum;
string How_Are_You;
float a, b;
char aChar;

/* Initialize local variables */
How_Are_You = "how are you? ";
aChar = 'a';
a = 3;
b = 45;

/* Test reading from keyboard and writing to console with string, int and char */
cout << "Hello world" << endl;
cout << "Enter a number: ";
cin >> Inum;
cout << aChar << endl;
cout << How_Are_You;

// Keep program from exiting
system("pause");

return 0;

}




But anyway, your coding style is as good as mine, as long as you're consistent, and keep it readable. All I, or anyone else, can do is to offer suggestions based on our own preferences.

Share this post


Link to post
Share on other sites
Some other sugestions:

1. Use intializers for the variables:

int Inum;
string How_Are_You = "how are you? ";
float a = 3, b = 45;
char aChar = 'a';

I think this is easier to read, because I don't have to look up the variable name once to see both its type and initial value.

2. Be consistent in your use of upper and lower case. In your piece of code you have variables named "a", "Inum" and "aChar" and "How_Are_You". There is no consitency in this naming. You should use the same style for each kind of names (local variables, functions, types, class member variables). Personally I use:
- short names for local variables, often a single character if it is clear what it is (Point p, String s)
- CamalCase for types
- C++ style for functions and other variables (long_variable_name)
Again, what you choose doesn't really matter, as long as it is consistent.

Share this post


Link to post
Share on other sites
Use descriptive names for variables and functions. How_are_you should have been named userPrompt or something like that.

Don't worry too much about your style, so long as it's consistent. Everywhere you work will have slightly different rules, so you'll have to change many, many times.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!