|  | |  |
09-17-2002, 07:46 PM
|
#1 (permalink)
| | bloomberg
Join Date: Jun 2002 Location: bloomberg
Posts: 263
| If statements i've just figured something out and i'm starting to use it but im not sure if it is really good coding style...
e.g. Code: //my if statements used to look like this
public void stuff()
{
if(abc)
{
doSomething(x);
}
else
{
doSomething(y);
}
}
//but now they look like this
public void stuff2()
{
if(abc)
{
doSomething(x);
return;
}
doSomething(y);
}
is this a good idea? doesn't matter? or should I stick to the regular if/else situation.
__________________ -- bloomberg. |
| |
09-17-2002, 09:22 PM
|
#2 (permalink)
| | Centurion Nova Prime
Join Date: May 2002 Location: Oak Park, IL (USA)
Posts: 287
| Personally, I prefer the if/else structure. It's clearer (not that the other one is a stretch). It makes a bigger difference on a method that returns something. For example: Code: public boolean checkit () {
boolean stat = false;
if (abc) {
doSomething(x);
stat = true;
} else {
doSomething(y);
stat = false; //the default, so not really necessary
}
return stat;
} Opinions will probably vary on this one, but I like having a single return where possible. (Although I'm sure I've violated this in some of my code).  |
| |
09-17-2002, 09:27 PM
|
#3 (permalink)
| | bloomberg
Join Date: Jun 2002 Location: bloomberg
Posts: 263
| Yeah, thats the way I used to like to do it but I found that the other way is pretty useful instead of putting huge if/else's around your code, saved me heaps of time and confusing Code: if(cantContinue) error("whatever"); return; rather than Code: if(!cantContinue)
{
//do things
}
else
{
//stop
error("whatever");
}
__________________ -- bloomberg. |
| |
09-17-2002, 09:46 PM
|
#4 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| i have wondered the same thing in php, but i decided to code the long way because it's easier to read.
i'm not using your exact example, but something more like this: PHP Code: <?
// short way
if($i == 1) echo "i is 1";
// long way
if($i == 1)
{
echo "i is 1";
}
?> just seams like it would take less time debugging if things went wrong. |
| |
09-17-2002, 09:53 PM
|
#5 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| does java support the ternary operator?
it is something i learned in c++, and recently learned that php supports PHP Code: <?
($i == 1) ? ($x = "i is 1") : ($x = "i is not 1");
?> $first ? $second : $third
above is the format.. if $first is true, then execute the $second expression, else execute the $third expression. |
| |
09-17-2002, 09:55 PM
|
#6 (permalink)
| | bloomberg
Join Date: Jun 2002 Location: bloomberg
Posts: 263
| I'm more referring to the fact that you can just "return" from a function thereby not needing the "else" part because your program will never get there if it "returns" prior.
e.g. Code:
private void x()
{
a();
b();
return;
c();
} gives a "Error, unreachable statement" at "c()" in java at least, im not sure if php supports it but i think it would from what i've seen.
i usually prefer the "long way" you're referring to as well and the way i'm suggesting would make it a little weird to read for debugging but i think it saves heaps of time, for me at least.
__________________ -- bloomberg. |
| |
09-17-2002, 10:08 PM
|
#7 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| aah, i do get it now..
that is pretty good .. sorry to keep referrencing php, but that is the only thing i can test quickly and know well =) PHP Code: <?
function test()
{
$i=1;
if($i==1) return 1;
return 0;
}
printf(test());
?> i didn't realize it would exit out of the code after a return. cool, thanks! |
| |
09-18-2002, 08:54 AM
|
#8 (permalink)
| | Code Monkey
Join Date: Jul 2002 Location: Michigan
Posts: 85
| As technobard said, opinions on this will vary. My opinion is to get out of the function ASAP and have as few indented code blocks as possible. As a general rule (not enforced) we like to refactor (factor-out) our code any time we we nest more than two code blocks, and one way to do this is to short-circuit the function and return eliminating the need for an else.
__________________ Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24 |
| |
09-19-2002, 02:17 PM
|
#9 (permalink)
| | Registered User
Join Date: Jun 2002 Location: Antarctica
Posts: 43
| It's all about preference. The first method is easier to read (to most people) and is more of a standard. The second method is probably slightly faster. (You'd have to pass through that bit of code thousands of times per second just for it to be slightly noticeable even on a 486 though.) I'd say use the second choice on your own programs if you like it better, but if you're going to get people to help you code, or you're doing it for a business, go with the first. |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 12:22 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |