vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Is this proper coding? (https://vborg.vbsupport.ru/showthread.php?t=260479)

Boofo 03-16-2011 01:18 PM

Is this proper coding?
 
Can someone tell me in their opinion if this is proper coding? The following code is the fix for the division by zero error.

I am replacing this:

Code:

if ($totalthreads == 0)
{
    $totalthreads = 1;
}
if ($totalposts == 0)
{
    $totalposts = 1;
}


With this:

Code:

$totalthreads == 0 ? $totalthreads = 1 : $totalthreads;
$totalposts == 0 ? $totalposts = 1 : $totalposts;


It works, but I am curious as to what your feelings are as to whether this is actually a proper way to do it or not.

Can anyone tell I got bored enough to do this? ;)

Boofo 04-14-2011 12:33 PM

No opinions one way or the other?

Lynne 04-14-2011 04:06 PM

I don't know which is better. When I 'read' code, I like it better the first way - it just looks cleaner to me.

Boofo 04-14-2011 04:22 PM

I agree somewhat that it is easier to understand what the code in the first example is doing. But I also like the second way as it condenses 8 lines into 2.

Lynne 04-14-2011 04:30 PM

For single 'actions' to a condition, I actually like it like this:

PHP Code:

if ($totalthreads == 0$totalthreads 1
if (
$totalposts == 0$totalposts 1


Boofo 04-14-2011 04:49 PM

Yes, that would be the better way to do it for single actions. I think I will use that instead of my way since I don't really need the else part of it for this particular code. Thanks for pointing that out. ;)

I guess I'm just used to the brackets being there. ;)

Disasterpiece 04-19-2011 07:59 AM

I can trump that.
PHP Code:

if (!$totalthreads$totalthreads 1
if (!
$totalposts$totalposts 1

PHP Code:

$totalthreads max($totalthreads,1); 
$totalposts max($totalposts,1); 


Boofo 04-19-2011 10:27 AM

Quote:

Originally Posted by Disasterpiece (Post 2186224)
I can trump that.
PHP Code:

if (!$totalthreads$totalthreads 1
if (!
$totalposts$totalposts 1

PHP Code:

$totalthreads max($totalthreads,1); 
$totalposts max($totalposts,1); 


Will you explain the logic behind that? Also, how would you do the following using max?

Code:

if ($vbulletin->options['something_here'] == '') $comma_separated = '0';

YankForum 04-19-2011 11:35 AM

Quote:

Originally Posted by Disasterpiece (Post 2186224)
I can trump that.
PHP Code:

if (!$totalthreads$totalthreads 1
if (!
$totalposts$totalposts 1

PHP Code:

$totalthreads max($totalthreads,1); 
$totalposts max($totalposts,1); 


nice one , but the most important question here is which one will be processed faster in php? in my opinion the second proposed code by boofo should be the fastest since it only takes 2 values true or false??? of course as lynne says the first one is more readable and easy to customize , i like it the most
nice thread , thanks boofo

Disasterpiece 04-19-2011 01:36 PM

Quote:

Originally Posted by Boofo (Post 2186248)
Will you explain the logic behind that? Also, how would you do the following using max?

Code:

if ($vbulletin->options['something_here'] == '') $comma_separated = '0';

in php you have automatic typecast, so everything evalueates to either true or false. Zero (numeric) evaluates to false, so the ! inverts the evaluation to "true" and therefore you set the totalthreads to 1. It's just that it looks nicer because it's short :P
if it takes one or more additional cycles to process it? maybe. But it's php after all, so the uber-performance isn't that of an issue.

max is a mathematic function which returns the greater value of the given params. If you don't want to have a number which is negative or equal to 0, you use max($n, 1) so you guarantee that you always have at least 1.
Not sure about the performance, It's possible that the php engine uses the native c math function, so this might even speed things up a bit? dunno but then again, doesn't really matter :P

Just intended to brag with my mad php skillz, honestly I think the idea itself is an issue here (it throws an error when it's zero? well, just increase it then. <- wtf?)
If you really want to get help, you should explain your problem instead of the solution :)


PHP Code:

if ($vbulletin->options['something_here'] == ''$comma_separated '0'

I have nothing to add. maybe use "empty()" instead of testing for empty string, since the empty function checks for null as well and doesn't throw a warning when the value is not set.


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

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01159 seconds
  • Memory Usage 1,755KB
  • 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
  • (4)bbcode_code_printable
  • (8)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete