Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Don't take vB's code as a model for perfect coding!
filburt1
Join Date: Feb 2002
Posts: 6,144

 

Maryland, US
Show Printable Version Email this Page Subscription
filburt1 filburt1 is offline 03-13-2003, 10:00 PM

Unlike rumors of vB3, vB2 has astetically horribly written code. Here are some things that you should do (and that vB doesn't always do):

1. When referencing a key in an array, always put the key in quotes. See http://www.php.net/manual/en/language.types.array.php for an explanation. For example, do this:
PHP Code:
$myarray['mykey'] = "whatever"
Not this:
PHP Code:
$myarray[mykey] = "whatever"
2. Whitespace is your friend. Use it. For example, this is painful to read:
PHP Code:
if($a==b&&$c xor ($d+1)){foo();bar()}; 
For the love of God, do this instead:
PHP Code:
if ($a == $b and $c xor ($d 1)
{
    
foo();
    
bar();

For readability the two braces really should be on their own lines although many prefer to put the opening brace with the previous line. But do not do the same with the last brace as this can cause more trouble.

3. Indent or be smacked.
Bad:
PHP Code:
while (true)
{
if (
$something)
{
for (
$i 0$i $x$i++
{
dosomething();
}
}

Good:
PHP Code:
while (true)
{
    if (
$something)
    {
        for (
$i 0$i $x$i++)
        {
            
dosomething();
        }
    }

If you write like the first example you'd be fired at your job. A side note: the typical editor uses four spaces or a tab character to indent. Try to keep this consistant! vB does not and can wreak havoc when trying to indent the standard four spaces because nothing will line up.


I'm sure many more examples are out there. Reply if you have any.
Reply With Quote
  #2  
Old 03-14-2003, 11:26 PM
Erwin's Avatar
Erwin Erwin is offline
 
Join Date: Jan 2002
Posts: 7,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Good advice from the turtle. Makes a difference especially when trying to debug errors.
Reply With Quote
  #3  
Old 03-15-2003, 07:16 AM
Chris M's Avatar
Chris M Chris M is offline
 
Join Date: Dec 2001
Location: Northampton, England
Posts: 6,186
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yup...

Thanks for that

*Sues vB*

On a side note: I really wish I could change Erwin's spoiler message...

Satan
Reply With Quote
  #4  
Old 03-15-2003, 09:26 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Indeed. Good tips there burt !

- miSt
Reply With Quote
  #5  
Old 03-15-2003, 01:31 PM
shovel's Avatar
shovel shovel is offline
 
Join Date: Mar 2002
Location: South Carolina, US
Posts: 222
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm really hoping vB3 has followed the true professional PHP guidelines [like filbert has shown us]. If not, I don't know what I'd do. Lol.
Reply With Quote
  #6  
Old 03-15-2003, 02:06 PM
Scott MacVicar Scott MacVicar is offline
 
Join Date: Oct 2001
Location: Glasgow, Scotland
Posts: 1,199
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

having a look at the vb_test.php script on vB.com shows how the code is layed out in vB3.

http://www.vbulletin.com/forum/attac...achmentid=4326

I also posted a thread on vB3 syntax last year https://vborg.vbsupport.ru/showthrea...threadid=42946
Reply With Quote
  #7  
Old 03-15-2003, 03:36 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Today at 11:06 AM PPN said this in Post #6
having a look at the vb_test.php script on vB.com shows how the code is layed out in vB3.

http://www.vbulletin.com/forum/attac...achmentid=4326

I also posted a thread on vB3 syntax last year https://vborg.vbsupport.ru/showthrea...threadid=42946
Quote:
Bad Request
Your browser sent a request that this server could not understand.


--------------------------------------------------------------------------------

Apache/1.3.27 Server at www.vbulletin.com Port 80
Although IIRC from reading the code I think you're right
Reply With Quote
  #8  
Old 03-15-2003, 11:49 PM
shovel's Avatar
shovel shovel is offline
 
Join Date: Mar 2002
Location: South Carolina, US
Posts: 222
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Today at 11:06 AM PPN said this in Post #6
having a look at the vb_test.php script on vB.com shows how the code is layed out in vB3.

http://www.vbulletin.com/forum/attac...achmentid=4326

I also posted a thread on vB3 syntax last year https://vborg.vbsupport.ru/showthrea...threadid=42946
Thanks PPN.
Reply With Quote
  #9  
Old 03-15-2003, 11:54 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just looked at it again and it does look way better than vB2. Only problem is it's still using deprecated variables like $HTTP_POST_VARS (instead of $_POST or $_GET). Also I don't know why vB doesn't use the true and false keywords but instead uses 1 and 0 everywhere...
Reply With Quote
  #10  
Old 03-15-2003, 11:57 PM
Freddie Bingham's Avatar
Freddie Bingham Freddie Bingham is offline
 
Join Date: Oct 2001
Posts: 506
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't see how you could have looked since you don't have the vB3 code but we only refer to $HTTP_POST_VARS when the server is running php < 4.1. You shouldn't base your assumptions on a test script that has to work on php 4.0.6 and greater.
PHP Code:
if (PHPVERSION '4.1')
{
    
$_GET = &$HTTP_GET_VARS;
    
$_POST = &$HTTP_POST_VARS;
    
$_COOKIE = &$HTTP_COOKIE_VARS;
    
$_SERVER = &$HTTP_SERVER_VARS;
    
$_ENV = &$HTTP_ENV_VARS;
    
$_FILES = &$HTTP_POST_FILES;

As for using TRUE/FALSE or 0/1 - we are using true/false more but just switched over so you will see both instances for now.
Reply With Quote
Reply


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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:31 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04981 seconds
  • Memory Usage 2,307KB
  • Queries Executed 23 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (7)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete