Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 12-18-2008, 03:49 PM   #1
MDBurpo
Junior Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss14
Location: Martinsville, IN
Default New to programming: Have question on preferred method of coding! (C++)

I'm enrolled in ITT Tech. I'm in the third week of my first term. It will be quite a while before I get to any real programming class as I need to knock out a bunch of general education first. I'm taking it upon myself to learn C++ in my spare time. I just started today, probably 3 hours ago... I've noticed a lot of similarities to PHP.

I'm on my third basic program. The first was a modified Hello World program. Basically a multiline self-introduction. Yay me. The second was similar, but slightly more complex. It's a self-introduction that presents itself line by line as the the user presses enter. Third, I'm currently working on a quiz reviewing the information found in the previous program. You're presented a question, you type your answer, you're right or wrong and continue the quiz.

I'm curious on the best way to format my coding. I see two ways to do it cleanly, and I'm not sure which way is correct or preferred.

Code:
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using namespace std;
int main()
{
	int answer;
	cout <<"What is the programmer's full name?\n";
	cout <<"1.) Matthew Benjamin Durpo\n";
	cout <<"2.) David Matthew Burpo\n";
	cout <<"3.) Matthew David Burpo\n";
	cout <<"4.) Benjamin Matthew Durpo\n";
	cout <<"\n";
	cout <<"Answer: ";
	cin >> answer;
	cin.ignore();
	if (answer==3)
	{
		cout <<"\n";
		cout <<"That's correct!\n";
		cout <<"\n";
		cout <<"Press 'Enter' to continue.\n";
		cin.get();
	}
	else
	{
		cout <<"\n";
		cout <<"Incorrect answer! The programmer is Matthew David Burpo.\n";
		cout <<"\n";
		cout <<"Press 'Enter' to continue.\n";
		cin.get();
	}
	cout <<"How old is Matthew?\n";
	cout <<"\n";
	cout <<"Answer: ";
	cin >>answer;
	cin.ignore();
	if (answer==24)
	{
		cout <<"\n";
		cout <<"That's Correct!\n";
		cout <<"\n";
		cout <<"Press 'Enter' to continue.\n";
		cin.get();
	}
	else
	{
		cout <<"\n";
		cout <<"Incorrect answer! Matthew is 24 years old.\n";
		cout <<"\n";
		cout <<"Press 'Enter' to continue.\n";
		cin.get();
	}
	cout <<"Matthew wants to be a:\n";
	cout <<"1.) Video Game Designer\n";
	cout <<"2.) Video Game Programmer\n";
	cout <<"3.) Video Game Producer\n";
	cout <<"4.) Both 1 & 2\n";
	cout <<"5.) Both 1 & 3\n";
	cout <<"6.) Both 2 & 3\n";
	cout <<"7.) 1, 2, & 3\n";
	cout <<"\n";
	cout <<"Answer: ";
	cin >>answer;
	cin.ignore();
	if (answer==4)
	{
		cout <<"\n";
		cout <<"That's correct!\n";
		cout <<"\n";
		cout <<"Press 'Enter' to continue.\n";
		cin.get();
	}
	else
	{
		cout <<"\n";
		cout <<"Incorrect answer! Matthew wants to be a video game programmer and designer.\n";
		cout <<"\n";
		cout <<"Press 'Enter' to continue.\n";
		cin.get();
	}
	cout <<"Press 'Enter' to exit the program...\n";
	cin.get();
	return 0;
}
OR

Code:
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using namespace std;
int main()
{
	int answer;
	cout <<"What is the programmer's full name?\n"
	"1.) Matthew Benjamin Durpo\n"
	"2.) David Matthew Burpo\n"
	"3.) Matthew David Burpo\n"
	"4.) Benjamin Matthew Durpo\n"
	"\n"
	"Answer: ";
	cin >> answer;
	cin.ignore();
	if (answer==3)
	{
		cout <<"\n"
		"That's correct!\n"
		"\n"
		"Press 'Enter' to continue.\n";
		cin.get();
	}
	else
	{
		cout <<"\n"
		"Incorrect answer! The programmer is Matthew David Burpo.\n"
		"\n"
		"Press 'Enter' to continue.\n";
		cin.get();
	}
	cout <<"How old is Matthew?\n"
	"\n"
	"Answer: ";
	cin >>answer;
	cin.ignore();
	if (answer==24)
	{
		cout <<"\n"
		"That's Correct!\n"
		"\n"
		"Press 'Enter' to continue.\n";
		cin.get();
	}
	else
	{
		cout <<"\n"
		"Incorrect answer! Matthew is 24 years old.\n"
		"\n"
		"Press 'Enter' to continue.\n";
		cin.get();
	}
	cout <<"Matthew wants to be a:\n";
	"1.) Video Game Designer\n"
	"2.) Video Game Programmer\n"
	"3.) Video Game Producer\n"
	"4.) Both 1 & 2\n"
	"5.) Both 1 & 3\n"
	"6.) Both 2 & 3\n"
	"7.) 1, 2, & 3\n"
	"\n"
	"Answer: ";
	cin >>answer;
	cin.ignore();
	if (answer==4)
	{
		cout <<"\n"
		"That's correct!\n"
		"\n"
		"Press 'Enter' to continue.\n";
		cin.get();
	}
	else
	{
		cout <<"\n"
		"Incorrect answer! Matthew wants to be a video game programmer and designer.\n"
		"\n"
		"Press 'Enter' to continue.\n";
		cin.get();
	}
	cout <<"Press 'Enter' to exit the program...\n";
	cin.get();
	return 0;
}
I appreciate any input. I'm an OCD perfectionist and would like to start off right with good coding habits. Thanks in advance!
MDBurpo is offline   Reply With Quote
Old 12-18-2008, 04:38 PM   #2
Adrir
Administrator
 
