Jump to content


Photo

What to do when your code gets messy and confusing?


  • Please log in to reply
5 replies to this topic

#1 FrogWarrior

FrogWarrior

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 19 September 2013 - 02:26 PM

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.



#2 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,208 posts
  • LocationBelgium

Posted 19 September 2013 - 03:16 PM

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, 19 September 2013 - 03:20 PM.

  • TOA likes this

#3 adoado

adoado

    Member

  • Members
  • PipPip
  • 14 posts

Posted 17 October 2013 - 11:12 PM

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.



#4 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,208 posts
  • LocationBelgium

Posted 18 October 2013 - 12:45 AM

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, 18 October 2013 - 12:49 AM.


#5 adoado

adoado

    Member

  • Members
  • PipPip
  • 14 posts

Posted 18 October 2013 - 05:18 AM

Hall of Famer is this you?

 

Nope, this is Adrian :P

 

 

 

Iteratively refactor your code as your domain knowledge of the project evolves.

 

Well said, that hits this question on the head I think!



#6 Adam

Adam

    Advanced Member

  • Gurus
  • 5,680 posts
  • LocationSheffield / UK

Posted 19 October 2013 - 07:37 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com