FRDB Archives

Freethought & Rationalism Archive

The archives are read only.


Go Back   FRDB Archives > Archives > IIDB ARCHIVE: 200X-2003, PD 2007 > IIDB Philosophical Forums (PRIOR TO JUN-2003)
Welcome, Peter Kirby.
You last visited: Today at 05:55 AM

 
 
Thread Tools Search this Thread
Old 07-09-2003, 05:54 PM   #31
Veteran Member
 
Join Date: Jan 2001
Location: USA
Posts: 1,072
Default

Quote:
Principia: In my analysis on the previous thread, I said (and here DNAunion dishonestly deletes the following section when he cites the same quotes in his post above):
DNAunion: Nice try, but no good. There was nothing dishonest about my dropping those comments. They were not related to the topic I was addressing…you know, your silly assertion that if is, in and of itself, “spaghetti code”. Your incorrect comments were also irrelevant, so, I replaced them with ellipses.

Next.

Here’s my code Principia references next (it's the code that confuses Principia, and as we all know, if Principia can't understand it, then it MUST be spaghetti code....ROTFLMAO!)

Code:
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;
     }
}
DNAunion: And here’s his “analysis”.

Quote:
Principia [quoting himself from a previous post]: Let me point out that here, the test (nDiscardTilesOnceChosen == 0) is always satisfied, so the then block is never reached.
DNAunion: I already explained to you the reason for that. Did you forget that too? Here, let me remind you.

Quote:
DNAunion: So? It is there for flexibility. If someone wants to change the model to use replacement (instead of discarding objects after they’ve already been selected), he/she simply changes a single value and the whole program is “reconfigured” – it works under the new set of parameters without requiring any new code to be written, or any existing code to be commented out.
DNAunion: Anyone familiar with programming would have known that.

Let’s look at more of Principia’s “analysis”… picking up right where we left off…

Quote:
Principia: Let me also point out that the 1st elseif is irrelevant, since its action to set nFoundOne to 0 is redundant (i.e. nFoundOne would have to be 0 to get there anyways).
DNAunion: And I explained to Principia the logic behind including that.

Quote:
DNAunion: I will point out that it is considered, by a great many programmers, to be a bad idea to sacrifice clarity for compactness.

When a tile is chosen, there are two possibilities: either it has been chosen before, or it hasn’t, and both possibilities have to be taken into account. If the statement setting nFoundOne to 0 were omitted, it could cause temporary confusion on someone encountering the code for the first time. “What? The programmer left out the code that handles the case where the tile was already chosen”. Putting it in makes the logic clearer.

As far as compactness:

1) It’s a only 14 characters and took me only about 2 seconds to type (and as the programmer, I get to decide whether to sacrifice clarity to save 2 seconds worth of typing)

2) The code does not become “fatware” simply because I added something like 14 bytes to it

3) The time it takes an average computer to execute that one command is negligible; even running 1,000,000 iterations it would probably add only a small fraction of a second to the total execution time.

So again, it is a tradeoff, and it is up to the programmer to decide how to handle it.
DNAunion: I think when I get a few free minutes I’ll time the code both with and without that one statement and see how much time my improved clarity is “wasting”.

Of course I, unlike Principia, will be smart enough to comment out the cout statements in the loop when doing any type of time-sensitive measurements (every computer programmer knows that massive amounts of screen output slows execution considerably).
DNAunion is offline  
Old 07-09-2003, 06:04 PM   #32
Veteran Member
 
Join Date: Jan 2001
Location: USA
Posts: 1,072
Default

Quote:
DNAunion: See [Principia], you did as much as explicitly state that if itself suffers from “spaghettiness” (all ifs do the two things you claim…well, kind of). You even showed just how “spaghetti-ish” if statements are by adding the LOL, emphasizing your “point”.

Quote:
LeftCoast: You are grasping at straws. Ifs are not "spaghetti-ish".
DNAunion: Uhm, I think you have the two sides reversed.

