|
My signature line says it all.
If you think 'goto' is evil, try writing an Assembly program without JMP.
|
|
|
|
|
If I'm not sure will compiler will understand goto's replacement better, I prefer goto (never trust any compiler).
|
|
|
|
|
|
Anyone who has been around for a long time may remember that the GOTO-less programming controversy was laid to rest (in FORTRAN anyway) by an article published in the December 1973 issue of Datamation magazine. I kept a copy of that article all these years in my humour file as it proposed solving the problem by eliminating GOTO statements entirely and replacing them with the COME FROM statement. Here is an excerpt from the article entitled "A Linguistic Contribution to GOTO-less Programming" (Thanks to the miracle of the internet, the full article can now be found online at this link: http://www.fortran.com/come_from.html )
---------------------------------------------------------
"This statement causes control to be
transferred to the next statement (the
statement immediately following the
COME FROM) upon completion of the
designated statement.
Example:
10 J = 1
11 COME FROM 20
12 WRITE (6,40) J
STOP
13 COME FROM 10
20 J = J+2
40 FORMAT (I4)
Explanation:
In this example, J is set to I by state-
ment 10. Statement 13 then causes control to
be passed to statement 20, which sets J to 3.
Statement 11 then causes control to be passed
to statement 12, which writes the current value
of J. The STOP statement then terminates the
program."
----------------------------------------------
In all seriousness, I have not had to use a goto statement in all the code I have written since the day that structured FORTRAN replaced FORTRAN IV on our mainframes in the early 80's. And I am including all the languages I have since used up until today: C, C++, PL/1, Java, C#, VB, Lisp, APL, and JavaScript. It is almost always possible to refactor logic that appears to require a goto into an equivalent form that doesn't. If your nesting is too deep, refactor into shorter, more succinct, and readable methods. Modern IDE's make refactoring dead easy and I have made extensive use of refactoring support in Eclipse as well as Visual Studio.
But for any fans of computer history, do yourself a favour and read the entire article at the link above.
Apologies for the Canadian (ie. British) spelling of 'humour' and 'favour'. I know they are somewhat anachronistic today, but that was how I was taught.
|
|
|
|
|
Ok, I lied...I just checked some of my FORTRAN source code written in 1985 and there were a handful of goto statements in 20,000 lines of code. But since FORTRAN, I have not used goto's (to the best of my knowledge
|
|
|
|
|
Use a real language
Just as a reminder BASIC = BEGINNERS All Purpose Instruction Code
|
|
|
|
|
... stuff like this:
switch (Session["Benutzer_Firma"].ToString())
{
case "ABC":
lblManipulation.Text = "Some text in a label";
break;
case "DEF":
lblManipulation.Text = "Some other text in a label";
break;
case "Default":
lblManipulation.Text = "The default text for the label";
break;
default:
goto case "Default";
break;
}
I just found this in a 9000 line ASP.Net page and the rest of it is not a bit better than this. The only things I have changed are the literal strings because the company was mentioned there several times. Especially the lines with the switch statement and the default case are inspiring.
And am I too harsh if I want to have the 'developer' thrown out of the guild, tarred, feathered and then chased out of the city?
I'm invincible, I can't be vinced
|
|
|
|
|
CDP1802 wrote: And am I too harsh if I want to have the 'developer' thrown out of the guild, tarred, feathered and then chased out of the city?
You missed the troll kick to the nadgers.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Well, the did not do any such thing to him. He is now in charge of quality assurance. And this is not a joke.
I'm invincible, I can't be vinced
|
|
|
|
|
|
|
At least this developer understands "how" to use switch statement syntax. I have had the experience of working on code where the developer clearly didn't understand this construct, or simple loops for that matter. It was commonplace to see the good old if...else approach instead of switch statements where they could be applied.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Wow. You actually found something the good man did right. But believe me, it's of little help. You can see by the 9000 lines in one single .aspx what kind of redundant spaghetti code he likes to produce. He practically tries to do everything with strings (why do they always have to do that?). All the code is written as if nothing can ever go wrong. When one of the abundant exceptions is thrown, it usually is not handled in any way. Here and there he uses try and catch, but only to sweep the errors under the rug and go on as if nothing has happened. This man has never heard of OOP and he uses only static methods, probably because he also never heard anything about classes and instances. Please believe me, the whole thing is a chaotic unmaintainable mess.
I'm invincible, I can't be vinced
|
|
|
|
|
CDP1802 wrote: Here and there he uses try and catch, but only to sweep the errors under the rug and go on as if nothing has happened. This man has never heard of OOP and he uses only static methods, probably because he also never heard anything about classes and instances.
I'm thinking we know the same developer here.
I figure that if you are a professional developer for more than 1 year you will run into code like this and know a developer who likes to make spaghetti.
CDP1802 wrote: Please believe me, the whole thing is a chaotic unmaintainable mess.
You're preaching to the choir here. I totally agree that it would be.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Our intern is a beginner. He makes mistakes, but he also is a smart young guy and really interested in learning. He makes progress and one day will be a good man in any team. I hope he does not read this
The fellow who wrote this stuff here has produced several websites, one more horrible than the next. There is no sign of learning at all. The first application of his we replaced was so bad that we wanted to post the complete source of it in this forum or publish it as a book (with critical annotations). In a way I pity him. Having to work without obviously knowing what he was doing and having to deliver something that at least appears to work on time must have been a nightmare. And every time I have to keep his stuff working without getting to replace it altogether I swear to drown him in the river should he ever cross my path.
I'm invincible, I can't be vinced
|
|
|
|
|
I got chewed out by a boss once, who said it's impossible not to write spaghetti code. So we should always write spaghetti code, and write the best spaghetti code we can.
I also got a bad review for the following:
1. Too many classes in my code.
2. Too much code.
3. The code is too structured.
Toward the end of my employment there, he made a rule that no bug fix, or new feature could go over three lines of code...
|
|
|
|
|
I hate when the boss is a complete idiot
|
|
|
|
|
What's so bad about if/else? I actually prefer using it in place of switch, just because switch is so ugly - you need a "break" after each case's statements? Seriously? I suppose it's not so bad in VB, where you're already using "end" statements instead of curly braces, but in C#, it just looks bad...
|
|
|
|
|
Only 9000 lines of switch cases?
I once found a database update function in a client-server app that had >10000 lines of if/else statements containing hardcoded table and database fieldnames, and field types. (I was supposed to fix it so it would reflect a recent change of the database scheme).
The person responsible for that later got hired by a head hunter. No kidding!
|
|
|
|
|
Stefan_Lang wrote: Only 9000 lines of switch cases?
No, that would just be boring. The switch was just a small sample from that spaghetti bowl. There is a Save() method, 1000 lines long, where he cobbles together a dataset. He begins by selecting some data from each table and then throwing away the results of his queries to get a DataSet with empty DataTables. Then he adds or removes some columns here and there and starts filling the newly configured DataSet with new rows and then finally tweaking the DataSet into storing this mess. At times I think this guy was a saboteur who deliberately tried to write the worst possible code. I could not come up with anything like that even if I wanted to.
I'm invincible, I can't be vinced
|
|
|
|
|
Adding and removing COLUMNS in a dataset while attempting to SAVE? Dear God, is he running ALTER TABLE statements in a save routine? Or just removing columns he doesn't actually want to persist?
|
|
|
|
|
Perhaps that's perfectly normal on some obscure planet where he comes from.
It rally truned out that he casually binds the dataset to a report before saving, so that you can print that out. The columns are added or removed according to the user's role and are perhaps added again after opening the report. Ahh, yes, he had to fill the some values into the newly added columns. He only does that in the first rows of the tables and ignores all others. That's also a common pattern in one of his other creations. He always assumes that datatables are filled with exactly one row, no more, no less. Anyway, he must have discovered that he needed an XML schema of the dataset, so he simply saves it after finishing his manipulation. Great idea. This way the schema is always accurate, no matter which route we took through the spaghetti code.
My best guess is that the report was added later and that he needed two or three slightly different reports, depending on the user's role. Anybody exept him would have clicked together a schema, created a typed dataset from it and added different queries to fill it for the different roles. Filling and binding it would have been something around 15 lines of code.
But don't worry too much about the database. Not a single table had a primary key and of course no foreign keys or other constraints. Why should he need them since he apparently believes that his queries always return exactly one row? I would not put it beyond him to simply reverse his manipulations in the dataset before saving. I honestly will not look if I don't have to.
I'm invincible, I can't be vinced
|
|
|
|
|
Behold!
I found something like this:
protected void Page_Load(object sender, EventArgs e)
{
List<Node> Stuff = loadStuff();
}
private List<Node> loadStuff()
{
List<Node> Stuff = new List<Node>();
someMoreStuff:
foreach (Node thing in Node.GetCurrent().Children)
{
Stuff.Add(thing);
}
Stuff = ShuffleNodeList(Stuff);
if (Stuff.Count < 100 && Stuff.Count > 0)
{
goto someMoreStuff;
}
return Stuff;
}
It worked... but I changed it so that it never happened:
protected void Page_Load(object sender, EventArgs e)
{
var Stuff = new List<Node>();
Stuff = loadStuff(Stuff);
}
private List<Node> loadStuff(List<Node> Stuff)
{
foreach (Node thing in Node.GetCurrent().Children)
{
Stuff.Add(thing);
}
Stuff = ShuffleNodeList(Stuff);
if (blablabla)
{
loadStuff(Stuff);
}
return Stuff;
}
Edit: So I changed it again...
protected void Page_Load(object sender, EventArgs e)
{
List<Node> Stuff = loadStuff();
}
private List<Node> loadStuff()
{
List<Node> Stuff = new List<Node>();
do {
foreach (Node thing in Node.GetCurrent().Children)
{
Stuff.Add(thing);
}
} while (Stuff.Count < 100 && Stuff.Count > 0))
return ShuffleNodeList(Stuff);
}
Giraffes are not real.
modified 21-Feb-12 11:13am.
|
|
|
|
|
Does C# do tail recursion natively? If not, there is an argument that the original is better, though it should be commented as such.
|
|
|
|
|
No idea... I assumed it was worse. Seen code of my senior colleagues using tail recursion too. (Monkey see, monkey do.)
Turns out it's not supported in C#; only in the CLR.
http://lookingsharp.wordpress.com/2010/03/08/tail-recursion-in-csharp-and-fsharp/[^]
The workaround looks interesting, but way too hardcore for the things the module is intended for. I will rarely need to loop, it's just to make sure it's flexible in case someone starts messing with the table while the site is live.
I wrote the first piece of code because I couldn't get it to work otherwise. It's pretty funny if it turns out to be better practice.
Giraffes are not real.
|
|
|
|