homepage Welcome to WebmasterWorld Guest from 54.234.147.84
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

This 108 message thread spans 4 pages: < < 108 ( 1 2 3 [4]     
Top 100 Signs That I Am Writing Spaghetti Code in PHP
Every one join in
ergophobe




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

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.

 

WhosAWhata




msg:1296130
 12:11 am on Feb 28, 2004 (gmt 0)

8.
you're reaad this whole thread and patted yourself on the back for only doing half of them

Timotheos




msg:1296131
 1:27 am on Feb 28, 2004 (gmt 0)

7. You post your code to WW looking for some answers and the responses run longer then the Google update threads.

Sorry I couldn't help myself ;-)

SlowMove




msg:1296132
 1:38 am on Feb 28, 2004 (gmt 0)

6. goto 5.

bedlam




msg:1296133
 1:43 am on Feb 28, 2004 (gmt 0)


5 GOSUB 100:REM In case we missed anything on the first pass...

ergophobe




msg:1296134
 9:03 pm on Feb 28, 2004 (gmt 0)


5 GOSUB 100:REM In case we missed anything on the first pass...

Especially since this is a "Spaghetti Code in PHP" thread ;-)

Four more and I'll post the entire 100. Then we can open a thread for each one and debate whether or not it's really bad and how high it should go on the list. I think Jatar_K has been getting too much sleep anyway.

Tom

slade7




msg:1296135
 3:44 pm on Feb 29, 2004 (gmt 0)

I'm speaking for myself here

4:
if ($status == "awake"){
$actitivity = "Writing spaghetti code";
}elseif($healthstatus == "OK"){
$activity = "Sleep";
}else{
print "CALL 911 IMMEDIATELY!";
}

ergophobe




msg:1296136
 4:42 pm on Feb 29, 2004 (gmt 0)

3. You work alone, but have to use CVS to keep track of your project.

For the record, I don't *have* to. I just do. So there.

[edited by: ergophobe at 4:48 pm (utc) on Feb. 29, 2004]

ergophobe




msg:1296137
 4:44 pm on Feb 29, 2004 (gmt 0)

2. You took over a project that was so convoluted you had to just scrap it and start over. Now, after three weeks, you find you have the exact same directory structure as the original and most filenames are the same.

ergophobe




msg:1296138
 4:44 pm on Feb 29, 2004 (gmt 0)

1. You've been watching this thread the entire time and haven't thought of a single thing you could add.

ergophobe




msg:1296139
 4:51 pm on Feb 29, 2004 (gmt 0)

[edit]I just noticed - this recap post is #100 in the Top 100 thread. Perfect

Complete Recap of the 100 List

I apologize for how long it took to post this recap. It took me quite a bit of testing with grep before I figured out the right regex to use in my class cl_Get_top_100_spaghettis_update, which of course extends cl_Get_top_100_spaghettis. Then it was all fouled up because I was already including top_100_spaghettis_new.php.inc in my script so I had to change it to require_once("top_100_spaghettis_new_2.php.inc"). I think I have it straightened out now. I don't think I could ever have gotten it fixed without my CRC cards though.

100 Signs that I May Be Writing Spaghetti Code

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.

97. Spelling 'referer' wrong everywhere so it matches the PHP $HTTP_REFERER syntax and then adding a comment everywhere it's used so everyone knows you did it on purpose.

96. Passing variables directly to 635 functions and using 'global' on 453 functions.

95. I have to modify the same function 10 times because I have not moved it into an include script...yet.

94. Finding out that the 2 libraries you need to include also happen to include each other

93. trying to edit html content on the index page and not being able to find the template until you go through 10 scripts (gotta love horde).

92. No comments.

91. You lose track of what include to use for what functions.

90. You edit an include and realize you've already written that function or something very similar...somewhere.

89. Opening and closing tags for an element like <p>, <table>, etc are in different files.

88. Can't find the include file anywhere in the folder I am examining.

87. Includes? What are includes? (This is a corollary to 91).

86. Everything looks the same, as in:
Top of page:
include "include.php";
include "include1.php":
include "include2.php":

