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

V

Victor Smootbank

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++???
 
V

Victor Bazarov

Victor said:
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
 
K

Kai-Uwe Bux

Victor said:
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
 
S

Sjouke Burry

Victor said:
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.....
 
P

Phlip

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.
 
B

benben

Victor said:
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
 
Z

Zara

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
 
A

Alf P. Steinbach

* 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.

Cheers, & hth.,

- Alf
 
A

Alf P. Steinbach

* Zara:
Only if none of the calculated gotos goes back in an spagetti way...

#define GOTO( n ) { label = n; goto dispatch; }

int main()
{
int label = 100;
int const taxCoefficient = 5;
dispatch:
switch( label )
{
100: { GOTO( 160 ); }
110: {} // Nothing to see here, move on, move on!
120: { return; }
130: { GOTO( 1000*taxCoefficient ); }
140: {} // Nothing to see here, move on, move on!
150: {} // Nothing to see here, move on, move on!
160: { GOTO( 130 );
5000: { GOTO 120; }
}
}

Cheers, & hth.,

- Alf
 
P

Philip Potter

benben said:
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;
 
J

James Kanze

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.)
 
J

James Kanze

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.
 
P

Phlip

James said:
(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".
 
J

Just me

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.
 
J

Juha Nieminen

Victor said:
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!
 
J

Joel Yliluoma

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.
 
P

Philip Potter

Joel said:
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?)
 
K

Kevin Handy

Kai-Uwe Bux said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top