By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

Coding convention

P: n/a
Consider the following code (contained in a class method):

switch($sectionType)
{
case 'layout':
return $this->getLayoutSection($sectionName);
break;
case 'content':
return $this->getContentSection($sectionName);
break;
}

Obviously neither break is going to get execute, because the method will
return before it gets to them.

I have gotten into the habit of putting breaks at the end of every case in a
switch, even if it is not necessary (as the above). I have it somewhere in
the back of my head that this is the "proper" way to do things. But now
that I think about it, I have to ask, is this true? Is it consider good
practice to always end case statements with a break, even when not
necessary?

I'd like to hear others' thoughts on this. Thanks!

Sincerely,
-Josh
Jul 17 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
Joshua Beall wrote:
Obviously neither break is going to get execute, because the method
will return before it gets to them.

I have gotten into the habit of putting breaks at the end of every
case in a switch, even if it is not necessary (as the above). I have
it somewhere in the back of my head that this is the "proper" way to
do things. But now that I think about it, I have to ask, is this
true? Is it consider good practice to always end case statements
with a break, even when not necessary?

I'd like to hear others' thoughts on this. Thanks!


IMO, good coding practices imply omitting unnecessary code. When the break
is never reached, just keep it out.
JW

Jul 17 '05 #2

P: n/a
In article <qJKvd.6914$E_6.1509@trnddc04>, Joshua Beall wrote:
I have gotten into the habit of putting breaks at the end of every case in a
switch, even if it is not necessary (as the above). I have it somewhere in
the back of my head that this is the "proper" way to do things. But now
that I think about it, I have to ask, is this true? Is it consider good
practice to always end case statements with a break, even when not
necessary?


$cost = 0;

switch($foo)
{
case "newcustomer":
$cost += 100;
case "customer":
case "goodcustomer":
$cost += ($qty * $price);
break;
case "partner":
$cost -= ($cost * 0,10);
}


--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #3

P: n/a

"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> wrote in message
news:qJKvd.6914$E_6.1509@trnddc04...
Consider the following code (contained in a class method):

switch($sectionType)
{
case 'layout':
return $this->getLayoutSection($sectionName);
break;
case 'content':
return $this->getContentSection($sectionName);
break;
}

Obviously neither break is going to get execute, because the method will
return before it gets to them.

I have gotten into the habit of putting breaks at the end of every case in a switch, even if it is not necessary (as the above). I have it somewhere in the back of my head that this is the "proper" way to do things. But now
that I think about it, I have to ask, is this true? Is it consider good
practice to always end case statements with a break, even when not
necessary?

I'd like to hear others' thoughts on this. Thanks!

Sincerely,
-Josh


Don't use switch loops in the first place, that's my advice.
Jul 17 '05 #4

P: n/a
.oO(Chung Leong)
Don't use switch loops in the first place, that's my advice.


Sometimes they are useful, even if it's just for better readable code.

BTW: What is a switch _loop_?

SCNR
Micha
Jul 17 '05 #5

P: n/a
.oO(Joshua Beall)
Consider the following code (contained in a class method):

switch($sectionType)
{
case 'layout':
return $this->getLayoutSection($sectionName);
break;
case 'content':
return $this->getContentSection($sectionName);
break;
}

Obviously neither break is going to get execute, because the method will
return before it gets to them.

[...]

I'd like to hear others' thoughts on this. Thanks!


I would remove the breaks in this case.

Micha
Jul 17 '05 #6

P: n/a
In article <32*************@individual.net>, Tim Van Wassenhove wrote:
In article <qJKvd.6914$E_6.1509@trnddc04>, Joshua Beall wrote:
I have gotten into the habit of putting breaks at the end of every case in a
switch, even if it is not necessary (as the above). I have it somewhere in
the back of my head that this is the "proper" way to do things. But now
that I think about it, I have to ask, is this true? Is it consider good
practice to always end case statements with a break, even when not
necessary?


$cost = 0;

switch($foo)
{
case "newcustomer":
$cost += 100;
case "customer":
case "goodcustomer":
$cost += ($qty * $price);
break;
case "partner":
$cost -= ($cost * 0,10);
}


i wish i were a partner. (stupid bug)

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #7

P: n/a
I would let the breaks there. This way, if you later refactor your code
this way, you won't have a problem (I like having one return per
function, even if I don't make it a religious case):