It is Principia who asserted that ifs are, in and of themselves, "spaghetti code". My position is that they are not, but are instead, the fundamental selection structure in structured programming, found and used in every leading structured and object-oriented programming language in existence.

Quote:
LeftCoast: The whole concept of "Spaghetti Code" is predicated on the notion that human beings must understand the flow and logic structure of the program. If statements are not really contributors in this case because they cause the branching to happen in very obvious and easily traceable ways - you will either execute the block contained within the 'true' condition or the block contained in the 'false'/else condition. The fact that at the machine level this is implemented as a jump instruction is irrelevant.
DNAunion: Bingo!

Quote:
LeftCoast: The machine does not in any way "understand" what it is doing, it simply executes the instructions in the order the program counter addresses them. If you look at the machine language (aka compiled) version of even the most elegantly constructed code, you will undoubtedly see many logic transfers that if written by a human would be labeled "spaghetti", however, since only the machine will ever encounter this code - and it does not get confused - calling it "spaghetti" is pointless.
DNAunion: Bingo again!
DNAunion is offline  
Old 07-09-2003, 06:29 PM   #33
Veteran Member
 
Join Date: Jan 2001
Location: USA
Posts: 1,072
Default

Quote:
NialScorva: The vast majority of programmers will go their entire career without creating a single new algorithm...
DNAunion: Every programmer who writes any code that specifies what actions to take and what order to perform them in, in order to achieve a desired outcome, is creating an algoritm.
DNAunion is offline  
Old 07-09-2003, 06:53 PM   #34
Veteran Member
 
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
Default

Quote:
Originally posted by DNAunion
DNAunion: Every programmer who writes any code that specifies what actions to take and what order to perform them in, in order to achieve a desired outcome, is creating an algoritm.
Read "create new algorithm".

Most programming is taking an algorithm that already exists and connecting it to other algorthms in a sequence. Implementing an insertion sort on a linked list day in and day out isn't creating a new algorithm.

If you want to be completely pedantic about it, then I'll agree that all programming is creating a "new" algorithm. I'd also have to say that everytime someone loads a truck it's implementing a new algorithm as well.
NialScorva is offline  
Old 07-09-2003, 07:02 PM   #35
Veteran Member
 
Join Date: Jan 2001
Location: USA
Posts: 1,072
Default

Quote:
DNAunion: 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?
Quote:
Mageth: Blind luck?
DNAunion: No, because of the way memory is allocated for array variables.

Conceptually, a 9x3 array (in C++ or C or other languages that use 0-based arrays) would appear like the following:

Code:
[0, 0]	[0, 1]	[0, 2]
[1, 0]	[1, 1]	[1, 2]
[2, 0]	[2, 1]	[2, 2]
[3, 0]	[3, 1]	[3, 2]
[4, 0]	[4, 1]	[4, 2]
[5, 0]	[5, 1]	[5, 2]
[6, 0]	[6, 1]	[6, 2]
[7, 0]	[7, 1]	[7, 2]
[8, 0]	[8, 1]	[8, 2]
But the two subscripts are misleading…there is no real element [5, 1] in the array. That is, to access element [5, 1] the computer does not go down 6 rows and then over to the right 1. No, elements of an array are stored linearly and contiguously. You can demonstrate this for yourself by using pointer arithmetic instead of subscripts.

This code:

Code:
for (int row = 0; row < 9; ++row)
{
	for (int col = 0; col < 3; ++col)
	{
		cout << myArray[row][col] << endl;
	}
}
can be rewritten as follows;

Code:
for (int index = 0; index < 27; ++index)
{
	cout << *(myArray + index) << endl;
}
(PS: Note the use of unneeded curly braces in both versions for the sake of clarity.)

The second form traverses the elements of the two-dimensional array one right after the other, with no stopping to move to the right. This would be “impossible” if the array elements weren’t stored linearly and contiguously.

