473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Coding convention

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
23 1772
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
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

"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
.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
.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
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
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
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
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
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
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
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
"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
"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
"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
.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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: News.microsoft.com | last post by:
Any one Who have coding convention? Please share me. Thanks in advance.
5
by: db2sysc | last post by:
ALl. Is it possible to get MS ACCESS CODING STANDARDS? TIA
9
by: Neil Zanella | last post by:
Hello, Some programmers like to use a coding convention where all names of variables that are pointers start with the letter p (and sometimes even use similar conventions for strings and other...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
39
by: Patrick | last post by:
The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this: int recordId; string name; It also suggests that variables within methods...
3
by: Ejaz ul Haq | last post by:
Hi, m a newbie to C# programming, please tell me coding convention or notation followed professionally by the programmers. If possible send me some web page link elaborating the idea. -- Ejaz ul...
1
by: Uncle Ben | last post by:
I'm starting a new ASP.NET C# project and would like to adopt a good coding convention. Things like indentation is 3 spaces, lining up the brace brackets, naming functions using Mixed case, etc. ...
6
by: Andrea Williams | last post by:
Responding to this link in an old thread: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht ml/cpconnetframeworkdesignguidelines.asp According to that naming...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.