85. I find that rewriting is quicker and easier than modifying the original design.

84. You have a library of small functions that you wrote yourself, and will cut'n'paste them into almost every project you work on... Whether they're needed or not...

82. I'm at that precipice where I suddenly realize that the whole application would be a lot better if I made a certain change - a fundamental change - one that would allow me to streamline my code, perhaps combine functions, and save me time and hassle later on. But it requires me to dump a day's (or more) work and because of a lack of time - I press on and ignore it.

81. Every time you want to change a variable you have to use grep to find it ;)

80. you have so many "for" loops you've declared a variable for each letter in the alphabet.

79. Dreamweaver starts to lag when you open the file.

78. You realize there is no possible way to unwind your cross included files and in your great wisdom you used include and require. You now have to replace all of them with include_once and require_once just to stop the contamination.

77. there are enough "if" statements to make you scroll to the right.

76. Your GET query string is so long it can crash a browser.

75. Your class object includes are stored on a single server and aliased on all your webservers...but you don't know which server those files are REALLY on anymore.

74. You curse at your co-worker's terrible logic just before you realise you wrote that code last week.

73. You see comments in the code that read something like:
Your Name: Don't ask me how or why this works, I know it looks overly complex and it appears there IS a simpler solution, but this works, it fixed a bug from the 'simpler code'...just leave it be...don't touch it, I've hired a voodoo witch doctor to place a curse on anybody who alters this code...trust me, just leave it alone!

72. Following on from 81 - you know you're digging through a bowl of the stuff when, as a regular tool, you're using the windoz's search feature to find functions, variables and other bits of code.

71. Following of from 80 and 77 - having to "peel" your way through while/for loops and a chunks of IF and ELSE statements because the original author (probably in all his haste) couldn't be bothered to use brackets. The code works but not always for the right reasons.

70. out of the 50 arrays used 49 are 3 dimensional.

69. You crash your SQL server more then 5 times while testing your queries

68. After uploading your scripts to the server you find that you've exceded your allotted bandwidth for the month

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

66. My enter key doesn't work, so I just put everything on one line, it works anyways.

65. [editor note: This one is not so much a sign, as an example. See Message #35].

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

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

62. You sit down, open your PHP script, and wonder where to begin with debugging... (ooh boy, I know the feeling of that!)

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.

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)

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

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

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.

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). [editor wants to change this to: 50. Your functions output HTML directly (either through switching out of PHP or via echo)].

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

48. The white board in your office is clean.

47. You've run out of meaningful variable names, and you are now reduced to using the names of family pets.

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

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.

41. While writing a new function, you are background thinking justifications for the last one you just wrote.

40: You surf over to WW to see how many queries per page others dare to use & scoff at the low number.

39: you plan your vacation using if-else complete with curly brackets.

38. You've worn paint off the "$" and "4" key on your keyboard.

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.

36. You create a separate class for every single function.

35. You have an include file called "stuff.inc"

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

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.

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.

31. You have no idea what a CRC Card is ...

31. You don't understand classes.

31. You can't count down from 100.

30. You know someone else is writing spaghetti code when.... you find
if (1==0) {
...
}

29. When you have more cases then a court house

28: reading this list you think.. "Hmmm what's so bad about that? Or that? Or that?

27. Infinitely cascading includes like:

index.php:
<?php include("menu.php");?>
...
menu.php:
<?php include("navigation.php");?>
...
navigation.php:
<?php include("newmenu.php");?>
...
newmenu.php:
<?php include("newmenu_final.php");?>
...
newmenu_final.php:
<?php include("menu_newer_version.php");?>
... etc

26. if your included file "stuff.inc" has become deprecated in favour of "newer-stuff.inc"

25. Parse error: parse error in /home/index.php on line 140
20 minutes later
Parse error: parse error in /home/index.php on line 141
20 minutes later
Parse error: parse error in /home/index.php on line 142

24. When the first line of your code is:
ini_set(max_execution_time,"99999");

23. And the last line of your codes is:
ini_set(max_execution_time,"30");