Why is this important? If the conceptual version were reality, then overshooting the second index by 1 would cause the last element’s data to written into the wrong row – the data for one row would be mixed in with the data for the next row, pushing its last data element into the next row, all the way down the line. No row would be immune from corruption. But since array elements are actually stored contiguously, in a linear manner, consistently overshooting by one simply displaces all elements by one, keeping the whole array intact.

The only issue that might arise is that the very last element overshoots the array bounds by 1 element’s worth. But, as long as the compiler doesn’t override my sequence of variable declarations to try some optimization, that doesn’t occur. All of the variables are defined at the very beginning of main(), and the last one defined is the array. So the array should be the last variable that memory is reserved for and overshooting the upper bound by one element does not cause a problem.
DNAunion is offline  
Old 07-09-2003, 07:19 PM   #36
Veteran Member
 
Join Date: Jan 2001
Location: USA
Posts: 1,072
Default

Quote:
NialsScorva: Most programming is taking an algorithm that already exists and connecting it to other algorthms in a sequence. Implementing an insertion sort on a linked list day in and day out isn't creating a new algorithm.
DNAunion: Even then, every programmer who uses linked lists, doubly linked lists, binary trees, stacks, or queues, has probably had to code one from scratch...it's part of learning the therory behind the data structures. Here's my implementation of a double linked list that I just finished.

Code:
// backwards.cpp
// Tests a stack implemented using a doubly linked list.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#include "Stack.h"

int main()
{
	char userInput[80], singleChar;
	int lcv = 0;

	Stack myStack;

	cout << "Enter some text: ";
	cin.getline(userInput, 80, '\n');

	// push characters one at a time onto the stack
	while (userInput[lcv]) myStack.Push(userInput[lcv++]);

	cout << endl;

	// pop characters one at a time off of the stack
	while (singleChar = myStack.Pop()) cout << singleChar;

	cout << endl;

	return 0;
}
Code:
// Stack.h

#ifndef STACK_H
#define STACK_H

#include "Node.h"

class Stack 
{
	private:
		Node *ptrFirstNode;
		Node *ptrLastNode;

	public:
		Stack();
		~Stack();

		void Push(char letter);
		char Pop();

		Node * GetFirstNode();
		void SetFirstNode(Node *pFirst);
		
		Node * GetLastNode();
		void SetLastNode(Node *pLast);
};

#endif
Code:
// Stack.cpp


#include "Stack.h"


Stack::Stack()
{
	SetFirstNode(0);
	SetLastNode(0);
}


Stack::~Stack()
{
	char singleChar;
	while (singleChar = Pop()) ;
}
	
	
void Stack::Push(char letter)
{
	Node *ptrNewest = new Node(letter);

	if (!GetFirstNode())
	{
		// Pushing first item onto the stack.
		SetFirstNode(ptrNewest);

		// There is only 1 item on the stack so it 
		// is both the first and the last item.
		SetLastNode(ptrNewest);
	}
	else
	{
		// Pushing item #2 or higher onto the stack.
		// First, create the back link from the newest 
		// node to its previous one.
		Node *ptrWasLastNode = GetLastNode();	
		ptrNewest->SetPrev(ptrWasLastNode);

		// Assign the Newest Node's pointer to what 
		// was the LastNode's Next pointer.
		ptrWasLastNode->SetNext(ptrNewest);
		
		// Point the LastNode pointer to the Newest Node
		// making it the last node in the series.
		SetLastNode(ptrNewest);
	}
}


char Stack::Pop()
{
	Node *pLastNode = GetLastNode();

	if (!pLastNode)
		// Stack is empty
		return 0;

	// Before deleting LastNode, save its data 
	// for returning
	char Letter = pLastNode->GetLetter();

	
	// NextToLastNode is needed to reassign LastNode 
	// once what is currently the LastNode is deleted
	// (since it is being popped off the stack).
	// If the current LastNode is the first node, 
	// then NextToLastNode will be NULL since the 
	// first node doesn't have a previous Node.
	Node *ptrNextToLastNode = pLastNode->GetPrev();

	// Delete the current LastNode.
	// Remember, if the first item is being popped, 
	// then ptrNextToLastNode will be NULL.
	if (!ptrNextToLastNode)
	{
		delete pLastNode;
		SetLastNode(0);
		SetFirstNode(0);
	}
	else
	{
		delete pLastNode;
		// Reassign LastNode since what was the 
		// LastNode is now gone.
		SetLastNode(ptrNextToLastNode);
	}

	return Letter;
}


void Stack::SetFirstNode(Node * pFirst)
{
	ptrFirstNode = pFirst;
}


void Stack::SetLastNode(Node * pLast)
{
	ptrLastNode = pLast;
}


Node * Stack::GetFirstNode()
{
	return ptrFirstNode;
}


Node * Stack::GetLastNode()
{
	return ptrLastNode;
}
Code:
// Node.h

#ifndef NODE_H
#define NODE_H

class Node
{
	private:
		char letter;

		Node *ptrNext;
		Node *ptrPrev;

	public:
		Node(char Letter);
		
		void SetLetter(char Letter);
		char GetLetter();
	
		Node * GetNext();
		void SetNext(Node *pNext);

		Node * GetPrev();
		void SetPrev(Node *pPrev);
};

#endif
Code:
// Node.cpp

#include "Node.h"

Node::Node(char Letter)
{
	SetLetter(Letter);
	SetPrev(0);
	SetNext(0);
}


void Node::SetLetter(char Letter)
{ 
	letter = Letter;
}


char Node::GetLetter() 
{
	return letter;
}

	
Node * Node::GetNext() 
{ 
	return ptrNext;
}


void Node::SetNext(Node *pNext) 
{ 
	ptrNext = pNext;
}


Node * Node::GetPrev() 
{ 
	return ptrPrev;
}


void Node::SetPrev(Node *pPrev) 
{ 
	ptrPrev = pPrev;
}
DNAunion: Of course the next step of many would be generalizing it by making it a template (currently, it only works for char's). But I've already created my own algorithm for a doubly linked list.
DNAunion is offline  
Old 07-09-2003, 07:33 PM   #37
Veteran Member
 
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
Default

When I say "creating" an algorithm, I mean designing a new method of doing something that has different performance, storage, or other characteristics. Contrast this with "implementing" an algorithm.

While it's not character-for-character the same, it is functionally and mathematically equivalent to practically every other doubly linked list algorithm ever written. Twenty years ago an oct tree for spatial decomposition might have been a new algorithm. Now that every 3d game uses it, the nth reimplementation isn't a new algorithm.
NialScorva is offline  
Old 07-09-2003, 10:13 PM   #38
Veteran Member
 
Join Date: Dec 2000
Location: Tucson, Arizona, USA
Posts: 1,242
Default

First responsibility of any programmer is to get the damn thing to work.
Jeremy Pallant is offline  
Old 07-09-2003, 10:56 PM   #39
RBH
Contributor
 
Join Date: Aug 2002
Location: Ohio
Posts: 15,407
Default

Jeremy Pallant wrote
Quote:
First responsibility of any programmer is to get the damn thing to work.
And the first question to ask is not "Does it work" but "Can I fix it?"

RBH
RBH is offline  
Old 07-09-2003, 11:13 PM   #40
Veteran Member
 
Join Date: Dec 2000
Location: Tucson, Arizona, USA
Posts: 1,242
Default

Quote:
Originally posted by RBH
And the first question to ask is not "Does it work" but "Can I fix it?"

RBH
To which I have always been able to reply "yup".

Admittedly I do less and less programming these days. I am diversifying into management.
Jeremy Pallant is offline  
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump


All times are GMT -8. The time now is 07:54 PM.

Top

This custom BB emulates vBulletin® Version 3.8.2
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.