473,563 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coding convention

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

switch($section Type)
{
case 'layout':
return $this->getLayoutSecti on($sectionName );
break;
case 'content':
return $this->getContentSect ion($sectionNam e);
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 1785
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 "newcustome r":
$cost += 100;
case "customer":
case "goodcustom er":
$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****@donotsp am.remove.me.he raldic.us> wrote in message
news:qJKvd.6914 $E_6.1509@trndd c04...
Consider the following code (contained in a class method):

switch($section Type)
{
case 'layout':
return $this->getLayoutSecti on($sectionName );
break;
case 'content':
return $this->getContentSect ion($sectionNam e);
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($section Type)
{
case 'layout':
return $this->getLayoutSecti on($sectionName );
break;
case 'content':
return $this->getContentSect ion($sectionNam e);
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.ne t>, 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 "newcustome r":
$cost += 100;
case "customer":
case "goodcustom er":
$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($section Type)
{
case 'layout':
$result = $this->getLayoutSecti on($sectionName );
break;
case 'content':
$result = $this->getContentSect ion($sectionNam e);
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->getLayoutSecti on($sectionName ) :
$this->getContentSect ion($sectionNam e);

Jul 17 '05 #9
In article <11************ **********@c13g 2000cwb.googleg roups.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 has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1267
by: News.microsoft.com | last post by:
Any one Who have coding convention? Please share me. Thanks in advance.
5
5392
by: db2sysc | last post by:
ALl. Is it possible to get MS ACCESS CODING STANDARDS? TIA
9
3086
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 numeric data types). Do such programmers also have conventions for naming structures and unions (e.g. do they like their names to begin with s and...
144
6776
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 initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force...
7
2555
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). In the example programs of Microsoft they use different coding conventions: private members sometimes with underscore, sometimes without; when...
39
3478
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 and method parameters use camel casing, like this: void SetName(int id, string newName)
3
1089
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 Haq
1
1698
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. If anyone could direct me to such a document, it would be greatly appreciated. Thank you.
6
1488
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 convention, a parameter should be be named like this "typeName" (ei stringFirstName). The types that they list add to the variable name and the amount of...
60
5003
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. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being...
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6244
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.