[Ktechlab-devel] bad pattern

Alan Grimes agrimes at speakeasy.net
Wed Mar 4 17:16:59 UTC 2009


Since I'm almost broke and stressed out, I've been spending all my time
hacking on ktechlab... I just spent most of the morning figuring out how
to better to link pin currents to nodal currents...

Here's a bad pattern I came across in Component:

#############################
void Component::setNodalCurrents() {

	const ElementMapList::iterator end = m_elementMapList.end();
	for (ElementMapList::iterator it = m_elementMapList.begin(); it != end;
++it) {
		ElementMap m = (*it);

		for (int i = 0; i < 4; i++) {
			if (m.n[i]) {
			m.n[i]->mergeCurrent(m.e->readCurrent(i));
			}
		}
	}
}
########################

See that? We have a loop here that basically manipulates a datastructure
in another class and then sends it another data structure
__FROM_THE_SAME_CLASS__. This should never happen, here's a better code:

###############################
void Component::setNodalCurrents() {

	const ElementMapList::iterator end = m_elementMapList.end();
	for (ElementMapList::iterator it = m_elementMapList.begin(); it != end;
++it) {
		(*it).mergeCurrents();
	}
}

void ElementMap::mergeCurrents()
{
	for (int i = 0; i < 4; i++) {
		if (n[i]) {
			n[i]->mergeCurrent(e->readCurrent(i));
		}
	}
}
##############################

See, that's much better. ;)


-- 
New president: Here we go again...
Chemistry.com: A total rip-off.
Powers are not rights.





More information about the Ktechlab-devel mailing list