Jump to content



1

Is it always a sin to repeat code?


6 replies to this topic

#1 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,910 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Wed Mar 19, 2008 7:22 PM

I read a long time ago in a magazine or a book that it was better to reuse code by using gosub or goto than to use the same code more than once, but what if the code is small and only used twice? Wouldn't the label and the added gotos or gosubs and returns almost not make it not worth it? If you want a specific example, here you go:

  color=color+2 : if color=0 then color=2
Seems like it would be better to just use that twice instead of doing something like this:

mainloop

  if !joy0fire && click=3 then click=0 : if timer<8 then timer=0 : goto changecolor

  if switchselect then goto changecolor

 . . .

changecolor
  color=color+2 : if color=0 then color=2
  goto mainloop

Does anyone have any thoughts about which is really better when it come to batari Basic?

#2 Fort Apocalypse OFFLINE  

Fort Apocalypse

    Stargunner

  • 1,593 posts

Posted Wed Mar 19, 2008 8:06 PM

View PostRandom Terrain, on Wed Mar 19, 2008 9:22 PM, said:

Does anyone have any thoughts about which is really better when it come to batari Basic?

If you aren't reusing code in an environment like the 2600 where space is at a premium, don't use gosub or goto blocks unless they are called in two places. If they are called in two places, then share the code.

More info:

See what Michael wrote in this thread about the amount of cycles that is taken up by gosubs. From what I gathered from that using
gosub
then
return bank1
takes up the least amount of cycles (37). However, in terms of space, here is the breakdown (I don't know whether this is constant, but I've seen this in my testing).

goto and goto = 6 bytes
gosub and return = 4 bytes
gosub and return thisbank = 4 bytes

So if the goal is to take up the fewest amount of bytes but cycles don't matter much, gosub and return are best. But if the goal is to take up the fewest amount of bytes and cycles matter, use return thisbank (when calling in same bank) according to Michael's post, and see Michael's post in that thread for detail.

In bB, goto mostly seems to only make sense when you need to break out of a block that you've gosub'd too (known as a "subroutine"), and when you use it there you must call pop as many times before the goto as the subroutine is deep, for example:

 mainloop
 ...
 gosub mylabel1
 ...
 gosub mylabel1
 ...
 goto mainloop

mylabel1
 ...
 if danger=1 then gosub mylabel2
 ...
 return thisbank

mylabel
 ...
 if died=1 then pop : pop : goto died
 ...
 return thisbank

Edited by Fort Apocalypse, Wed Mar 19, 2008 8:13 PM.


#3 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Wed Mar 19, 2008 8:17 PM

View PostFort Apocalypse, on Wed Mar 19, 2008 10:06 PM, said:

So if the goal is to take up the fewest amount of bytes but cycles don't matter much, gosub and return are best. But if the goal is to take up the fewest amount of bytes and cycles matter, use return thisbank (when calling in same bank) according to Michael's post, and see Michael's post in that thread for detail.
Good point, but note that my comments about gosub and goto were related to bankswitching. If you do a straight gosub in the same bank, it takes only 12 cycles of overhead-- 6 for the gosub, and 6 for the return.

In general, if you have two or more sizable sections of code that are identical, then it's a lot better to put the code into a single subroutine. Even if there are two or more segments that are almost the same but with some differences, you might be able to combine them into a single routine with some ifs to choose between alternate segments where the code diverges a bit. But for short sections of code, it might be more efficient to just repeat the code wherever it's needed.

Michael

#4 Fort Apocalypse OFFLINE  

Fort Apocalypse

    Stargunner

  • 1,593 posts

Posted Wed Mar 19, 2008 8:27 PM

Thanks, Michael!

I also messed up by overgeneralizing and using bad grammar and saying

Quote

In bB, goto mostly seems to only make sense when you need to break out of a block that you've gosub'd too

I should have added a second case for goto. goto seems also to be the only good choice for one way trips like:
 if nomonster=1 then goto mylabel0
 rem monster code
 ...
mylabel0

Edited by Fort Apocalypse, Wed Mar 19, 2008 8:28 PM.


#5 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,910 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Wed Mar 19, 2008 9:58 PM

Thanks for the info. So I guess in this case I'll just repeat that small amount of code then.

#6 lord_mike OFFLINE  

lord_mike

    Chopper Commander

  • 161 posts

Posted Wed Mar 19, 2008 10:57 PM

View PostRandom Terrain, on Wed Mar 19, 2008 9:22 PM, said:

I read a long time ago in a magazine or a book that it was better to reuse code by using gosub or goto than to use the same code more than once.

Well, academically speaking, yes, it is better to reuse code for various reasons... mostly 'cos of readibility and ease maintenance... but, when you are dealing with an environment like the 2600 where both space and availalbe cycles are at a premium, you may have to make sacrifices that aren't always "pretty".

If you need to save space, use subroutines

If you need to save cycled, repeat the code

#7 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,910 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Wed Mar 19, 2008 11:19 PM

View Postlord_mike, on Thu Mar 20, 2008 12:57 AM, said:

Well, academically speaking, yes, it is better to reuse code for various reasons... mostly 'cos of readability and ease maintenance... but, when you are dealing with an environment like the 2600 where both space and available cycles are at a premium, you may have to make sacrifices that aren't always "pretty".

If you need to save space, use subroutines

If you need to save cycles, repeat the code
Thanks. Good thing to remember.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users