Current location: Hot Scripts Forums » Programming Languages » PHP » How to write 'elegant' code in php?


How to write 'elegant' code in php?

Reply
  #1 (permalink)  
Old 08-03-08, 05:26 AM
aditya2071990 aditya2071990 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to write 'elegant' code in php?

I mean, how can you possibly define elegance in an abstract programming language, of which you can see nothing but lines and lines of code? Is it just about the right code indentation? Or is there something deeper to it? I am very much a n00b at php, but I want to start out early at writing elegant code, which so many hackers and great programmers have expressed deep admiration...and also expressed their deepest distaste over n00bs who write 'crappy' code (what's crappy about it?)...I mean there isn't even a standard in php, like XHTML in HTML...so its all the more harder for me to understand....even a link to the right direction will be appreciated, folks...thanks in advance, Aditya.
Reply With Quote
  #2 (permalink)  
Old 08-03-08, 06:36 AM
Wolf1994 Wolf1994 is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
In my humble opinion, elegant code not one well-formed, but one well-optimized. For last one there is no strict rule, you need just practice and find the best way for each task.
Reply With Quote
  #3 (permalink)  
Old 08-03-08, 07:19 AM
leeprice2006 leeprice2006 is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
I don't think there is such a thing as elegant code. Just do it the best way for you and the best way to get the job done, which may not be the same way, so then you need to decide...
Reply With Quote
  #4 (permalink)  
Old 08-03-08, 08:58 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Characteristics of elegant or graceful code:
  • Easy to read, indented in a consistent manner, meaningful variable and function names, objects/functions well organized. Comments where needed.
  • Straightforward, no unnecessary complexity
  • Good architecture. No redundant/duplicate code, reuse of modules/files/functions as appropriate.
  • Separation of application and presentation. HTML not embedded in PHP, but stored in templates.
  • Robust field validation, graceful error handling
  • Secure, appropriate reactions to malicious or accidental actions.
  • Runs as intended
  • Common configuration settings/constants sourced from a single point
  • Use of all available resources. Includes using Linux commands or scripting where appropriate.
  • Efficient logic
  • Small footprint
Reply With Quote
  #5 (permalink)  
Old 08-03-08, 10:03 AM
aditya2071990 aditya2071990 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a load, wirehopper...just what I needed...informative and complete answer

Hmm, using ur answer as a checklist, lemme see:

1. Already following all that, as I don't want to break my head in 2 when I come back a month later and don't understand what I did.

2. That is a virtue? I mean I would rather prefer <?echo 'Hello world!'?> to the more complicated <?= "Hello World!"?> and other weird shortcuts used by those who call themselves uber coders. Nice to know I'm doing the right thing!

3. Hmm, reuse is something I do practice, as I am pretty lazy. Another good thing I do...

4.Hey that's a new thing there. I usually wrap all my HTML inside an echo. I gotta learn how to do what you said.

5. Yes. Done. I love outputting witty and entertaining error messages when they do happen.

6.Hmmmmmm....secuuurityy....the one thing I can't seem to get my head around. I'll have to put in more research time into this one I guess.

7.Of Course.

8.Hmm? I didn't get you wid that...

9. I dunt know linux yet. Problem it ees. However, can you explain with an example as to where linux scripting wud fit into, say, a php project of classified ads?

10 & 11. U have once again left me in the abstract with those. How do we define 'efficient' logic and its 'inefficient' counterpart? And what's a footprint?

Thanks again for the reply, wirehopper, and everybody else...
Reply With Quote
  #6 (permalink)  
Old 08-03-08, 10:51 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
2. Complex vs. simple means finding the simplest solution and using it. If you find yourself writing alot of code for something, you may be making it more complicated than it should be.

4. Refers more to web applications (for example a cart or content management system) than sites, where there is significant logic. In that case, you want to allow the design team access to the HTML, without having them editing PHP files.

8. If you have a database that will be accessed from more than one place, the username, and password should be stored in a single place. That way, if they change, they only have to change in one place.

9. PHP is primarily for web sites. If it has to run a file system command, such as a copy, it should be done as a Linux command, otherwise, you are reinventing the wheel. There may be times this won't work, due to permissions and configurations.

10. Efficient logic covers issues such as if you have to iterate through an array more than once, all the operations are performed in a single loop, even if they are unrelated. That way, there is only one loop.

11. Footprint refers to file size at run time. Break the system up into smaller files, because PHP is an interpretive language and it has to read the file every time it runs. Smaller files can be read and processed faster.
Reply With Quote
  #7 (permalink)  
Old 08-04-08, 11:07 PM
aditya2071990 aditya2071990 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Trophee Wow

Wow again, that explains everything! And I do realize that these tips can be used everywhere, not just php, and the result will be efficient, powerful code that just works. Thanks a lot again, wirehopper, I would love to have dugg this, but I can't seem to find the button...and you shud really consider setting up a blog of concepts like these...if u already have one, do post the url....thanks again, and bye!
Reply With Quote
  #8 (permalink)  
Old 08-05-08, 06:55 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
aditya - I'm glad the posts were helpful. Good luck

Blog: http://know-waste.com
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouseover php code needhelp? PHP 3 02-03-07 01:50 PM
php/apacher server displaying php code doctork1 PHP 2 12-15-04 12:10 PM
Need Help In PHP Programming for a single code suggestion !! CyberRomeo PHP 1 11-27-04 10:38 AM
Need help with PHP code rodman PHP 0 05-21-04 09:28 AM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM


All times are GMT -5. The time now is 07:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.