homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam

The Geek Culture Forums


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» The Geek Culture Forums   » Other Geeky Stuff   » Ask a Geek!   » programming (Page 2)

 - UBBFriend: Email this page to someone!  
This topic comprises 2 pages: 1  2 
 
Author Topic: programming
Metasquares
Highlie
Member # 4441

Member Rated:
5
Icon 1 posted May 08, 2006 16:44      Profile for Metasquares   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Kinguy:
Failing with a giant boom and a stack trace is much more useful then just silently accepting it.

One of my professors used to call that principle "blow up early, blow up often, and blow up loudly".
Posts: 664 | From: Morganville, NJ | Registered: Oct 2005  |  IP: Logged
Kinguy
Geek
Member # 4527

Member Rated:
5
Icon 1 posted May 08, 2006 21:12      Profile for Kinguy     Send New Private Message       Edit/Delete Post   Reply With Quote 

Posts: 181 | Registered: Oct 2005  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted May 08, 2006 21:42      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by drunkennewfiemidget:
quote:
Originally posted by GameMaster:

- Put constants first in comparisions.

I agree with all of your points except that one. Code that compares with the constant first looks funny, and is counterintuitive imnsho.
I used to agree. But here's why I changed my mind.
code:
for(int i = 0; i = 20; ++i)
{
cout << i << endl;;
}

Compiles. And runs. Exactly like you told it to... forever, and ever, and ever...

Where as:
code:
for(int i = 0; 20 = i; ++i)
{
cout << i << endl;
}

won't compile.

Fail fast, it's one of the few places C/C++ lets you fail fast without adding anything extra.

Meta, I've heard the same thing from one of my profs... He's working on research about fail fast and uniqueness.

Streams in C++ are horrible in that they set a flag and quietly go on running... They should throw an exception, IMHO.

--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
Metasquares
Highlie
Member # 4441

Member Rated:
5
Icon 1 posted May 09, 2006 07:57      Profile for Metasquares   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
stream.exceptions(ios::badbit | ios::eofbit | ios::failbit);

There you go; now they'll throw [Smile]

IMO, the benefit of writing assignments constant first doesn't outweigh the loss of readability that results unless you are particularly prone to making that mistake. It doesn't happen often.

Posts: 664 | From: Morganville, NJ | Registered: Oct 2005  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted May 09, 2006 11:43      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
5 > 3 and 3 < 5 carry the same meaning in my eyes, as does i > 3 and 3 < i. I have no problem reading either way. Especially when talking about is equal ==.

Thanks for the tip about making the streams throw... I wish they were on by default though, because when ever I pass the lab there is always someone with an infinate loop because they tried to use
code:
int a;
while(!cin.eof())
{
cin >> a;
//stuff
}

Then give a char or string. The error flag flips, but eof isn't true. The stream stops taking input and causes the body of the loop to happen infinatly. I constantly see students do it, and I remember doing it myself. If it had just thrown, I would have realized, always get as a string and convert later.

--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
Metasquares
Highlie
Member # 4441

Member Rated:
5
Icon 1 posted May 09, 2006 19:18      Profile for Metasquares   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
You can actually make that:

while (cin >> a) {
//stuff
}

and the loop will exit once the stream hits EOF (or the other bits get set). That's one of the most useful idioms I've seen in the language.

Posts: 664 | From: Morganville, NJ | Registered: Oct 2005  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted May 10, 2006 16:40      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Metasquares:
You can actually make that:

while (cin >> a) {
//stuff
}

and the loop will exit once the stream hits EOF (or the other bits get set). That's one of the most useful idioms I've seen in the language.

Yes, you can, but most of the people in the begining programming courses that "... have a question, it'll just take a minute." don't know that yet... More important, they won't realize exactly why that works...

>> operator returns the stream. The if is looking for a bool, and the first member in the istream and ostream classes is the bool for the error flag. Which is implicitly converted to a void *, which C/C++ allows to be cast as the first member in a struct or class... The fact that there is a lot of implicit casts make it not the ideal solution to tell the student who is doing (the trouble code -- same as above):

code:
int a;
while(!cin.eof())
{
cin >> a;
//stuff
}

and in a low level course. You'll be there a for a year explaining how 'cin >> a' is a cast bool. Especially if they haven't seen casting yet.

Moreover, your code doesn't fix the actual problem that causes the infinite loop. The fact that a char was typed when expecting a number. What I generally advise, is always read as a string and use atoi(). For anything real, where the people ooking at are coders and not early students, the readability of "while(cin >> a)" or "while(getline(cin,a,'\n')" is nice (given a is a string).

Another thing I sometimes do help readability is:
code:
#define EVER (;;)

for EVER
{
}

That way, it's clear that loop is meant to run forever and didn't just forget to fill in the rest of for loop.

--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
Metasquares
Highlie
Member # 4441

Member Rated:
5
Icon 1 posted May 10, 2006 18:34      Profile for Metasquares   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Oh, you meant it freezes when they type a something non-numeric when it's expecting a number. You're right, that won't fix that problem.
Posts: 664 | From: Morganville, NJ | Registered: Oct 2005  |  IP: Logged


All times are Eastern Time
This topic comprises 2 pages: 1  2 
 
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

Contact Us | Geek Culture Home Page

2015 Geek Culture

Powered by Infopop Corporation
UBB.classicTM 6.4.0



homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam