473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to write spaghetti code in C++???

I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???

Sep 27 '07 #1
20 6603
Victor Smootbank wrote:
I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???
What's the problem? Just keep doing what you've been doing.
Oh, are you confused by the case sensitivity? You can work
around it by defining macros like

#define GOTO goto

Trust me, it's not difficult to write spaghetti code in C++.
It's rather difficult to write *working* spaghetti code...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #2
Victor Smootbank wrote:
I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???
You should go back to classes.....
Sep 27 '07 #3
Victor Smootbank wrote:
I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???
Relax, C++ has goto. You should also have a look into try-throw-catch. That
is a goto on steroids that allows you to pass a value to the target of the
jump (it only works jumping up the call stack, though).
Best

Kai-Uwe Bux
Sep 27 '07 #4
>How can I write spaghetti code in C++???
>>
You should go back to classes.....
The most tangled spaghetti possible in C++ is templates. Without a single
goto, you can weave miles of infinitely tangled control-flow.

--
Phlip
Sep 27 '07 #5
Victor Smootbank wrote:
I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???
Just adding to others' opinions here: there's no shortage of language
support for spaghetti code in C++. You really should take a look at the
preprocessor and try to write a whole program in macros!!

For example:

#define SUBPROGRAM(x, ret) (ret) (x)(void)
#define BEGIN {
#define END }
#define OUTPUT std::cout<<
#define INPUT std::cin>>
#include <iostream// anyone knows how to write this line in macro?

SUBPROGRAM(main, int)
BEGIN
INPUT x;
INPUT y;
int z = x + y;
OUTPUT z;
END
Sep 27 '07 #6
On Wed, 26 Sep 2007 22:03:29 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Victor Smootbank wrote:
>I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???

What's the problem? Just keep doing what you've been doing.
Oh, are you confused by the case sensitivity? You can work
around it by defining macros like

#define GOTO goto

Trust me, it's not difficult to write spaghetti code in C++.
It's rather difficult to write *working* spaghetti code...

But he will have real problems trying to translate something of this
sort:

GOTO 1000*input_tax_coefficient

Yes, BASIC seems really powerful to dummies.

Zara
Sep 27 '07 #7
On Thu, 27 Sep 2007 07:57:28 +0200, "Alf P. Steinbach"
<al***@start.nowrote:
>* Zara:
>>
But he will have real problems trying to translate something of this
sort:

GOTO 1000*input_tax_coefficient

That's what the switch statement is for.
Only if none of the calculated gotos goes back in an spagetti way...
Sep 27 '07 #8
benben wrote:
Victor Smootbank wrote:
>I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???

Just adding to others' opinions here: there's no shortage of language
support for spaghetti code in C++. You really should take a look at the
preprocessor and try to write a whole program in macros!!

For example:

#define SUBPROGRAM(x, ret) (ret) (x)(void)
This results in SUBPROGRAM(main, int) expanding to:
(int) (main)(void)
Is this legal? I would probably have written
#define SUBPROGRAM(x, ret) ret x(void)
#define BEGIN {
#define END }
#define OUTPUT std::cout<<
#define INPUT std::cin>>
#define DIM int
#define LET
>

#include <iostream// anyone knows how to write this line in macro?

SUBPROGRAM(main, int)
BEGIN
INPUT x;
INPUT y;
int z = x + y;
DIM z;
LET z = x + y;
OUTPUT z;
END

--
Philip Potter pgp <atdoc.ic.ac.uk
Sep 27 '07 #9
On Sep 27, 4:03 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Victor Smootbank wrote:
I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.
Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.
How can I write spaghetti code in C++???
What's the problem? Just keep doing what you've been doing.
Oh, are you confused by the case sensitivity? You can work
around it by defining macros like
#define GOTO goto
Trust me, it's not difficult to write spaghetti code in C++.
It's rather difficult to write *working* spaghetti code...
In any language, no? :-)

