Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBC] Rewrite goto's challenge

Author
Message
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 31st Oct 2007 10:39 Edited at: 1st Nov 2007 04:45
The programme is from the magazine Sinclair Programs, August 1985 issue, and was written for the Spectrum or Spectrum Plus computers of the day.

Since DB Classic does not recognise line numbers, I had to show them as labels (the number followed by a colon). Not that they are necessary in the running of the programme. Just for reference.

As you can see, the programme contains a few of the so-called dreaded goto's, between lines 150 and 280, and I'd like to issue a challenge as to how this part of the programme could be written differently.



gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... In 1911, 3 men were hung for the murder of Sir Edmund Berry at Greenbury Hill, their last names were Green, Berry, and Hill.
Roxas
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: http://forum.thegamecreators.com
Posted: 31st Oct 2007 20:05
And the point was?

Nobody uses gotos anymore so why even brother?

Functions FTW!

[center]

Click for details!
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 1st Nov 2007 03:52 Edited at: 1st Nov 2007 03:53
Here's the Goto-less version of the old Speccy code.

It took a good ten or so minutes to get my head around it (why I absolutely hate code which uses Goto) and another five to re-write!



What do I win?

TDK_Man

gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 1st Nov 2007 05:15 Edited at: 1st Nov 2007 05:16
To Roxas

Thanks a lot for your help.

Quote: "Nobody uses gotos anymore so why even brother?"


I presume you mean bother.

The point is, not everyone has the same programming experience and I wanted to see how others would write out the goto.

By the way, what does FTW stand for? Why not write it in full so everyone can understand?

To TDK_Man

Thanks a lot for YOUR help.

You are always so helpful and informative in your replies, no matter what the question. I think I've said it before - from whom better to learn?

I had an idea it would have to be a lot of if/else but no idea of how to write it. I am looking to convert more programmes that were written for these earlier computers to run in DB Classic and this will certainly help.

Sorry! No prizes. Hope you are not disappointed?

gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... In 1911, 3 men were hung for the murder of Sir Edmund Berry at Greenbury Hill, their last names were Green, Berry, and Hill.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 1st Nov 2007 06:28
Quote: "What do I win?"

lots of riches

I'm gonna take the code and see what I can do with it (havent looked at TDK's yet)

"You must be someone's friend to make comments about them." - MySpace lied.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 1st Nov 2007 21:49 Edited at: 1st Nov 2007 21:57
I have one.



And no, I won't accept "simply put the cleanup code in every place where the function returns" as an answer. Nor will I accept calling another function as an answer, the function has to remain self-contained. As a matter of fact I can think of one solution, but I'll see if anyone else can guess it.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
gbark
18
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 1st Nov 2007 23:58 Edited at: 1st Nov 2007 23:58


I think that does the same thing as your function, Benjamin.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 2nd Nov 2007 01:54
That's a pretty good way of doing it actually, although I can imagine it becoming quite messy when there are a lot of places for the function to return from.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
gbark
18
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 2nd Nov 2007 02:09
True, if you have like 10 checks you need to do it would get kinda messy. I suppose this is another way you could do it, doesn't look as elegant IMO, but it would allow you to fairly easily add more checks.



Of course at this point I would probably start subdividing this function by each check, but I guess it depends on exactly how many checks you plan to do.
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 3rd Nov 2007 02:22 Edited at: 3rd Nov 2007 02:22
Thanks Benjamin and gbark, but this was a "gimme the code" sort of request

Quote: "The point is, not everyone has the same programming experience and I wanted to see how others would write out the goto."


gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... In 1911, 3 men were hung for the murder of Sir Edmund Berry at Greenbury Hill, their last names were Green, Berry, and Hill.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Nov 2007 01:21
Quote: "doesn't look as elegant IMO"


Ben's code was OK as it was - adding all of those other checks reduced the clarity of the code IMO.

*pours petrol on the flames of goto*

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
gbark
18
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 4th Nov 2007 02:25
Quote: "Ben's code was OK as it was - adding all of those other checks reduced the clarity of the code IMO.

*pours petrol on the flames of goto*"


Heh, I guess you're right there... I don't mind GOTOs in small doses, it's just I've found that bad things happen when you use them to much (like the example in the first post )

I think of GOTOs like junk food, not bad once in a while, but I don't get into the habit of using them.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 6th Nov 2007 15:20
My philosophy is: if you can write a program without using GOTO... you can write a program without using GOTO.

"You must be someone's friend to make comments about them." - MySpace lied.
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 8th Nov 2007 01:18
Sorry!

Quote: "Thanks Benjamin and gbark, but this was a "gimme the code" sort of request"


Quote: "The TGC Forum AUP

* Don't Ask For Code

Other forum users will help you fix problems with your programs. They will NOT write programs for you - that's not the idea... To do so would simply discourage you from learning how to program for yourself.

The idea is that you LEARN to program in DB yourself and write your own programs.

It's common for someone to write a small program - called a code snippet - to demonstrate the subject you are struggling with, but no-one is going to write something for you to just drop into your own program and work. The idea is that you use the snippet to learn from and with that knowledge implement it in your own program yourself.

So, don't post an idea and expect someone to provide you with a DB program to do what you want - you will only get negative responses."


gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... In 1911, 3 men were hung for the murder of Sir Edmund Berry at Greenbury Hill, their last names were Green, Berry, and Hill.
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 29th Dec 2007 17:44
Disguising a "gimme the code" request as a challenge - good use of reverse psychology.

All GOTO programs can be turned into flowcharts, and then turned into If/Else/Endif branches.

Your signature has been erased by a hyper-intelligent pan-dimensional being (a mod)
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 14th Jan 2008 07:27
Oh damnit, I didn't realize TDK did it at the top




Come see the WIP!
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 15th Jan 2008 01:08
Thanks to everyone who offered help ...... constructive or otherwise.


gearce
(GRC)

LANG MEY YER LUM REEK
Whatever the job you are asked to do at whatever level, do a good job because your reputation is your resume.

Login to post a reply

Server time is: 2024-05-17 06:23:28
Your offset time is: 2024-05-17 06:23:28