Freethought & Rationalism ArchiveThe archives are read only. |
07-08-2003, 07:47 PM | #1 | ||||||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
A little on the "science" of computer programming
DNAunion: I was doing a little light reading the other night and ran across something interesting. The following quotes should speak for themselves (but hey, I’ll comment anyway).
Quote:
That settles that. But it also inspired me to dig a bit for more quotes on this general topic. So the next question is, is it really an error to use a little redundancy in order to improve the clarity of source code, as [somebody] asserted? Nah. Quote:
Quote:
Quote:
In that same text, the authors state: Quote:
DNAunion: Let’s face it, code readability and clarity are considered important goals in the programming world. And, tradeoffs – such as introducing slight redundancy, using a few more CPU cycles than required, using up several extra bytes of memory, etc. – are frequently made in order to promote those goals, AS PART OF GOOD PROGRAMMING PRACTICES. Next. Now, what was that [somebody] said about the if statement being spaghetti code????? Quote:
Let’s see. The goto/spaghetti-code crisis existed back in the 60s and 70s, and it was during the 70s that structured programming began taking hold as a means of eliminating such confusing code. Why in the world, then, would a brand-spanking new language like C# include the if structure if it is itself, as alleged-by-[somebody], “spaghetti code”? And why devote 5 or 6 pages to if if it is itself, as alleged-by-[somebody], “spaghetti code”? That’s a real puzzler, ain’t it??? Note also that two out of C#’s three selection structures are “if’s”. Worse yet, the third C# selection structure, switch, has pretty limited flexibility/applicability and therefore cannot substitute for ”if’s” in a vast many situations. However, the opposite is not true: that is, anything that a switch can do can be recoded using if (as can any if/else logic). Therefore, contrary to what [somebody] would have us believe, if is the fundamental selection construct of structured programming. And it’s not just C#. All major programming languages – Visual Basic and VBA and VB Script, Visual C++ as well as ANSI standard C++, C and PHP, Java and J++, Visual FoxPro, etc. – include the if selection mechanism, and it plays a pivotal role in each. If is clearly here to stay – it’s not about to be deprecated. Quote:
How about another one. Quote:
|
||||||||
07-09-2003, 04:46 AM | #2 |
Veteran Member
Join Date: Dec 2001
Location: southeast
Posts: 2,526
|
Who said IF was bad?
A couple of notes:
There is a vast difference between compile-time efficiency and run-time efficiency. Adding a redundant paren has no effect on run-time performance, and a virtually unmeasurable effect at compile-time. On the other hand, an extra paren may save hours of developer-time. I don't see where the if statement is claimed to lead to spagetti code. Where exactly are you reading that? |
07-09-2003, 05:55 AM | #3 | |||||
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
Quote:
Quote:
Quote:
Code:
Symbolic Derivative - Spaghetti Code (define (deriv exp var) (cond ((number? exp) 0) ((symbol? exp) (if (and (symbol? var) (eq? exp var)) 1 0)) ((and (pair? exp) (eq? (car exp) ’+)) (list ’+ (deriv (cadr exp) var) (deriv (caddr exp) var))) ((and (pair? exp) (eq? (car exp) ’*)) (list ’+ (list ’* (cadr exp) (deriv (caddr exp) var)) (list ’* (deriv (caddr exp) var) (caddr exp)))))) Code:
if (x == 1) { if (y == 1 && x > 0) { if (x > 2 || y < 2) printf("Hello world.\n"); else if (y < 3) printf("Goodbye world.\n"); } else { if (y < 3) if (x == 0) printf("Where am I?\n"); else break; } } else if (z == 2) { printf("Now I'm really confused.\n"); if (x == 1) { printf("But didn't we just test for this above?\n"); if (x < 0) printf("Whaa...??\n"); } } Quote:
Quote:
|
|||||
07-09-2003, 06:03 AM | #4 | |||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
DNAunion: See, [somebody] claimed that if itself was inherently "spaghetti-ish". How wrong could one person be. Quote:
PS: I should point out that while grabbing the above quote I just looked at my hastily written C++ code for the first time in weeks and immediately noticed a problem. Although I did remember to convert from 1-based arrays in VFP (the original language the program was written in) to 0-based arrays in C++ (the second language) during my quick conversion (which spanned only an hour or so, while posting simultaneously at 3 boards), I didn’t do so for all indices. Now there's an oversight...but it took me to find it! PPS: That mistake does not have any negative impact on the accuracy of the program's results...as both my and [somebody's] runs demonstrated. I wonder if [somebody] could explain to us why? |
|||
07-09-2003, 06:23 AM | #5 | ||||||||
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
Now for my rebuttal of DNAunion's strawman attack.
If anyone is wondering where this thread is coming from, it is here: http://www.iidb.org/vbb/showthread.p...5&pagenumber=1 In that thread, I demonstrated convincingly that DNAunion is a novice programmer, before the moderators shut down the thread for DNAunion's tantrums. It seems like, 4 months later, DNAunion still has a grudge, and wants to relive that thread. So I shall oblige. In his first section, DNAunion complains: Quote:
Code:
long GetRandomNumber(int nMin, int nMax) { long lRandomNumber; lRandomNumber = rand(); while (lRandomNumber < nMin || lRandomNumber > nMax) { lRandomNumber = rand(); } return lRandomNumber; } Quote:
Code:
for (nTrial = 1; nTrial <= nTrialsPerIteration; nTrial++) { // Pull a single tile out of the urn nFoundOne = 0; while (nFoundOne == 0) { nIndex = (int) GetRandomNumber(0, nLetteredTiles - 1); if (nDiscardTilesOnceChosen == 0) { // Doesn't matter if the tile has been // chosen previously because selected // tiles are placed back into the urn. nFoundOne = 1; } else if (cUrn[nIndex][2] == 'T') { // This tile has already been chosen and // discarded: it can't be selected again. nFoundOne = 0; } else if (cUrn[nIndex][2] == 'F') { // This tile has not been chosen previously. nFoundOne = 1; } } // A tile has been chosen: flag it as such cUrn[nIndex][2] = 'T'; // Does the chosen tile match one of the targets? if (cUrn[nIndex][3] == 'T') { nMatches += 1; } // No need to continue pulling tiles for this iteration // if we've obtained enough matches for success if (nMatches >= nMatchesNeededForSuccess) { nTrial = nTrialsPerIteration + 1; } } Code:
if (x == 1) { if (y == 1 && x > 0) { if (x > 2 || y < 2) { x=0; y=0; printf("Hello world.\n"); } else if (y < 3) printf("Goodbye world.\n"); } else { if (y < 3) if (x == 0) printf("Where am I?\n"); else { x = 2; break; } } } else if (z == 2) { printf("Now I'm really confused.\n"); if (x == 1) { printf("But didn't we just test for this above?\n"); if (x < 0) printf("Whaa...??\n"); } } Quote:
Quote:
Quote:
Quote:
Quote:
|
||||||||
07-09-2003, 06:41 AM | #6 | ||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
Sorry my dear old friend, but I have to go to work now. The rest of your easliy refuted statements will get to go unchallenged for a few hours. Now's the time for you to run for the hills and stop embarrassing yourself! |
||
07-09-2003, 08:16 AM | #7 |
Veteran Member
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
|
"Good programming practices" lists are almost always a list of results from good practices, not ways to achieve good practices. Removing gotos won't necessarily make your program any better, but a good program will contain fewer gotos. The single biggest problem with "how to program" guides is that they rarely understand this. Most of them are books that say "sometimes you need to sacrifice horsepower for emissions standards" and "Don't attach doors with wire, use hinges and weld them on", then claiming to be a book on building a car from the ground up.
|
07-09-2003, 08:58 AM | #8 |
Contributor
Join Date: Jul 2001
Location: Deep in the heart of mother-lovin' Texas
Posts: 29,689
|
PPS: That mistake does not have any negative impact on the accuracy of the program's results...as both my and [somebody's] runs demonstrated. I wonder if [somebody] could explain to us why?
Blind luck? |
07-09-2003, 09:19 AM | #9 |
Moderator - Science Discussions
Join Date: Feb 2001
Location: Providence, RI, USA
Posts: 9,908
|
Principia:
Now for my rebuttal of DNAunion's strawman attack. DNAunion: Tsk tsk...going for the personal jabs already Principia? Can't beat me legitimately...so sad. "Strawman" is an attack on a person's argument, not on the person themselves, so it's not against the rules. But a number of Principia's other comments involved statements about DNAunion's motives, honesty, etc., and those are definitely not appropriate here. Principia, please either avoid these types of personal attacks in your posts, or don't post on this thread at all. |
07-09-2003, 09:55 AM | #10 | |||||
Senior Member
Join Date: Feb 2003
Location: San Diego, California
Posts: 719
|
Speaking of "good" programming practices, I find one can learn quite a lot from this site.
Here are a few excerpts I thought were rather enjoyable: Quote:
Quote:
Quote:
Quote:
Quote:
Code:
class Truth { boolean isTrue ( boolean assertion ) { if ( assertion != false ) return assertion; else return assertion; } } ... Truth trutherizer = new Truth() ; if ( trutherizer.isTrue ( s.equals( t ) ) ) doIt = true; else doIt = false; // hint: all the above accomplishes is: doIt = s.equals( t ); |
|||||
Thread Tools | Search this Thread |
|