(I remember one C program I had to make work: 2000 lines in
main, and no other functions. I threw it out, and wrote the
same thing in 180 lines, in one fourteen hour sitting. Got it
right first go---no errors. The original programmer had spent
close to a month on his version, before leaving on vacation, and
it still didn't work correctly.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 27 '07 #10
On Sep 27, 5:23 am, "Phlip" <phlip...@yahoo.comwrote:
How can I write spaghetti code in C++???
You should go back to classes.....
The most tangled spaghetti possible in C++ is templates.
Without a single goto, you can weave miles of infinitely
tangled control-flow.
And I've seen some pretty impressive spaghetti inheritance in the
past as well. Twenty layers deep, with multiple inheritance and
virtual inheritance all over the place.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 27 '07 #11
James Kanze wrote:
(I remember one C program I had to make work: 2000 lines in main, and no
other functions. I threw it out, and wrote the same thing in 180 lines,
in one fourteen hour sitting. Got it right first go---no errors. The
original programmer had spent close to a month on his version, before
leaving on vacation, and it still didn't work correctly.)
The last time I did that, I mechanically converted some pitiful Basic to C,
then made it part of my test rig. My new code had to match - artifact by
artifact - the old output:

http://flea.sourceforge.net/

I call this "Extract Algorithm Refactor".

--
Phlip
Sep 27 '07 #12
On Thu, 27 Sep 2007 07:50:00 +0200, Zara wrote:

>
But he will have real problems trying to translate something of this
sort:

GOTO 1000*input_tax_coefficient

Yes, BASIC seems really powerful to dummies.

I think someone needs to clarify which BASIC because IIRC, BASIC
originally required the target of a GOTO to be a line number constant.
Expressions were not allowed...at least in the DEC BASIC+ I used most
frequently and while I vaguely remember at least one BASIC dialect that
allowed non-constant GOTOS, I thought they were in the minority.

"Real programmers like FORTRAN because you can write self modifying code
using arrays and EQUIVALENCE" -- paraphrased from Real Programmers Dont
use PASCAL, Datamation, July 1983.

Sep 27 '07 #13
Victor Smootbank wrote:
How can I write spaghetti code in C++???
That's easy: Write your entire program inside the main() function.
The larger the program, the better. Use labels generously and jump to
them with goto whenever you can.
Gosub can be a bit tough, since C++ has no direct support for it.
However, emulating gosub is a great chance of making your code even more
spaghetti'ish than it already is. You can emulate gosub, for example,
like this:

int ReturnLocation = 1;
goto subroutine;
ReturnLocation1:
// more code here...

int ReturnLocation = 2;
goto subroutine;
ReturnLocation2:
// even more code here...
// etc...

subroutine:
// the subroutine code here...
if(ReturnLocation == 1) goto ReturnLocation1;
if(ReturnLocation == 2) goto ReturnLocation2;
if(ReturnLocation == 3) goto ReturnLocation3;
// etc...

What an exciting opportunity at spaghetti coding!
Sep 29 '07 #14

"Victor Smootbank" <me**********@yahoo.comwrote in message
news:11*********************@19g2000hsx.googlegrou ps.com...
How can I write spaghetti code in C++???
You might start here: http://en.wikipedia.org/wiki/Duff%27s_device
Sep 30 '07 #15
On Sat, 29 Sep 2007 10:32:11 +0300, Juha Nieminen wrote:
Gosub can be a bit tough, since C++ has no direct support for it.
However, emulating gosub is a great chance of making your code even more
spaghetti'ish than it already is. You can emulate gosub, for example,
like this:
Under GCC, emulating GOSUB is a lot easier, because of
the scoped __label__ extension, the && operator that
takes an address of a label, and the goto * command.

#include <list>
#include <stdio.h>

std::list<void*GosubStack;
void B_ERROR(int e) { throw e; } // for reporting errors...

#define B_GOSUB(label) \
{ __label__ gosub_return; GosubStack.push_back(&&gosub_return); \
goto label; gosub_return:; }
#define B_RETURN() \
{ if(GosubStack.empty()) B_ERROR(2);/* RETURN WITHOUT GOSUB*/ \
else { void* ret = GosubStack.back(); GosubStack.pop_back(); \
goto *ret; } }
#define B_RETURN_TO(label) \
{ if(GosubStack.empty()) B_ERROR(2);/* RETURN WITHOUT GOSUB*/ \
else { GosubStack.pop_back(); goto label; } }

int main()
{
B_GOSUB(root);
puts("0");
goto owa;
root:
puts("1");
B_GOSUB(beg);
puts("2");
beg:
puts("3");
B_GOSUB(wan);
puts("4");
B_GOSUB(too);
puts("5");
wan:
puts("6");
B_RETURN();
too:
puts("7");
B_RETURN_TO(wan);
owa:
return 0;
}

Obviously, this program outputs 1 3 6 4 7 6 2 3 6 4 7 6 0.
What an exciting opportunity at spaghetti coding!
That, it is.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 1 '07 #16
Joel Yliluoma wrote:
On Sat, 29 Sep 2007 10:32:11 +0300, Juha Nieminen wrote:
> Gosub can be a bit tough, since C++ has no direct support for it.
However, emulating gosub is a great chance of making your code even more
spaghetti'ish than it already is. You can emulate gosub, for example,
like this:

Under GCC, emulating GOSUB is a lot easier, because of
the scoped __label__ extension, the && operator that
takes an address of a label, and the goto * command.

#include <list>
#include <stdio.h>

std::list<void*GosubStack;
void B_ERROR(int e) { throw e; } // for reporting errors...

#define B_GOSUB(label) \
{ __label__ gosub_return; GosubStack.push_back(&&gosub_return); \
goto label; gosub_return:; }
#define B_RETURN() \
{ if(GosubStack.empty()) B_ERROR(2);/* RETURN WITHOUT GOSUB*/ \
else { void* ret = GosubStack.back(); GosubStack.pop_back(); \
goto *ret; } }
#define B_RETURN_TO(label) \
{ if(GosubStack.empty()) B_ERROR(2);/* RETURN WITHOUT GOSUB*/ \
else { GosubStack.pop_back(); goto label; } }

int main()
{
B_GOSUB(root);
puts("0");
goto owa;
root:
puts("1");
B_GOSUB(beg);
puts("2");
beg:
puts("3");
B_GOSUB(wan);
puts("4");
B_GOSUB(too);
puts("5");
wan:
puts("6");
B_RETURN();
too:
puts("7");
B_RETURN_TO(wan);
owa:
return 0;
}

Obviously, this program outputs 1 3 6 4 7 6 2 3 6 4 7 6 0.
Except it doesn't. It outputs:
1
3
6
4
7
6
2
3
6
4
7
6

[No trailing 0].

(What idiot came up with B_RETURN_TO? When would it possibly be useful?)

--
Philip Potter pgp <atdoc.ic.ac.uk
Oct 1 '07 #17
Kai-Uwe Bux wrote:
Victor Smootbank wrote:
>I did program in BASIC for more than 20 years and for me
it's still the ultimate programming language.

Unfortunately, I was forced to upgrade to C++ and now I'm
confused. While programming in BASIC, my favourites were
GOTOs and sometimes GOSUBs and spaghetti code was
my trademark.

How can I write spaghetti code in C++???

Relax, C++ has goto. You should also have a look into try-throw-catch. That
is a goto on steroids that allows you to pass a value to the target of the
jump (it only works jumping up the call stack, though).
It is possible to emulate GOSUB/RETURN using setjmp/longjmp,
and a stack.

If you can't figure it out, you are not worthy of
writing spaghetti C or C++ code!

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Oct 1 '07 #18
On Mon, 01 Oct 2007 17:23:48 +0100, Philip Potter wrote:
>Obviously, this program outputs 1 3 6 4 7 6 2 3 6 4 7 6 0.

Except it doesn't. It outputs:
1
3
6
4
7
6
2
3
6
4
7
6

[No trailing 0].
Weird, it does output the trailing zero for me.

(What idiot came up with B_RETURN_TO? When would it possibly be useful?)
Spaghetti code is not spaghetti code without irregular execution paths.
B_RETURN_TO of course models the "RETURN <label>" statement in BASIC.
One such use for it would be for example, in a game where "GOSUB gamemain"
could be matched with a "RETURN" that asks for a new game and "RETURN endgame"
that cleans up and terminates.
It is a crude mechanism resembling function return values in languages
such as C++.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 2 '07 #19
Joel Yliluoma wrote:
On Mon, 01 Oct 2007 17:23:48 +0100, Philip Potter wrote:
>>Obviously, this program outputs 1 3 6 4 7 6 2 3 6 4 7 6 0.
Except it doesn't. It outputs:
1
3
6
4
7
6
2
3
6
4
7
6

[No trailing 0].

Weird, it does output the trailing zero for me.
Argh! I thought you were confusing the 'return 0;' with 'puts("0")'; I
didn't notice the puts("0") at the top which, confusingly, is the last
puts() called!
>(What idiot came up with B_RETURN_TO? When would it possibly be useful?)

Spaghetti code is not spaghetti code without irregular execution paths.
B_RETURN_TO of course models the "RETURN <label>" statement in BASIC.
It doesn't exist in BBC BASIC, which is the variant I'm familiar with.
One such use for it would be for example, in a game where "GOSUB gamemain"
could be matched with a "RETURN" that asks for a new game and "RETURN endgame"
that cleans up and terminates.
It is a crude mechanism resembling function return values in languages
such as C++.
Oh. That kinda sorta makes some sense...

--
Philip Potter pgp <atdoc.ic.ac.uk
Oct 2 '07 #20
Philip Potter wrote:
Argh! I thought you were confusing the 'return 0;' with 'puts("0")'; I
didn't notice the puts("0") at the top which, confusingly, is the last
puts() called!
That's what makes it such beautiful spaghetti code... :P
Oct 2 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Sullivan | last post by:
I want to use this algorithm but can't figure it out, I never used BASIC. I tried translating it but got lost in the gosubs and "for i - 1 to ... nexts". I have figured out that FNU(X) is degree...
7
by: CoreyWhite | last post by:
You know what I have to say to the giant spaghetti monster? Do you want to know? Do you? Well I'm tired of sucking on your spaghetti and chewing on your fresh meat balls! It isn't only about...
0
by: CoreyWhite | last post by:
In any kind of communication and language, even in computer programming. We can see a difference between total spaghetti and celtic knot work. But it is easy to confuse the two, because sometimes...
40
by: Campy Happer | last post by:
Hello all, I would like to bring to your attention a concept and a term for it that I think is long overdue. But first: In the bad old days of programming, before subroutines were even...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.