Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reducing code.

Status
Not open for further replies.

Phil79

Technical User
Jun 5, 2002
64
GB
Hi guys, me again with my "Why use 5 lines when 25 will do" code. Please could somebody show me a more efficient way of writing the following, as it looks like I may need to put the same code (but slightly differant) about 20 times.

Thanks a bunch.

Phil


Code:
for (i=1; i<18; i++){
		if (sN==1 && e[i].e1!=0){
			addrow (id, (q*e[i].e1)+&quot; x&quot;, e[i].code, e[i].name);
			pt[i+67]=(q*e[i].e1)
		}
		if (sN==2 && e[i].e2!=0){ 
			addrow (id, (q*e[i].e2)+&quot; x&quot;, e[i].code, e[i].name);
			pt[i+67]=(q*e[i].e2)
		}
		if (sN==3 && e[i].e3!=0){
			addrow (id, (q*e[i].e3)+&quot; x&quot;, e[i].code, e[i].name);
			pt[i+67]=(q*e[i].e3)
		}
		if (sN==4 && e[i].e4!=0){
			addrow (id, (q*e[i].e4)+&quot; x&quot;, e[i].code, e[i].name);
			pt[i+67]=(q*e[i].e4)
		}
		if (sN==5 && e[i].e5!=0){
			addrow (id, (q*e[i].e5)+&quot; x&quot;, e[i].code, e[i].name);
			pt[i+67]=(q*e[i].e5)
		}
		if (sN==6 && e[i].e6!=0){ 
			addrow (id, (q*e[i].e6)+&quot; x&quot;, e[i].code, e[i].name);
			pt[i+67]=(q*e[i].e6)
		}	
	}
 
Hi Phil79,

It should have been something like this (I quickly made the code, I didn't tested it):

[ignore]
for (i=1; i<18; i++){

for (j=1; j<7; j++){
if (sN==j && eval('e.e' + j + '!=0'){
addrow (id, eval(q*e.e' + j)+&quot; x&quot;, e.code, e.name);
pt[i+67]= eval('q*e.e' + j)
}
}

}

Maybe the part eval(q*e.e' + j)+&quot; x&quot; is wrong and could be something like: eval(q*e.e' + j + ' x')

[/ignore]

Hope this points you in the right direction,
Erik <!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
Or, reduce by function --

Code:
for (iI=1; iI<18; iI++) {
  switch(sN) {
    case 1: 
      magicFn(e[iI],e[iI].e1,iI+67)
      break
    case 2: 
      magicFn(e[iI],e[iI].e2,iI+67)
      break
    case 3: 
      magicFn(e[iI],e[iI].e3,iI+67)
      break
    case 4: 
      magicFn(e[iI],e[iI].e4,iI+67)
      break
    case 5: 
      magicFn(e[iI],e[iI].e5,iI+67)
      break
    case 6: 
      magicFn(e[iI],e[iI].e6,iI+67)
      break
    }
  }
function magicFn(obj,val,idx) {
  if (val) {
    var calc = q * val
    addrow(id,calc+&quot;x&quot;,obj.code,obj.name)
    pt[idx] = calc
    }  
}
This type of reorganization stresses the relationships between elements, tries to move focus away from the programming language, towards the problem being solved. It can help you spot errors of analysis, solutions that are perfectly programmed but don't work.

Even after reorganization there are lots of &quot;magic&quot; numbers, 1 - 6, e1 - e6, 18, 67. Give yourself a break and tag these number by what they represent. It will make things go easier now and later when you have to change something.

(It is possible that you are being purposefully cryptic to &quot;protect&quot; your implementation. If so, reconsider. Nearly no one cares what the source code looks like. If you're lucky enough to get your program in front of someone that does care you'll probably want to be showing your best efforts not hiding them.)
 
Thanks for your help guys.

Wray, it's not that I'm trying to be cryptic with my code, it's just that I realise that the way I'v done my coding may not be the most efficient manner. I guessed that there must be a more efficient way to write such a repetitive piece of code - I just didn't know how to do it!!!

Thanks very much for your comments though. I will take them on board.
 
Thanks for your help guys.

Thanks very much for your comments Wray. I will take them on board. It's just that I'm struggling to think of differant names for all the variables. The program contains nearly 1200 lines of javascript (although someone with a bit of coding skill could probably knock it down to 500!!!).
 
for (i=1; i<18; i++){
eval(&quot;if (e[&quot;+i+&quot;].e&quot;+i+&quot;!=0){addrow (id, (q*e[&quot;+i+&quot;].e&quot;+i&quot;)+' x', e[&quot;+i+&quot;].code, e[&quot;+i+&quot;].name); pt[&quot;+i+&quot;+67]=(q*e[&quot;+i+&quot;].e&quot;+i+&quot;)&quot;)
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top