vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Don't take vB's code as a model for perfect coding! (https://vborg.vbsupport.ru/showthread.php?t=50148)

filburt1 03-13-2003 10:00 PM

Don't take vB's code as a model for perfect coding!
 
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.

Erwin 03-14-2003 11:26 PM

Good advice from the turtle. :) Makes a difference especially when trying to debug errors.

Chris M 03-15-2003 07:16 AM

Yup...

Thanks for that:)

*Sues vB;)*

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

Satan

Dean C 03-15-2003 09:26 AM

Indeed. Good tips there burt :)!

- miSt

shovel 03-15-2003 01:31 PM

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.

Scott MacVicar 03-15-2003 02:06 PM

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

filburt1 03-15-2003 03:36 PM

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 :)

shovel 03-15-2003 11:49 PM

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.

filburt1 03-15-2003 11:54 PM

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...

Freddie Bingham 03-15-2003 11:57 PM

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.

filburt1 03-16-2003 12:06 AM

Whoops, didn't see that bit. :)

Dean C 03-16-2003 08:44 AM

Quote:

$_FILES = &$HTTP_POST_FILES;
Oh no ampersand when assigning variables. This is going to confuse me majorly as i still don't understand the concept :(

- miSt

Scott MacVicar 03-16-2003 11:07 AM

ampersand means you are referncing the variables.

PHP Code:

$array = array();
$newarray = &$array;
$array[] = 'test';
$array[] = 'cows';
print_r($newarray); 

basically you can change array and the values are reflected in $newarray.

Dean C 03-16-2003 11:09 AM

*Kinda understands*

- miSt

filburt1 03-16-2003 02:16 PM

It's a pointer:
PHP Code:

$a = &$b

So any time you modify $a you modify $b.

Dean C 03-16-2003 05:16 PM

I finally learnt this after reading a section from SAMS learn PHP in 24 hours :)

The books is good so far :D

- miSt

filburt1 03-16-2003 05:21 PM

I told you and everybody I know, Sams books are awesome :)

NTLDR 03-16-2003 06:33 PM

Personally I find the way the vB2 code is currently written far easier to read than all your examples of how to do it "correctly", but thats just me. I tend to be quite picky about how I lay my code out and do it in the way that I find easiest to read myself.

refertech 03-16-2003 06:51 PM

Oh, man I just left Barns and Noble and didnt buy that book. S**t, I guess ill have to go back. So that is the best book to starter with?

Mark

Dean C 03-16-2003 06:56 PM

SAMS php in 24 hours is awesome. I'd reccomend it so far :D

- miSt

Scott MacVicar 03-16-2003 08:03 PM

Core PHP 2nd Edition by Leon Atkison

Anything that claims you can learn a language in 24 hours lies.

Mastering MySQL 4 by Ian Gilfillan is what i've been using to read up on the new mysql 4 features and is a good reference for functions.

Boofo 03-16-2003 08:16 PM

Quote:

Today at 04:03 PM PPN said this in Post #21
Core PHP 2nd Edition by Leon Atkison

Anything that claims you can learn a language in 24 hours lies.

Mastering MySQL 4 by Ian Gilfillan is what i've been using to read up on the new mysql 4 features and is a good reference for functions.

Are both of these books good for novices that are just learning PHP?

Scott MacVicar 03-17-2003 12:23 AM

Core PHP is excellent for novices, its the book i used to learn PHP and I still use it from time to time to check out some of the wierd functions. Though its probably a bit out of date function wise now.

Boofo 03-17-2003 12:28 AM

What would you recommend getting now, especially with the coding for vb3 being so different?

Dean C 03-17-2003 02:30 PM

Hehe you can only learn if you read the book for 24 hours - it will take me a month with my schedule ;)

- miSt

Dean C 03-17-2003 02:31 PM

www.ineasysteps.com - Boofo i bought my first book from there. It was excellent and informative but very basic... I know the basics now :p

Brad 03-19-2003 10:58 PM

Quote:

Mastering MySQL 4 by Ian Gilfillan is what i've been using to read up on the new mysql 4 features and is a good reference for functions
Im going to pick that book up tomorrow, ive been putting it off...

Ive never learned from a book personlly, ive always done it the hard way, but i am picking up a book on php soon to help with some projects :)

Rose 03-19-2003 11:09 PM

I've been putting off getting a book and putting it off. But by golly I'm getting one by the end of the month!!!

N9ne 03-23-2003 04:39 PM

I hate the InEasySteps books, they're no where near in depth enough, www.wrox.com >> get Beginning PHP 4, I have it, superbly in depth and informative with excellent examples...

I sound like a salesman, don't i?



I do?





I've succeeded then :D

PennylessZ28 03-31-2003 11:56 AM

Books never helped me, I need to visualize. I have found that having someone hold a gun to your head, is a good way to qucikly figure out how things work.

;) I second www.wrox.com ;)

Cyburbia 04-18-2003 04:13 PM

I'm a bit anal about consistency with grammar and capitalization. For instance, I'll see a menu item that reads:

Modify This part of Your profile

followed by

Modify another aspect of Your Profile

Even if you look at the borrom of this page, you'll see

Show a Printable Version (headline case)

and

Receive updates to this thread (sentence case)

I did a LOT of template searching and replacing to make things consistent.

TECK 04-22-2003 10:48 PM

Quote:

03-15-03 at 04:06 PM Scott MacVicar 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

I still like to space out even the brackets:
Code:

$event = 'false';
if ( $string == 'value' )
{
        $event = 'true';
}

The TAB usage is vital also, instead of spaces. It will help you shrink the file size by a lot if it's a big one...


All times are GMT. The time now is 12:01 AM.

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.01569 seconds
  • Memory Usage 1,820KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (9)bbcode_php_printable
  • (7)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (32)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete