align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

Re: [orlandophp] Writing Professional PHP Code

From: Jorge C.
Sent on: Sunday, November 4, 2012 1:14 AM
I highly recommend the book PHP Objects, Patterns, and Practice. It talks about "separation of concerns", design patterns, code standards like PEAR. I keep coming back to this book for design patterns specifically in PHP. http://amzn.to/PuPRbz


A couple of things I've learned myself during the course of 6 years:
  • COMMENT, COMMENT, COMMENT, but don't overdo it. You should only need to comment in places that aren't very clear why you wrote a statement a certain way.
  • Write descriptive variables and functions. You could almost write your code in English sentences and it'll make sense:
    • $people, not $array1
    • $isValid  instead of $x
    • $bigGroups instead $bgrpArray
    • findAllStatusesWhere() instead of find() if your routine is searching for statuses by some condition, for example. Now if you're context already gives more information, like for instance your class is Status, then your method could be called findAllWhere() or even findWhere. Status::findAllWhere( array('created' => '> today') );
  • Write PHP DocBlocks with correct info. Most, if not all IDEs can automatically write them for you.
  • Be consistent with your variable, function, and class naming. Don't write half of your variables in a function in underscore and half in camel case. Use either or. (I have to keep reminding myself) This could especially happen when you're writing in different contexts like your javascript and database columns are in underscores, but you're PHP is in camel case.
  • DON'T DUPLICATE CODE PERIOD. It's easy the first time, but can definitely introduce bugs later and plus it's just lazy and messy. Place what's common in a different function, and leave what makes the function unique in that same function. Basically least common denominator.
  • If your function has more than one responsibility it's time to break it up into multiple functions each with only one responsibility. Same goes for classes, but use your best judgement.
  • Be consistent with your whitespace, like returns. You shouldn't need more than one empty line between functions, class properties, etc.
  • Get to learn as many native php functions as possible. This will keep you from reinventing the wheel and keeping your code more concise. Many times there's multiple functions you could use to solve the same problem. At least know their names and what you can use them for. You can look up what exact parameters you need later in the PHP manual or your IDE. Also read the comments in the PHP manual for each function for some interesting insights that you could use in your own code.
  • Write as many lines necessary in a routine, but always think about doing it in the least amount of lines possible without sacrificing readability and performance.
  • Don't leave commented dead code inside of your files. If you used it before and now your not using it DELETE IT. Sometimes you may need to leave commented code in your development stage, but when it's ready to go live remove it.
  • It's nice to abstract things and keep things loosely coupled, but don't forget that if you overdo it, you actually make more complicated code that requires more lines of code to do simple things. More complexity means more points to break, which means more bug fixing, and slowing down your velocity as a programmer. Be pragmatic, not theoretical. *cough Zend Framework cough cough*
  • Please don't litter your functions with HTML, CSS, and Javascript and turn your code into spaghetti. Pull them out and put them in their own files or at least pull them out of a string for starters. If your HTML structure changes significantly and you have HTML littered in your business logic throughout several files, well get ready to pull out a bottle of Advil.
  • Learn where and when to use design patterns. Don't force one "just because", it'll complicate things and make things unnatural. You should be able to articulate why you need to use a specific pattern in a certain situation.
  • Like everyone else says just code, and see where you could improve things line by line. The more lines of code your write while keeping the aforementioned in mind plus other best practices, eventually you'll get better. Looks back at your old code you wrote a few years back and point out what was wrong with it so you can learn and not make the same mistakes.

Regards,

Jorge

Director of Web Development/Owner
2UP Media
c:[masked]
[address removed]


On Nov 3, 2012, at 10:34 AM, Derek Gallo wrote:

That is the main point in my opinion... Experience. Through experience from writing and writing and writing more code you pick up techniques and style along the way. After 10+ years of programming I still find myself changing my style as I learn new things. 

Sent from my NFiPhone
Please excuse any typos. 

Derek Gallo
Managing Director R&D
MemberFuse

On Nov 3, 2012, at 7:18 AM, William Crawford <[address removed]> wrote:

Hopefully David will reply with some tips as well.  I learned more about writing clean, readable code while I worked with him than I learned in the entire rest of my career.  I don't know if he can condense months of tips into an email, but I hope he'll try!

On Fri, Nov 2, 2012 at 11:39 PM, Emerson <[address removed]> wrote:
Hello Everybody,

I was looking at some PHP code from frameworks like CodeIgniter, CakePHP. Their code looks so succinct  so elegant and so professional. I'm trying to see if I can write PHP code like that. (I'm not talking about OOP vs. functional)

Is there anything that you'd recommend besides the advice of "keep reading their code" ? How does one learn to write PHP in such way?

Thank you for pointers!

-Emerson 




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Emerson ([address removed]) from The Orlando PHP User Group.
To learn more about Emerson, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by William Crawford ([address removed]) from The Orlando PHP User Group.
To learn more about William Crawford, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Derek Gallo ([address removed]) from The Orlando PHP User Group.
To learn more about Derek Gallo, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy