Welcome to WebmasterWorld Guest from 18.208.159.25

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Top 100 Signs That I Am Writing Spaghetti Code in PHP

Every one join in

     
8:18 pm on Feb 15, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8639
votes: 283


This got started a little off topic in another thread [webmasterworld.com]. I was curious to see what it might generate if it had it's own thread. I started with numbers 99 and 100. Jatar_K chimed in with number 98. Now let's see what we can come up with collectively.

And for those unfamiliar with the term, here's part of the definition from the Wikipedia:


Spaghetti code is a pejorative term for code with a complex and tangled control structure,

In other words it is inelegant, difficult to understand for anyone who takes over a project, difficult to maintain. It is likely to be inefficient, but may in fact be very efficient if only anyone could figure out how it worked.

Top 100 Signs That I Am Writing Spaghetti Code in PHP

100. I have no idea where this constant is defined.
99. I have echo stmts littered throughout my code.
98. There is an error but it isn't handled, and I can't find it to figure out what's wrong.

7:03 am on Feb 17, 2004 (gmt 0)

Junior Member

joined:Sept 9, 2003
posts:107
votes: 0


69. You crash your SQL server more then 5 times while testing your queries
7:05 am on Feb 17, 2004 (gmt 0)

Junior Member

joined:Sept 9, 2003
posts:107
votes: 0


68. After uploading your scripts to the server you find that you've exceded your allotted bandwidth for the month
3:29 pm on Feb 17, 2004 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


67. I use every imaginable variation of the word "foobar" in variable names

[edited by: httpwebwitch at 3:32 pm (utc) on Feb. 17, 2004]

3:31 pm on Feb 17, 2004 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


66. My enter key doesn't work, so I just put everything on one line, it works anyways
3:40 pm on Feb 17, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


Ok, I'm going to just paste a snippet of code from a project I took over (I've rewritten this properly, but...this was just too funny to delete completely)

65.


DO
if Request.Form("Action") = "CREATE" and strCreate = "Y" then
IF strPersonId <> "" then
for iLoop = 0 to UBound(astrSubTest)
if Request.Form(astrSubTest(iLoop) & "CodeID") <> "" then
if DBConnNextKey.Errors.Count > 0 then
for iLoop2 = 0 to DBConnNextKey.Errors.Count - 1
next
exit do
end if
if Request.Form(astrSubTest(iLoop) & "V") = "" then
else
end if
if DBConnU.Errors.Count > 0 then
exit for
end if
end if
next
if DBConnU.Errors.Count = 0 then
else
for iLoop2 = 0 to DBConnU.Errors.Count - 1
next
end if
End IF
end if
if Request.Form("Action") = "UPDATE" and strUpdate = "Y" then
IF strPersonId <> "" then
for iLoop = 0 to UBound(astrSubTest)
if Request.Form(astrSubTest(iLoop) & "ID") = "" then 'row does not exist
if Request.Form(astrSubTest(iLoop) & "CodeID") <> "" then 'Create
if DBConnNextKey.Errors.Count > 0 then
for iLoop2 = 0 to DBConnNextKey.Errors.Count - 1
next
exit do
end if
if Request.Form(astrSubTest(iLoop) & "V") = "" then
else
end if
end if
elseif Request.Form(astrSubTest(iLoop) & "CodeID") = "" then 'Delete
else 'Update
if Request.Form(astrSubTest(iLoop) & "V") = "" then
else
end if
end if
if strSQL <> "" then
end if
if DBConnU.Errors.Count > 0 then
exit for
end if
next
if DBConnU.Errors.Count = 0 then
else
for iLoop = 0 to DBConnU.Errors.Count - 1
next
end if
End IF
end if
if Request.Form("Action") = "DELETE" and strDelete = "Y" then
IF strPersonId <> "" then
for iLoop = 0 to UBound(astrSubTest)
if Request.Form(astrSubTest(iLoop) & "ID") <> "" then 'row exists
end if
next

if DBConnU.Errors.Count = 0 then
else
for iLoop = 0 to DBConnU.Errors.Count - 1
next
end if
End IF
end if
LOOP UNTIL TRUE

5:02 pm on Feb 17, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 10, 2001
posts:1551
votes: 10


64. You're not using Python instead. ;)

(sorry, couldn't resist)

5:05 pm on Feb 17, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 29, 2002
posts:981
votes: 1


63. Some functions work even though you know they shouldn't as you haven't finished them yet. Being unable to make them fail as expected, you are left with only one conclusion: your code has spontaneously become self-aware - and worse, it *knows* you want to shut it down...
5:56 pm on Feb 17, 2004 (gmt 0)

New User

10+ Year Member

joined:Feb 17, 2004
posts:13
votes: 0


62 - You sit down, open your PHP script, and wonder where to begin with debugging... (ooh boy, I know the feeling of that!)
6:52 pm on Feb 17, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8639
votes: 283


61. Your final page doesn't validate. You look at the HTML source and see the problem clearly, but you decide not to fix it because you have no idea where this output comes from.

In response to #81 (message 19, inspired by a tongue in cheek remark made by a certain someone in message 17):

When grep is outlawed, only outlaws will use grep

No seriously, sometimes I know within 20 lines where a variable is, but I still grep to get the exact line number or, if I really know what I want to do, just use grep and replace with the value I want. That way, all I do is type what I want into grep - don't have to open the file, find the line, change it, save the file, close. That and my code is such a pile of spaghetti that in truth I couldn't actually find anything without grep ;-)

