Combining Code

This is a big one. For me, at least.

I have several different drawing methods that each perform similarly, to the point that the code is nearly redundant. The problem is that they have enough differences that combining them into one big honking draw method would either require lots of counter-productive if/else statements or a bajillion parameters. I tried the bajillion parameters way and stuff broke and I chickened out and restored everything from backups.

So my questions are:

  1. Is combining the methods into one even necessary/advantageous? Will it be faster to keep them separate, especially if they’re called simultaneously?

  2. I tried using a table to store the values of the parameters of each object that could be drawn so that each method call would look like

 draw(table) 

instead of

 draw(x, y, rotation, scale, label, list, index, ...)

That didn’t work. What is the proper way to pass a table in as a series of parameters?

  1. Is there a better way to do this that I just completely missed the bus on? If so, tell me, oh wise one. [import]uid: 89724 topic_id: 16911 reply_id: 316911[/import]

Why just not create you own “draw” function which use table/object elementh as param??? [import]uid: 12704 topic_id: 16911 reply_id: 63451[/import]

Could you kindly elaborate? [import]uid: 89724 topic_id: 16911 reply_id: 63465[/import]

Lets look at this logically:

function a()  
 do  
 a  
 bunch  
 of  
 stuff  
 unique for a  
 do   
 a   
 bunch  
 of  
 repeat  
 code  
end  
  
function b()  
 do  
 a  
 bunch  
 of  
 stuff  
 unique for b  
 do   
 a   
 bunch  
 of  
 repeat  
 code  
end  
  
if some condition then  
 a()  
else  
 b()  
end  

vs.

function combined()  
 if some condition then  
 do  
 a  
 bunch  
 of  
 stuff  
 unique for a  
 else  
 do  
 a  
 bunch  
 of  
 stuff  
 unique for b  
 end  
 do   
 a   
 bunch  
 of  
 repeat  
 code  
end  
  
combined()  

The second version has the same number of if statements. The unique code is the same between the two versions, but you saved repeating the common block.

Which is more efficient? [import]uid: 19626 topic_id: 16911 reply_id: 63479[/import]

That makes sense.

Now let’s say the unique elements are such that I still require the use of a long string of parameters to facilitate a combined draw function.

How do I pass a long-donkey stream of parameters without naming them every time I call the method?

Also, that technically didn’t answer my question. I asked if it was faster, not more efficient. From what I could tell with my earlier attempts at combining, calling the same function with different parameters four times sequentially is slower than calling four different methods simultaneously. [import]uid: 89724 topic_id: 16911 reply_id: 63486[/import]