FrogWarrior Posted September 19, 2013 Share Posted September 19, 2013 I'm not a professional programmer (I plan on becoming one though) so I'm only learning to design PHP applications. I've made a few applications and a problem I keep running into is that when they get big, they get really confusing and difficult to work with, so when I need to fix something or alter something I waste ages just trying to debug and figure out exactly what does what. I started using classes but they haven't made anything easier for me, so I'm guessing I don't know how to use them properly. Heres an example of a little script I made for personal use recently: http://pastebin.com/FC16LGta I omitted most of the code and just left the structure. This is my latest script, so its the best attempt I've made so far to make it easy to manage, but I've already run into trouble with it. Quote Link to comment https://forums.phpfreaks.com/topic/282292-what-to-do-when-your-code-gets-messy-and-confusing/ Share on other sites More sharing options...
ignace Posted September 19, 2013 Share Posted September 19, 2013 (edited) It's a problem many programmers face and this is due to the fact that you don't put any thought into your application before starting to program. To get better at this you need to break your application down into parts you know how to do and parts you don't know how to do. Start with the parts you know how to do, think of how you will achieve them. Then take the one of the parts you don't know how to do and research it, until you have a better understanding of it, and see if you can break it down further again in a part you know how to do and the part you don't know how to do. To take your example: "GETS A LIST OF PDF FILES IN A DIRECTORY AND LOADS THE FILENAMES INTO AN XML DOCUMENT" "CHECKS THE URL VARIABLES, AND DECIDES WHETHER TO RUN THE SPIDER, OR DISPLAY THE XML FILE" "THE OPTIONS LIST WHERE THE USER CAN CHANGE THE WAY THE APPLICATION RUNS" These are just comments in your script, but added together they don't make much sense. Because you just started to hack away and none of the classes in your script have a real goal. From what I deduced from your code I gather that you want a script that checks an url variable then scrapes or displays an xml. When you say it like that it sounds so simple, right? So, thus summarize for yourself what you want to achieve. /** * Crawls an URL endpoint and returns a list of PDF files. * When $crawl_next_pages is true will crawl paginated content. */ function spider_crawl($url, $crawl_next_pages = false) { .. } /** * Given a list of PDF files generates a standard XML file. */ function xml_generate($files, $filename = null) { .. } if ($_GET['action'] == 'scape') { $pdf_files = spider_crawl($_GET['url']); $xml_path = xml_generate($pdf_files); echo 'XML generated.'; } else { readfile($_GET['xml_path']); }When you have all pieces of the puzzle, you may want to consider what your best plan of attack is, like what functions/classes will you have, what will their responsibilities be. Where do you need/want flexibility. Edited September 19, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/282292-what-to-do-when-your-code-gets-messy-and-confusing/#findComment-1450305 Share on other sites More sharing options...
adoado Posted October 18, 2013 Share Posted October 18, 2013 This happens to everyone every now and then, and usually it's because the underlying design pattern is a wrong fit. There are many potential candidates to make a small website, but if it changes a lot or is modular, other patterns look more favorable. If it is "widgetized" then it's different again. If you have a lot of design and you want to separate it then MVC is king. The problem is, when the project is small, it's less obvious because all appear good - this is really a matter of planning and experience, luckily both of which are obtainable I'd spend time reading up on design patterns, and understand concepts like code coupling and dependencies. Why is your code hard to manage - is it too tightly bound, or are the naming conventions wrong? Is the logic spread all over the place, or is it too hard to talk to the database? These are all valid questions, hell, even "I have to change my code too much" is a warning of a design pattern which is not completely suitable. Object oriented programming is a big tool, a God among development paradigms. Make sure you know OOP, beyond just standard "classes". Polymorphism, encapsulation, abstraction, etc. Quote Link to comment https://forums.phpfreaks.com/topic/282292-what-to-do-when-your-code-gets-messy-and-confusing/#findComment-1454389 Share on other sites More sharing options...
ignace Posted October 18, 2013 Share Posted October 18, 2013 (edited) Object oriented programming is a big tool, a God among development paradigms. Hall of Famer is this you? When the project is small, it does not matter which pattern you use, or if you use patterns at all (and I am not saying you should), since the codebase is small and change is easy. And no matter the size of the project, change is a constant in every project. So you won't get it right the first time, therefor it is best you simply build the simplest thing that could possibly work as to not make it harder on yourself when you got it wrong, and you will. Iteratively refactor your code as your domain knowledge of the project evolves. Edited October 18, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/282292-what-to-do-when-your-code-gets-messy-and-confusing/#findComment-1454394 Share on other sites More sharing options...
adoado Posted October 18, 2013 Share Posted October 18, 2013 Hall of Famer is this you? Nope, this is Adrian Iteratively refactor your code as your domain knowledge of the project evolves. Well said, that hits this question on the head I think! Quote Link to comment https://forums.phpfreaks.com/topic/282292-what-to-do-when-your-code-gets-messy-and-confusing/#findComment-1454413 Share on other sites More sharing options...
Adam Posted October 20, 2013 Share Posted October 20, 2013 Worth pointing out that decent frameworks can teach you a trick or two about how to structure your applications. Laravel is a good one, and relatively easy to learn too compared to something like Symfony's framework. I would learn as much as you can from them, while helping you build applications quick, if you're going to use one. Quote Link to comment https://forums.phpfreaks.com/topic/282292-what-to-do-when-your-code-gets-messy-and-confusing/#findComment-1454573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.