switch($sectionType)
{
case 'layout':
$result = $this->getLayoutSection($sectionName);
break;
case 'content':
$result = $this->getContentSection($sectionName);
break;
}
return $result;
(Sorry for the bad indentation. Google doesn't like it)

Jul 17 '05 #8

P: n/a
A switch loop really shouldnt be used for decisions involving only 2
items. This might actually incur more overhead than an if/else.

do this

return ($sectionType == "layout") ?
$this->getLayoutSection($sectionName) :
$this->getContentSection($sectionName);

Jul 17 '05 #9

P: n/a
In article <11**********************@c13g2000cwb.googlegroups .com>, Steve wrote:
A switch loop really shouldnt be used for decisions involving only 2
items. This might actually incur more overhead than an if/else.


should one choose for a switch or if/else based on the overhead the
methods have?

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #10

P: n/a
This thread proves that code conventions are also a matter of taste. What
works for me certainly doesn't have to work for others. I, for one, don't
have any problems omitting a break statement when it isn't used and
inserting it when needed.

And when I say that I totally dislike the switch statement (in any language)
because of its (IMO) ugly syntax, many people will agree and many will
disagree.

Keeping your code readable, involves good documentation and compact code.
Adding a break to a switch doesn't dismiss the necessity of good
documentation and vice versa. But this is my opinion...
JW

Jul 17 '05 #11

P: n/a
Joshua Beall wrote:
Consider the following code (contained in a class method):

switch($sectionType)
{
case 'layout':
return $this->getLayoutSection($sectionName);
break;
case 'content':
return $this->getContentSection($sectionName);
break;
}

Obviously neither break is going to get execute, because the method
will return before it gets to them.

I have gotten into the habit of putting breaks at the end of every
case in a switch, even if it is not necessary (as the above). I have
it somewhere in the back of my head that this is the "proper" way to
do things. But now that I think about it, I have to ask, is this
true? Is it consider good practice to always end case statements
with a break, even when not necessary?

I'd like to hear others' thoughts on this. Thanks!


In keeping with the single point of return concept, I'd recommend this:

switch($sectionType)
{
case 'layout':
$result = $this->getLayoutSection($sectionName);
break;
case 'content':
$result = $this->getContentSection($sectionName);
break;
}
return $result;

But I'd probably also include a catch-all to handle the situarion when
$sectionType is not 'layout' or 'content'.

- Virgil
Jul 17 '05 #12

P: n/a
set a variable to this-> etc..
then break and then return at the end.

returns all over the place is not easily readable esp. in long pieces
of code.
Jul 17 '05 #13

P: n/a
"Tim Van Wassenhove" <eu**@pi.be> wrote in message
news:32*************@individual.net...
-snip-
i wish i were a partner. (stupid bug)


You'd also wish you'd read the OP's question more carefully ;-)

However I'd agree with everyone else who said that the break statement is
unnessecary when preceeded by a return statement and can safely be removed.

Jul 17 '05 #14

P: n/a
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@news.euronet.nl...
This thread proves that code conventions are also a matter of taste. What
works for me certainly doesn't have to work for others. I, for one, don't
have any problems omitting a break statement when it isn't used and
inserting it when needed.

And when I say that I totally dislike the switch statement (in any language) because of its (IMO) ugly syntax, many people will agree and many will
disagree.


Although a switch structure can lack flexibility, i.e. only one argument per
structure it is certainly preferable to a large number of elseif statements.
Jul 17 '05 #15

P: n/a
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:m8********************************@4ax.com...
.oO(Chung Leong)
Don't use switch loops in the first place, that's my advice.


Sometimes they are useful, even if it's just for better readable code.

BTW: What is a switch _loop_?

SCNR
Micha


In many situation where you would use a switch loop in C/C++, in PHP you can
use an associative array. Doing a hash look-up is much faster than going
through a chunk of op-codes. You get much flexibility as you can build the
table programmatically. And the name => value notation presents the
information much more clearly.

For example, say you have a routine that resizes images in various formats.
Using associative arrays, your code would look something like this:

$image_functions = array(
'.gif' => "imagecreatefromgif,imagegif",
'.png' => "imagecreatefrompng,imagepng",
'.jpeg' => "imagecreatefromjpeg,imagejpeg",
'.jpg' => "imagecreatefromjpeg,imagejpeg"
);
if(list($open, $save) = explode(',' $image_functions[$ext])) {
$img = $open($filename);
/* resizing code */
$save($img, $filename);
}

Very compact and obvious. Using switch loops on the other hand means
stacking two of them on top of each other.

As for the terminology, you must admit that it's much more colloquial to
"put some code in a loop" than to "put some code in a control structure" :-)
Jul 17 '05 #16

