473,387 Members | 1,790 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,387 software developers and data experts.

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)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
22 1492
Christopher Benson-Manica <at***@nospam.cyberspace.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(expression)
{
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.powernet.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.cyberspace.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)cyberspace.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********@yahoo.com) (cb********@worldnet.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********@yahoo.com) (cb********@worldnet.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.comnojunk (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
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
In article <20***************************@mb-m19.aol.com>,
la*****@aol.comnojunk (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?


Because people use different defaults for the number of spaces in a
tab.
Several times I have come across what looks like a badly formatted
peice of code only to find it uses a wierd number.
Nov 14 '05 #11
CBFalconer <cb********@yahoo.com> spoke thus:
Any decent editor will fix that for you on your first edit.


However, certain command-line print utilities (*cough* a2ps) are
tab-unfriendly, and so I too have developed an aversion to them.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #12
CBFalconer <cb********@yahoo.com> wrote in
news:40***************@yahoo.com:
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.


I've yet to see one handle the guy who uses space over to tab and true
tabs well. E.g.

.. = space
- = tab
first tabstop 4 second 12, third 20, etc.

int main(void)
{
...- int var;
- int foo;

.....for (var = 0; var < 12; ++var)
- {
-- foo = var * 2;
.........- foo /= 2;
-- }

....-return 0;
}

I see this all the time when I tell my editor to show spaces and tabs.

--
- Mark ->
--
Nov 14 '05 #13
"Mark A. Odell" wrote:
CBFalconer <cb********@yahoo.com> wrote in
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.


I've yet to see one handle the guy who uses space over to tab and
true tabs well. E.g.

. = space
- = tab
first tabstop 4 second 12, third 20, etc.

.... snip ...
I see this all the time when I tell my editor to show spaces and tabs.


Vedit will handle this very nicely. Also there are various detab
filters available that do all that too. The help response from
mine:

c:\c\hashlib>detab
DETAB copyright (c) 1989 by C.B. Falconer. Ver 1.0

usage: DETAB [tablist] <infile >outfile
If tablist is omitted a tab interval of 8 is assumed
If tablist has only one number, then that is the tab interval
If tablist contains multiple numbers, separated by spaces,
then they are assumed to be the tab points. The leftmost
column is numbered 0.
If tablist is a single 0, then tabs are not expanded
Backspaces in a line shut off tab expansion until cr

Any isolated lf's are converted into cr/lf pairs (Unix files)
Omitting the "<infile" causes this help screen

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #14
"Mark A. Odell" wrote:
I see this all the time when I tell my editor to show spaces and
tabs.


I'll second that observation! DOWN WITH TABS!!

--
|_ CJSonnack <Ch***@Sonnack.com> _____________| How's my programming? |
|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL |
|_____________________________________________|___ ____________________|
Nov 14 '05 #15
CBFalconer <cb********@yahoo.com> wrote in
news:40***************@yahoo.com:

I've yet to see one handle the guy who uses space over to tab and
true tabs well. E.g.

. = space
- = tab
first tabstop 4 second 12, third 20, etc.
... snip ...

I see this all the time when I tell my editor to show spaces and tabs.


Vedit will handle this very nicely. Also there are various detab
filters available that do all that too. The help response from
mine:


Vedit eh? I'll look but I *must* have a Brief keymap or my productivity
will plummet.
c:\c\hashlib>detab
DETAB copyright (c) 1989 by C.B. Falconer. Ver 1.0

usage: DETAB [tablist] <infile >outfile
If tablist is omitted a tab interval of 8 is assumed
If tablist has only one number, then that is the tab interval
If tablist contains multiple numbers, separated by spaces,
then they are assumed to be the tab points. The leftmost
column is numbered 0.
If tablist is a single 0, then tabs are not expanded
Backspaces in a line shut off tab expansion until cr


How might someone get a copy of 'detab'?

Regards.

--
- Mark ->
--
Nov 14 '05 #16
In article <1a**************************@posting.google.com >,
ro***********@antenova.com (Rob Thorpe) wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message
news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
In article <20***************************@mb-m19.aol.com>,
la*****@aol.comnojunk (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?


Because people use different defaults for the number of spaces in a
tab.
Several times I have come across what looks like a badly formatted
peice of code only to find it uses a wierd number.


I see. Well, I hate source code that doesn't use tab = four spaces :-)
Nov 14 '05 #17
In article <ch*********************************@slb-newsm1.svr.pol.co.uk>,
Christian Bau wrote:
In article <1a**************************@posting.google.com >,
ro***********@antenova.com (Rob Thorpe) wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message
news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
>
> Why [are tabs in C source code bad]?


Because people use different defaults for the number of spaces in a
tab.
Several times I have come across what looks like a badly formatted
peice of code only to find it uses a wierd number.


I see. Well, I hate source code that doesn't use tab = four spaces :-)


As is pointed out in the Busybox style guide, there is such a
thing as using tabs properly. If N tabs are used at the beginning
of lines that have N levels of brace nesting, they are visible as
such no matter what the tab-stop-pitch is on-screen for the current
editor (and indeed it naturally follows the preference of the reader,
not that of the author). This works most of the time. When you want
fancier alignment than can be achieved by this technique (e.g.,
lining up comments in a section of code that has multiple indentation
levels), you do indeed need to switch back to spaces instead of tabs,
and subject the reader to the tab-stop-pitch whim of the author.

- Larry
Nov 14 '05 #18
"Mark A. Odell" wrote:
CBFalconer <cb********@yahoo.com> wrote in
.... snip ...
c:\c\hashlib>detab
DETAB copyright (c) 1989 by C.B. Falconer. Ver 1.0

usage: DETAB [tablist] <infile >outfile
If tablist is omitted a tab interval of 8 is assumed
If tablist has only one number, then that is the tab interval
If tablist contains multiple numbers, separated by spaces,
then they are assumed to be the tab points. The leftmost
column is numbered 0.
If tablist is a single 0, then tabs are not expanded
Backspaces in a line shut off tab expansion until cr


How might someone get a copy of 'detab'?


Email me or otherwise supply a valid address and I'll send you the
MsDos executable. I lost my source in a crash years ago, but
never had any great urge to recreate it. If you wish to
promulgate it you have my full permission.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #19
"Mark A. Odell" <no****@embeddedfw.com> wrote:
CBFalconer <cb********@yahoo.com> wrote in
news:40***************@yahoo.com:
c:\c\hashlib>detab
DETAB copyright (c) 1989 by C.B. Falconer. Ver 1.0

usage: DETAB [tablist] <infile >outfile
If tablist is omitted a tab interval of 8 is assumed
If tablist has only one number, then that is the tab interval
If tablist contains multiple numbers, separated by spaces,
then they are assumed to be the tab points. The leftmost
column is numbered 0.
If tablist is a single 0, then tabs are not expanded
Backspaces in a line shut off tab expansion until cr


How might someone get a copy of 'detab'?


One does all the exercises in K&R 2 <g>.

Richard
Nov 14 '05 #20
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
In article <1a**************************@posting.google.com >,
ro***********@antenova.com (Rob Thorpe) wrote:
Because people use different defaults for the number of spaces in a
tab.
Several times I have come across what looks like a badly formatted
peice of code only to find it uses a wierd number.


I see. Well, I hate source code that doesn't use tab = four spaces :-)


Myeah. Problem is, most printers hate source that doesn't assume tabs to
be eight spaces, which is far too much to be useful.

Richard
Nov 14 '05 #21
Richard Bos wrote:
"Mark A. Odell" <no****@embeddedfw.com> wrote:
CBFalconer <cb********@yahoo.com> wrote in
c:\c\hashlib>detab
DETAB copyright (c) 1989 by C.B. Falconer. Ver 1.0

usage: DETAB [tablist] <infile >outfile
If tablist is omitted a tab interval of 8 is assumed
If tablist has only one number, then that is the tab interval
If tablist contains multiple numbers, separated by spaces,
then they are assumed to be the tab points. The leftmost
column is numbered 0.
If tablist is a single 0, then tabs are not expanded
Backspaces in a line shut off tab expansion until cr


How might someone get a copy of 'detab'?


One does all the exercises in K&R 2 <g>.


I suspect you are right, but I can't find it. At any rate the
above version was written in Pascal. As you can see I had no urge
to deal with the complications of backspacing.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #22
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
In article <1a**************************@posting.google.com >,
ro***********@antenova.com (Rob Thorpe) wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message
news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
In article <20***************************@mb-m19.aol.com>,
la*****@aol.comnojunk (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?


Because people use different defaults for the number of spaces in a
tab.
Several times I have come across what looks like a badly formatted
peice of code only to find it uses a wierd number.


I see. Well, I hate source code that doesn't use tab = four spaces :-)

Well, you can hate what you like :).
There are large amounts of code out there with 4, 6 and 8 stops used
for tabs.
I have to swap between them quite frequently.
As a result I've gained a bias against the whole idea of indenting
with tabs. Since almost every editor has the capability to insert
spaces for indentation these days it seem an unnecessary irritation.
Nov 14 '05 #23

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

Similar topics

12
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={}):...
15
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
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...
1
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...
4
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 =...
83
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...
39
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...
18
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...
3
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...
6
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.