![]() |
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) With this: Code:
$totalthreads == 0 ? $totalthreads = 1 : $totalthreads; 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? ;) |
No opinions one way or the other?
|
I don't know which is better. When I 'read' code, I like it better the first way - it just looks cleaner to me.
|
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.
|
For single 'actions' to a condition, I actually like it like this:
PHP Code:
|
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. ;) |
I can trump that.
PHP Code:
PHP Code:
|
Quote:
Code:
if ($vbulletin->options['something_here'] == '') $comma_separated = '0'; |
Quote:
nice thread , thanks boofo |
Quote:
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:
|
Quote:
Quote:
|
If you're going for shortest code, how about:
PHP Code:
Anyway, I'm not a php expert but I think I'd go for readability unless it's in a critical area where you're specifically worried about the speed. If the point is to make sure $totalthreads and $totalposts are a positive number, I'd probably go with PHP Code:
even if it's not "supposed" to ever be less than 0. (ETA: or on second thought maybe using max() would make the intention more obvious). |
Quote:
Altough I could argue, that the addition is always called and therefore produces more cycles then a comparison :P However, this is way too little code to actually perform "real" optimization. In the end, it's the coders preferrence which is important and easier to read for himself. There are more grave issues of coding-crimes where I sometimes like to kick the developer in the face; how if/else and brackets are placed doesn't belong in this category tough. |
Quote:
|
Quote:
Quote:
Quote:
|
vb 4 use max in quite a few places in the code, which I had never noticed before.
It all really boils down to what we get used to in coding styles, I guess. There are always newer and better ways to do things, so change is inevitable. ;) |
All times are GMT. The time now is 07:03 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|