P: n/a
.oO(Chung Leong)
In many situation where you would use a switch loop in C/C++, in PHP you can
use an associative array. Doing a hash look-up is much faster than going
through a chunk of op-codes.
OK, good point.
As for the terminology, you must admit that it's much more colloquial to
"put some code in a loop" than to "put some code in a control structure" :-)


I assume you also like if-loops? ;)

Micha
Jul 17 '05 #17

P: n/a
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:4s********************************@4ax.com...
As for the terminology, you must admit that it's much more colloquial to
"put some code in a loop" than to "put some code in a control structure"
:-)
I assume you also like if-loops? ;)


:-)

I think the word Chung was looking for is "block".

- Robert
Jul 17 '05 #18

P: n/a
On Wed, 15 Dec 2004 03:11:59 +0100, Michael Fesser <ne*****@gmx.net> wrote:
.oO(Chung Leong)
Don't use switch loops in the first place, that's my advice.


Sometimes they are useful, even if it's just for better readable code.

BTW: What is a switch _loop_?


There's the PHP quirk that 'continue', which only works in loops in most
languages, also gets you out of 'switch' statements. Pushing the definition a
bit though :-)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #19

P: n/a
d
What the heck do you use instead of a switch? Yes, the syntax isn't worthy
of Titian, but come on! It's perfectly functional.

have you thought of interior decorating? :)

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41***********************@news.euronet.nl...
This thread proves that code conventions are also a matter of taste. What
works for me certainly doesn't have to work for others. I, for one, don't
have any problems omitting a break statement when it isn't used and
inserting it when needed.

And when I say that I totally dislike the switch statement (in any
language) because of its (IMO) ugly syntax, many people will agree and
many will disagree.

Keeping your code readable, involves good documentation and compact code.
Adding a break to a switch doesn't dismiss the necessity of good
documentation and vice versa. But this is my opinion...
JW

Jul 17 '05 #20

P: n/a
d wrote:
What the heck do you use instead of a switch? Yes, the syntax isn't
worthy of Titian, but come on! It's perfectly functional.


So is eval(), but that doesn't mean I will use it without searching for
other options first.
JW

Jul 17 '05 #21

P: n/a
In the next release of PHP5, there will be an invisible_Elf function
that will magically handle all of these quandries.

Maybe we should all just sit back and be greatful that we have such a
flexible language to work with, and not be code-nazis like those freaks
you had in your comp sci classes. Just relax!
Happy Christmahanukwanzakah!

Jul 17 '05 #22

P: n/a
What about using a variable to store the return value, use the break
as it was supposed to be used, and return at the end of your function
?

It's dangerous to return in more than one place, because you could
overlook the extra returns, when reading.

And reading is what source is all about.

You may spend many more hours reading the source than the computer
will spend time executing it.

In most cases, not all, speed is unimportant.

Code for everybody else, not for yourself, because your software may
live long after you died.

So do textbook coding, don't be smarter than everybody else.

fr gr
Erik

Jul 17 '05 #23

P: n/a
Joshua Beall wrote:
Consider the following code (contained in a class method):

switch($sectionType)
{
case 'layout':
return $this->getLayoutSection($sectionName);
break;
case 'content':
return $this->getContentSection($sectionName);
break;
}

Obviously neither break is going to get execute, because the method will
return before it gets to them.

I have gotten into the habit of putting breaks at the end of every case in a
switch, even if it is not necessary (as the above). I have it somewhere in
the back of my head that this is the "proper" way to do things. But now
that I think about it, I have to ask, is this true? Is it consider good
practice to always end case statements with a break, even when not
necessary?

I'd like to hear others' thoughts on this. Thanks!

Sincerely,
-Josh

The thinking behind this 'good practice' is that when you revisit the
code to change it to what the customer wants this week, it will still work.

Like *always* putting braces around if blocks, even is one line long.

Steve
Jul 17 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.