Adrir's Avatar

Activity Longevity
1/20 20/20
Today Posts
0/11 sssss1467
Location: London, UK
Default

Wow, keep up the good work. I'm sure you'll be a pro in no time at all and completely pwn your programming classes. Yaustar has provided a great coding example that I'm sure you'll find really useful. Another great resource is the book Code Complete which has a ton of useful tips and tricks.

The main issues I can see here are:
  • If you are going to use the 'using' keyword it is conventional to declare it inside the function to avoid potential conflicts later on down the line;
  • Statements are usually kept to a single line where there is enough space - personally I prefer the first example;
  • Conventionally, one puts the constant before the variable in an if statement in order to avoid accidently compiling (answer = 4) since (4 = answer) will throw a compile-time error;
    For example:
    Code:
    if (4 == answer)
  • Declare varialbes closer to where you start using them;
  • Use the spacebar key plenty to help improve readability;
  • Comment your code!

Of course you are welcome to have a difference of opinion with the conventions I try to follow; none the less I hope this helps.
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education
Networking | Current Project | Research | Teaching

Last edited by Adrir : 12-18-2008 at 04:46 PM.
Adrir is offline   Reply With Quote
Old 12-18-2008, 06:06 PM   #3
MDBurpo
Junior Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss14
Location: Martinsville, IN
Default

Code:
(4 == answer)
This is the correct way?
MDBurpo is offline   Reply With Quote
Old 12-18-2008, 06:11 PM   #4
nef
Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss58
Default

I agree mostly w/ Adrir w/ a few exceptions.

First, my thoughts on the two examples. The way you layout your code dosen't REALLY matter. From my experience, you are told to format your code according the standard provided by your job anyways. But I like the first example. Breaking your calls to cout onto multiple lines made me double check to make sure it was valid at first, I've never seen anyone code like that .

Second, and this is just a portability thing, flip through the options in your editor and make sure that you are doing tabs as spaces. The tab character can fuck things up for people working in linux, the spacing may not be consistent. Personally, I prefer 4 spaces, but it looks here to be about 8, but again, thats just my preference.

You don't waste space w/ un-neded empty lines, which is also a personal preference of mine. You also don't do that crazy:

if (asdf) {
}

bracket notation either which I don't like.

And I would have to agree w/ adrir w/ using spacing more. (Mostly w/ the logical operations, like (variable==5) to (variable == 5) But again, its all about preference. For instance i could care less if there is a space after the 'if' or not.

Declaring variables when you use them. Hmm, personally I don't care to a certain extent. For instance, i have 0 problems as far as variable declaring is concerned w/ your code. But when you have a 250 line function, I care if you declared the variable used on line 157 at line 4. Just be reasonable is all.

To me all of this is personal preference, don't expect a real, or clear answer. Your style will grow and evolve as you get more experienced, or you'll just be straight up required to use a specific style

As far as better practices go, If i were to see this in a code review I would recommend the following:

1.) You are using the entire std namespace, yet you only use cout. Don't get into the habbit of just including namespace when you need a specific class. It creates a bigger executable by linking in the entire std namespace! This may not seem like a big deal now, but when you have hundreds of object files to link against, every little bit helps

Instead do this:

using std::cout;

or for string objects:

using std::string;

Or, some nazzi's don't 'use' anything at all, until they actually use it. They would include iostream and their cout calls would look like this:

std::cout << "hi guyz" << std::endl;

Thats to much for me, so i opt to just use the object in the namespace i want. IMO this is a very good habbit to get into early, because its a giant bitch to break later.

2.) answer isn't initialized to anything. This is big man, big. It may not seem like it now, but the shit i've seen and days I've spent tracking bugs that originated because of uninitialized variables. Stop that, always initialize, no questions asked.

3.) if (answer==3). 3? Wtf is 3? No magic numbers! :P

Seriously though, if you really want to get into good habbits, don't use magic numbers like this. Either define an enum up top, or declare variables w/ meaningful names and check if the answer given is equal to the variable/enum.

Again, from experience. When i look through a gigantic legacy function and find out that the variable 'a' I've been tracking for a 100 lines has changed from the magic number 2 to the magic number -1 only to be returned (and the returned value is expected to be -1, 5 or 6) And each one changes the state of the game dramatically I wana kick faces

4.) Your doing multiple "things" in one function. I would break up each question into a seperate operation (function) so that the code for each break in logic is sectioned off. It 's easier to debug a bug in question 2 when you know that all the logic for question 2 is isolated in function AskQuestionTwo();

That's it for the fundamental changes I would recommend in the style of the code. I would personally put all the questions and answers in some sort of expendable data structure and create a generic AskQuestion() function that takes in the list of questions and correct answer for easier extendability, but thats also over-engineering the problem here (ask two simple questions). If this was to go out i would argue its a must, to make it easily expendable.

To go all out would be to make a it data driven w/ a formated file and a generic question asking library. Then simply load the questions and answers, load the data either w/ your library or pass it to it and then call the AskQuestion() function. But I say this as just an example, obviously it over engineers the problem at hand.

Remember, over-engineering isn't good either. I'm not sure how much general programming experience you have to really know what i mean by over-engineering, but you'll learn that yourself someday

On a side note, to Adrir. I've never, ever seen someone using a namespace or class within a function. I don't doubt it's possible, but what are the advantages to this? I would have to strictly disagree it's conventional, solely because I've never seen it done
nef is offline   Reply With Quote
Old 12-18-2008, 06:25 PM   #5
nef
Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss58
Default

Quote:
Originally Posted by MDBurpo View Post
Code:
(4 == answer)
This is the correct way?
It's not "correct". Technically, both ways are "correct" but it is prefered by some because it helps detect one specific error.

Lots of people make the mistake of doing '=' for a compare, by accident of course, if you were to type:

(answer = 4)

By accident, this would compile and you would have a logic bug. (It would always evaluate to true)

if you force yourself into the habit of constant before variable when you do make this mistake you get a compile error and bam! bug fixed.

Some people swear by it, I've learned to go w/o it and very rarely make the mistake. I don't think I've made this particular mistake for at least over half a year now.
nef is offline   Reply With Quote
Old 12-18-2008, 06:49 PM   #6
MDBurpo
Junior Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss14
Location: Martinsville, IN
Default

nef,

I'm pretty sure I understand the changes you're pointing me to. I'm gonna start a new program and try some things out.
MDBurpo is offline   Reply With Quote
Old 12-18-2008, 07:02 PM   #7
nef
Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss58
Default

Quote:
Originally Posted by KKKJJJ View Post
Yaustar: Don't quote the post, it makes it a pain to delete
gg no captchas ?

Last edited by yaustar : 12-19-2008 at 08:06 AM.
nef is offline   Reply With Quote
Old 12-18-2008, 07:03 PM   #8
nef
Member

Activity Longevity
0/20 16/20
Today Posts
0/11 sssssss58
Default

Quote:
Originally Posted by MDBurpo View Post
nef,

I'm pretty sure I understand the changes you're pointing me to. I'm gonna start a new program and try some things out.
cool have fun

It's good you care about these things early. Most people I feel end up breaking bad habits or building good ones after they've already blown their feet off.... a couple times :P
nef is offline   Reply With Quote
Old 12-19-2008, 01:05 AM   #9
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
1/11 ssssss698
Location: Netherlands
Default

Nice advices there, nef. I might even have learned something from that! (I use 'magic numbers' exhaustively.) Although I absolutely hate putting accolades on a line of their own...

I have a question about the code MDBurpo posted. As I see it, the code:

Code:
cout <<"What is the programmer's full name?\n";
cout <<"1.) Matthew Benjamin Durpo\n";
cout <<"2.) David Matthew Burpo\n";
Will call the function cout three times, whereas:

Code:
cout <<"What is the programmer's full name?\n"
"1.) Matthew Benjamin Durpo\n"
"2.) David Matthew Burpo\n"
Will only call it once. Isn't the latter better? Or are they behaviorally equivalent? (I.e. will they compile to the same byte code?)
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is offline   Reply With Quote
Old 12-19-2008, 02:00 AM   #10
Adrir
Administrator
 
Adrir's Avatar

Activity Longevity
1/20 20/20
Today Posts
0/11 sssss1467
Location: London, UK
Default

Quote:
Originally Posted by nef View Post
First, my thoughts on the two examples. The way you layout your code dosen't REALLY matter. From my experience, you are told to format your code according the standard provided by your job anyways...Remember, over-engineering isn't good either. I'm not sure how much general programming experience you have to really know what i mean by over-engineering, but you'll learn that yourself someday.
Great post!

I have to say I completely missed number 2. I guess I need more practice reviewing code!

Quote:
Originally Posted by nef View Post
On a side note, to Adrir. I've never, ever seen someone using a namespace or class within a function. I don't doubt it's possible, but what are the advantages to this? I would have to strictly disagree it's conventional, solely because I've never seen it done
I'm still at university so I don't know whether this is conventional in the actual industry or not. As you mentioned earlier, different companies have their own house styles too, so I don't doubt that many of the conventions that the module leaders insist upon will be redundant by the time I get a job.

You can declare the directve within any scope. So why not? The main advantage being that it is easier than explicitly typing the namespace for every function call but can reduce the risk of calling incorrect objects or functions.
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education
Networking | Current Project | Research | Teaching
Adrir is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 04:32 PM.






UBM Tech