Jump to content

What to do when your code gets messy and confusing?


FrogWarrior

Recommended Posts

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.

Link to comment
Share on other sites

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 by ignace
Link to comment
Share on other sites

  • 4 weeks later...

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.

Link to comment
Share on other sites

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 by ignace
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.