Tom

11:28 pm on Feb 17, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


60. When you have to grep for a var from / because you have no idea where in your directory structure the file is, all you know is it is above the root of the site. (I've had to do this a few times)

I guess we're both outlaws ;)

12:15 am on Feb 18, 2004 (gmt 0)

New User

10+ Year Member

joined:Feb 17, 2004
posts:13
votes: 0


59 - When a simple HELLO WORLD takes up 40 lines of code...

(And this was an example in a book I borrowed from my library! Wish I still had it, but it made me laugh. If I had taken it seriously, I probably never would have attempted learning PHP...)

3:00 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:June 15, 2003
posts:93
votes: 1


58.

Nothing like throwing in a hierarchy of javascripts that are all dependent on each other. Something along the lines of "this script must be defined after variables hd,hm and hy are defined".

Then, while running the page, only to find that something was left out somewhere and the script runs every year from 1 to 2004. Got to figure out why the page is 200k in size?

Of course all that fancy javascript could have been handled just as well by a ~5 line "Ymd" mktime function ...

3:09 am on Feb 19, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


57. You find comments like this in your code:
This code, while convoluted and apparantly redundant fixes a bug in a M$ dll/function/object/etc.
8:41 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


56. When every time the boss and artist come walking through the door, you know you are going to have to change 50 lines of code.

55. You wrap it up and realize that there are 20 funtions in the include files which are never called, although at some point, they were all required.

54. The project scope continues to creep, but the timeline scope continues to deminish (don't care who you are, if you are keeping up with that, you are hacking dough)

53. You are using database structure changes in the middle of the project

52. In an attempt to same time, you are making system calls for OS functions.

51. Changing database is (e.g MS SQL to MySQL) requires the rewrite of SQL statments.

50. There is HTML in your functions. (this is a little debatable, but it is a watch sign for me).

8:45 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


Ha!

49. On the weekend you find yourself testing the effects of gzip on include files.

.. yes.. I've been there.. talk about solving the wrong problem.

8:53 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


48. The white board in your office is clean.
8:56 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


47. You've run out of meaningful variable names, and you are now reduced to using the names of family pets.
9:01 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


46. The ratio between the number of variable names, and the number of field names in the database, is 5 to 1

45. You realize that the only differance between three functions is the number of variables being passed.

44. You realize that you are changing global variables with functions inside include files

You are using GLOBAL VARIABLES! (Not sure if that one has already been stated, but it crossed my mind it probably has.. so no number here.)

9:19 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


43. Your SQL statments have SELECT * in them

42. You know that the algorythm you just created to check the data coming back from the database is a O((N2 -N)/2) .. but you just don't care anymore.

9:23 am on Feb 19, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


41. While writing a new function, you are background thinking justifications for the last one you just wrote.
9:59 pm on Feb 19, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8639
votes: 283


All right webadept! I thought the thread was stagnating and signed in today to see it is over half way to the 100 mark.


50. There is HTML in your functions. (this is a little debatable, but it is a watch sign for me).

I would change this to

50. Your functions output HTML directly (either through switching out of PHP or via echo).

I would say there are many situations where it makes sense to have a function *return* HTML. But then... I'm trying to approach all signs with an open mind... except of course Jatar's foolish and irresponsible comments about grep :-)

Tom

5:28 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:132
votes: 0


40: You surf over to WW to see how many queries per page others dare to use & scoff at the low number.
5:35 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 20, 2003
posts:132
votes: 0


39: you plan your vacation using if-else complete with curly brackets.
6:59 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


Nothing to it.. after 15 years of writing code, the first five hacking dough with some of the worst of them, I could probably finish this list myself ;=)
7:17 am on Feb 20, 2004 (gmt 0)

Junior Member

joined:Sept 9, 2003
posts:107
votes: 0


38. You've worn paint off the "$" and "4" key on your keyboard.
7:56 am on Feb 20, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


37

You are using classes, but create a "miscellaneous" class where you put all those odds and ends. Finally you realize the "misc" class has become your main class and needs to be included in every script.

...or...

36

You create a separate class for every single function.

10:33 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


35. You have an include file called "stuff.inc"
10:41 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


34. Your for loop pops a query and sends the results to a funtion, which runs a for loop across the array you sent with the results from the query, sending the 3, 5 and 10 cells to seperate funtions, and formats the htmml for the 1, 2, 4, 5, 6, 7, 9 values of the array, sending those to a function, which has a for loop, ...
10:59 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


33. You have come up with 5 blistering hot ideas for the program you are working on, which have taken you the last 3 weeks to bring to reality, only to find, now that they are all working, and they work even better than you could have imagined 3 weeks ago, they also block the original functionality of the program spec, and are in conflict with the main idea of the client.
11:31 am on Feb 20, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2004
posts:61
votes: 0


32. Your hands are bruised and cut from the shards of exploding classes after hours of trying to shove the client’s feature requests, into your well thought out architecture.
This 108 message thread spans 4 pages: 108