views:

81

answers:

3

as web developer using PHP/JS/CSS for years , i suffer from repeat myself over and over and over and over , even if i use a PHP framework , i feel the same thing every time, i start to build new feature , so every time i started new thing i feel the 'Déjà vu' ( i write it before ) , and i rewrite many things from scratch despite i write something same before , if i use previous written code , i may take more time to reuse it , i love programming and web development , but by this way i will lose this :( ...

Where the problem ? in language , my way to built software or what ?

A: 

The problem is that when PHP started, every tutorial, every demo and all code was made in a very structured way, because PHP descended from the C/C++ family. Many developers took this problem and have carried it, and it was visible that we couldn't keep this.

Now, we have PHP 5.3.3 available, we have namespaces, objects, classes, interfaces and all of this. Many of the things that helpt making bad code like magic quotes and register globals are now deprecated. PHP is evolving into the OO world, it took long enough, but is happening. So if you want to start again to "relearn" PHP I would suggest getting your hands on some frameworks like:

symfony, joomla, propel, doctrine

learn about mvc and design patterns applied to PHP and you will see how you'll start making good OO code, and the deja-vus might stop before you knwo it.!

Hope I can help David

David Conde
well i learn MVC pattern and i implement small one for personal use , and already use codeigntier but ,still the same : repeat my self over and over :) , and i read alot about OOP , the same loop exists ...
shox
A: 

This is exactly why a framework is the best way to go. It takes out the mundane parts of code (the stuff you always have to write over and over like DB connections).

I would suggest picking a good framework (I use CakePHP, but there are others like Symphony, Codeignitor, Zend, etc.), and as you build various components, you can reuse them in other projects. It will reduce the time it takes to write all future projects.

cdburgess
+2  A: 

if i use previous written code , i may take more time to reuse it

This, I believe, is your real problem: you're writing way too much quick-and-dirty code and are suffering from the resulting techincal debt. The problem is, what is quick in the short term can lead to being very slow in the long term.

If I may offer a non-technical comparison, consider a Ferrari and a cargo ship. When delivering just one letter, it seems obvious that the Ferrari would be much faster not to mention much more convenient compared to using a cargo ship. Just loading the ship before leaving the docks can take hours. By which time the Ferrari would have already completed the delivery. But when you have to deliver a hundred tons of mail, the cargo ship would complete the delivery while the Ferrari would still be busy speeding back and forth delivering each letter.

It's the same in your case. When developing just a single web site, it will always seem obvious that you should write the least amount of the simplest possible code to get the job done quickly. And if your whole career is devoted to just one web site I would even say that that's the right way to do it. It is, after all, the essential spirit of Agile methods. But if your job is to develop websites (plural) then this approach will start to become slower with each new site. Just like how the Ferrari is not ideal for delivering a hundred tons of anything.

One real world example is 37signals. When their business model requires that they should be able to quickly develop and deploy sites as necessary, instead of just diving in and develop their first site in a quick-and-dirty way, they took a step back and developed Ruby on Rails instead.

I'm not saying that you should go ahead and start your own framework, re-inventing the wheel is exactly what you want to avoid at this point. What I'm suggesting is to separate the functionality you're developing from the web related code. In other words, when writing new code write it as a library. Yes, it's a bit more involved writing code in two or more files compared to inlining the functionality directly in the PHP page. But in the end your next web site would be easier/quicker to implement.

I always, as a matter of habbit, create a lib directory in a project's base directory whenever I start a new project. I then force myself to write all code even remotely reusable as separate modules in this directory. These days I find that, more often than not, I'd simply copy (or more commonly make a symlink) of files I need to the lib directory instead of writing the needed code.

slebetman