22. When you open the same db 11 times in the same file.

21. When you have to do your includes in a page being called as in mypage.php?page=somedullpage.

20. You include a file that includes the original file...
<!-- file1.php -->
<? include("file2.php"); //other stuff?>

<!-- file2.php -->
<? echo "stuff"; include("file1.php");?>

19. You have so many custom fucntions that you try to redifine the nl2br function

18. You have so many custom functions that the WhatDoTheseFunctionsDo.txt file is 20MB

17. Your kids understand the code...after college.

16. You have to grep for all occurences of include() or require() and replace them with include_once() and require_once() just to get your app to run.

15. You're still using extract, cause well, those security folks are all just anal anyways.

14. You need to write a Perl script to check your PHP code.

13. When your orignal project was to recreate a windows desktop application in php/html/javascript to run on windows desktops.

12.You start trying to recreat a windows app with php...and end up making another form to e-mail app.

11. You realise you have three functions with the exactly the same name, but which do completely different things, scattered across several includes.

10. When your control script forgets that it is handling the return from one of many scripts it calls and re initializes all the vars... and you don't figure it out for a month.

9. You have read through this whole thread so far and said to yourself "I don't do any of those" and patted yourself on the back.

8. You've read this whole thread and patted yourself on the back for only doing half of them.

7. You post your code to WW looking for some answers and the responses run longer then the Google update threads.

6. goto 5.

5. GOSUB 100:REM In case we missed anything on the first pass...

4.
if ($status == "awake"){
$actitivity = "Writing spaghetti code";
}elseif($healthstatus == "OK"){
$activity = "Sleep";
}else{
print "CALL 911 IMMEDIATELY!";
}

3. You work alone, but have to use CVS to keep track of your project.

2. You took over a project that was so convoluted you had to just scrap it and start over. Now, after three weeks, you find you have the exact same directory structure as the original and most filenames are the same.

1. You've been watching this thread the entire time and haven't thought of a single thing you could add.

[edited by: ergophobe at 5:01 pm (utc) on Feb. 29, 2004]

brotherhood of LAN




msg:1296140
 4:51 pm on Feb 29, 2004 (gmt 0)

>>Then we can open a thread for each one and debate whether or not it's really bad and how high it should go on the list

A little background explanation too, im wondering the main difference between include and include_once

//added
hehe bad timing, but still would be nice :)

ergophobe




msg:1296141
 5:03 pm on Feb 29, 2004 (gmt 0)

for ($i=0; $i<10; $i++) {
include("file1.php");
include_once("file2.php");
}

file1.php is included 10 times.
file2.php is included once.

While we're on it

$x = FALSE;
if ($x) {
include("file3.php");
require("file4.php");
}

file1.php is ignored.
file2.php is read and becomes part of the script.

jatar_k




msg:1296142
 7:50 pm on Feb 29, 2004 (gmt 0)

When I read it in one big list like that it makes me cry it's so funny. :)

ergophobe




msg:1296143
 10:57 pm on Feb 29, 2004 (gmt 0)

I don't know what I expected when I launched the thread, but I didn't expect so many entries to be funny. At least a half dozen made me laugh out loud when I read them the first time.

Tom

mikeb




msg:1296144
 12:06 pm on Mar 1, 2004 (gmt 0)

80. Afterwards

mikeb




msg:1296145
 12:11 pm on Mar 1, 2004 (gmt 0)

You read and reply to this thread only realising <? include(previous.thread) ;?> that it's finished. Doh!

ergophobe




msg:1296146
 5:00 pm on Mar 2, 2004 (gmt 0)

Okay, Mike, so make it Top 200. Whatever. In fact, having just finished, I thought of two that nobody mentioned

101. You need to have register_globals on to make your script run.

102. Your development server has error reporting set to E_ERROR.

Tom

ergophobe




msg:1296147
 12:52 am on Mar 24, 2004 (gmt 0)

I suppose this thread should die, but it's been one of those days where....

103. Everything I fix breaks something else.

This 108 message thread spans 4 pages: < < 108 ( 1 2 3 [4]
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved