Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-17-2002, 06:46 PM   #1 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
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.
abc123 is offline   Reply With Quote
Old 09-17-2002, 08:22 PM   #2 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
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).
technobard is offline   Reply With Quote
Old 09-17-2002, 08:27 PM   #3 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
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.
abc123 is offline   Reply With Quote
Old 09-17-2002, 08:46 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
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.
sde is offline   Reply With Quote
Old 09-17-2002, 08:53 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
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.
sde is offline   Reply With Quote
Old 09-17-2002, 08:55 PM   #6 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
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.
abc123 is offline   Reply With Quote
Old 09-17-2002, 09:08 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
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!
sde is offline   Reply With Quote
Old 09-18-2002, 07:54 AM   #8 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
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
sdeming is offline   Reply With Quote
Old 09-19-2002, 01:17 PM   #9 (permalink)
kenshi
Registered User
 
kenshi's Avatar
 
Join Date: Jun 2002
Location: Antarctica
Posts: 43
kenshi is on a distinguished road
Send a message via ICQ to kenshi
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.
kenshi is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on MySQL Statements infinite_root Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 5 04-21-2005 05:37 PM
Brain Melting: Trouble With If Statements..... UnusualGameBoy Java 3 10-27-2004 02:10 PM


All times are GMT -8. The time now is 06:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting