Jump to content
IGNORED

Is it always a sin to repeat code?


Random Terrain

Recommended Posts

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Thanks, Michael!

 

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

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...