473,609 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Style question

How do you indent your switch statements? My boss prefers

switch(x) {
case 1:
...
case 2:
...
default:
...
}

while I am steadfastly (stubbornly?) clinging to

switch(x) {
case 1:
...
case 2:
...
default:
...
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
22 1514
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote in
news:bv******** **@chessie.cirr .com:
How do you indent your switch statements? My boss prefers

switch(x) {
case 1:
...
case 2:
...
default:
...
}

while I am steadfastly (stubbornly?) clinging to

switch(x) {
case 1:
...
case 2:
...
default:
...
}


switch (cond)
{
case COND_MATCH_1:
break;

case COND_MATCH_2:
break;

default:
/* default first if the normal case and
** not the exception case.
*/
break;
}

However, I do see the appeal of the case pulled left one tab stop.

--
- Mark ->
--
Nov 14 '05 #2
Christopher Benson-Manica wrote:
How do you indent your switch statements? My boss prefers

switch(x) {
case 1:
...
case 2:
...
default:
...
}


I agree with your boss, except the case statements themselves
should be indented 4 spaces, not 2. (And the opening brace should
be in that first column.)

--
|_ CJSonnack <Ch***@Sonnack. com> _____________| How's my programming? |
|_ http://www.Sonnack.com/ _______________ ____| Call: 1-800-DEV-NULL |
|______________ _______________ _______________ _|_____________ __________|
Nov 14 '05 #3
Christopher Benson-Manica wrote:
How do you indent your switch statements? My boss prefers

switch(x) {
case 1:
...
case 2:
...
default:
...
}

while I am steadfastly (stubbornly?) clinging to

switch(x) {
case 1:
...
case 2:
...
default:
...
}


Stubborn is right.
Just get a C code reformatter like indent:

http://www.gnu.org/software/indent/indent.html

and have it both ways.

Nov 14 '05 #4
Christopher Benson-Manica wrote:
How do you indent your switch statements?


switch(expressi on)
{
case FIRST_CASE:
{
rc = FirstCaseCode() ;
break;
}
case SECOND_CASE:
{
rc = SecondCaseCode( );
break;
}
case ETC:
{
rc = EtcCaseCode();
break;
}
default:
{
rc = DefaultCode();
break;
}
}

I've used function calls rather than "inline" code here, merely to make the
structure clearer. Whether I would actually use a function call would
depend on the situation.

The purpose of the extra braces is a simple one. Sometimes one wants to skip
from the top of a case to its end very quickly. Adding a brace-pair means
that editors such as vim, the VC editor, the Borland editor, and (I'll be
bound!) emacs can get from one end of the case to the other - and back -
very quickly.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #5
Christopher Benson-Manica <at***@nospam.c yberspace.org> spoke thus:
How do you indent your switch statements? My boss prefers


And as a corollary, I'm trying to think of a consistent way to address
the possibility of one-line switch cases (rendered nonexistant if one
dislikes multiple returns)...

switch( val ) {
case 0: return 3;
case 1: return 2;
case 2:
/* do_stuff */
break;
default:
...
}

Are one line cases such as that suitably readable? I'll admit it, I'm
a space-saver, so I tend toward obfuscation. Or how about

switch( val ) {
case 0: a=3; break;
case 1: a=4; break;
default: a=1;
}

?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6
Christopher Benson-Manica wrote:

How do you indent your switch statements? My boss prefers

switch(x) {
case 1:
...
case 2:
...
default:
...
}

while I am steadfastly (stubbornly?) clinging to

switch(x) {
case 1:
...
case 2:
...
default:
...
}


I agree with your boss, who is obviously a person of skill and
importance. Most disagree with me and he. My argument is that
the case labels are really goto labels in disguise, and should be
easily discerned on reading. I will only indent case labels when
a nested case is in effect, i.e. I am maintaining two distinct
case indentation levels with switches. Thus the base level cases
are pulled out to the extreme left column.

To me, indentation is used to make finding the immediate
controlling statement easy, in this case the switch, where all the
decisions were made.

I will often have code snippets that look like:

switch (foo) {
case 0: bar(); break;
case 1: baz(); break;
default: punt(); break;
} /* switch */

If we are executing foo, and wonder why we got there, we just look
north and find the switch statement, unencumbered by meaningless
verbiage. For the particular case being executed, we look north
on the left hand column. All this is especially worthwhile IMO in
long complex switch statements, such as found in many state
machines.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #7
Christopher Benson-Manica wrote:
How do you indent your switch statements? My boss prefers


<chop><chop>

switch (x)
{
case 1:
/* ...do stuff... */
break;

case 2:
/* ...do stuff... */
break;

default:
/* ...do stuff... */
break;
}

Indentation as shown, in increments of three spaces.
And I do mean spaces (NOT tab-characters). :>)
(On-the-job I support several old
C software packages, where
too @$#!!! spacing was done
with <ugh> tabs. I HATE tabs. :>))

Nov 14 '05 #8
LaEisem wrote:
.... snip ...
Indentation as shown, in increments of three spaces.
And I do mean spaces (NOT tab-characters). :>)
(On-the-job I support several old
C software packages, where
too @$#!!! spacing was done
with <ugh> tabs. I HATE tabs. :>))


Any decent editor will fix that for you on your first edit.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #9
In article <20************ *************** @mb-m19.aol.com>,
la*****@aol.com nojunk (LaEisem) wrote:
Indentation as shown, in increments of three spaces.
And I do mean spaces (NOT tab-characters). :>)
(On-the-job I support several old
C software packages, where
too @$#!!! spacing was done
with <ugh> tabs. I HATE tabs. :>))


Why?
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3812
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}): class C(object): pass C.__bases__ = bases dict = 0
15
1716
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ?
33
2489
by: amerar | last post by:
Hi All, I can make a page using a style sheet, no problem there. However, if I make an email and send it out to my list, Yahoo & Hotmail totally ignore the style tags. It looks fine in Netscape though..... Question: I've tried linking & embedding the style tags with no luck. How can I use them inline? I've read that inline style sheets is the way to go if you want them to work in most email clients.......
1
1403
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I wanted. The page still appears properly, and it shows in Netscape Messenger just fine, but on Hotmail and Yahoo, each <DIV> tag does not appear where it is supposed to appear.
4
6588
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel = wx.Panel(self,-1) self.button = wx.Button(self.panel,-1,"Klikkerdeklik") self.button.SetPosition((200,40)) self.Bind(wx.EVT_BUTTON, self.VeranderLabel, self.button)
83
15572
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include <stdio.h> #include <string.h> void main() { char *str1="abcd";
39
2193
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A ? style A: .... .... int a = 1; if(0==a) {
18
2114
by: pocmatos | last post by:
Hi all, While I was programming 5 minutes ago a recurring issue came up and this time I'd like to hear some opinions on style. Although they are usually personal I do think that in this case as also to do with making the code easier to read. Imagine a function returning void (for example) and it's body is a big if with lots of special cases:
3
1951
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It works fine when I stream it out in my html page like this: <DIV ID="popSearch" STYLE=" position:absolute; visibility:hidden;
6
1984
by: MatthewS | last post by:
I've seen the question raised several times here, but apparently never answered. Since PyInstance_Check returns False for new-style class instances, is there a standard procedure for testing this using the C- Api? I would greatly appreciate some help with this. /Matthew
0
8074
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8571
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8535
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8220
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6997
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4017
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4080
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2530
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1386
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.