Stylistic note on loops

B

BGB / cr88192

markspace said:
Yeah, but the de facto standard. Someone already linked to the Java code
style guidelines, so I won't repeat it here.

yes, but as I see it, it shouldn't matter that much...

not that I disagree with them that much either, only the idea that there
"should" be rigid style rules, and a few misc things I think are
pointless...

I think they are affected. I'm not affected by the code I wrote, but I
assume a maintainer would be. Therefore I asked for opinions in a public
forum.

possibly, but not likely that much, so long as it is nothing "too"
obnoxious.


say, if one were naming their variables something like:
"NsZ__ROFLOL_ia"
one would have to question their judgement regardless of the programming
language in use.

admittedly, I tend to dislike naming things with
"some_glob_of_words_and_underscores", as this doesn't really help matters
much at all...


but, inserting extra braces, or using "{}" in place of ";", ... these thing
seem like silliness.


about the only really sane rules I think are:
in general, use "camelCase" and avoid underscores in Java (I typically only
use underscores as scope separators in C anyways, and also when doing name
mangling).

I use them some in a cusom version of the class library, but more for "Z_*"
classes, which are intended to be used internally by the classlib
implementation (IOW: they intentionally look ugly...). I may switch though
to just simply using a "Z*" prefix instead for internal classes.

admittedly, I also tend to use an "I*" prefix some for interfaces, since it
makes it obvious what are classes and what are interfaces.

In my experience, tabs died out a decade ago. All indentation is with
spaces now. I always favored tabs myself, but the world has moved on. Que
sera sera.

well, I still see a lot of tabs.

but, with space-based tabs, it is a matter of if the tab spot is 4 or 8
spaces ().
a lot of older code used 2-space indentation, but I dislike this and think 4
or 8 is better (and except in rare cases have not seen 2-space indentation
in a long time).

Well, yeah. They have to be backwards compatible. They can't change
*existing* public APIs.


Trying to say this with good humor: I get the feeling that some of your
info is 15 years out of date. ;)

I am still seeing what I am still seeing, which is what I see in the
languages I tend to use (mostly C, some C++, and some Java and C#, and some
x86 and x86-64 assembler thrown in the mix...).
 
A

Arne Vajhøj

Somewhat off topic here, but:

»Karl Kraus was convinced that every little error, albeit
of an importance that was seemingly limited in time and
space, shows the great evils of the world and era. Thus,
he could see in a missing comma a symptom of that state
of the world that would allow a world war. One of the
main points of his writings was to show the great evils
inherent in such seemingly small errors.

Language was to him the most important tell-tale for the
wrongs of the world. He viewed his contemporaries'
careless treatment of language as a sign for their
careless treatment of the world as a whole.«

http://en.wikipedia.org/w/index.php?title=Karl_Kraus&oldid=384906924

Karl Kraus died in 1936 and didn't know programming
languages, but compare the above with:

»I have never, ever, ever seen a great software developer
who does not have amazing attention to detail.«

http://www.softwarebyrob.com/articles/Personality_Traits_of_the_Best_Software_Developers.aspx

Very interesting.

And I agree with much of it.

But there is a tough world out there with budgets and deadlines.

Arne
 
S

Stefan Ram

Eric Sosman said:
(...) Personally, I cannot recall a single occasion in more than forty
years of programming when I (1) wanted to skip over two numbers this
way and (2) wanted to ignore what those numbers were and (3) had such

I have been looking for similar loops »in the wild«:

A sequence of skipping scanning while loops:

while (!isspace(*p)) ++p; while (isspace(*p)) ++p;

http://github.com/mono/mono/blob/master/libgc/os_dep.c

While loops with a semicolon (and no corresponding »do« in front of them):

while (*c && *c++!='\n');

http://www.opensource.apple.com/source/man/man-4/man/man2html/man2html.c

Well, and I believe:

while( *p++ = *q++ );

is an idiom known to every C programmer.
 
C

ClassCastException

well, I still see a lot of tabs.

but, with space-based tabs, it is a matter of if the tab spot is 4 or 8
spaces ().
a lot of older code used 2-space indentation, but I dislike this and
think 4 or 8 is better (and except in rare cases have not seen 2-space
indentation in a long time).

It still seems to be standard in the Lisp/Scheme world.
 
S

Stefan Ram

ClassCastException said:
It still seems to be standard in the Lisp/Scheme world.

One Way to Format Parentheses

There are several different ways to format texts with braces
and parentheses. One of them is being described here.

Indentation within Braces

An indentation of just one space often is too small to be seen
clearly, because the natural width and form of characters
often varies by an amount that is not very much smaller than a
space. Therefore, the indentation should amount to at least
two positions. In order not to waste horizontal spaces, an
indentation of exactly two positions is chosen. This means,
that the left position of the next level is two larger than
the position of the directly enclosing level.

Indentation by two positions within a block

{ ++x;
++x; }
^ ^
0 2

Bad: A small indentation by one position is not always visible
clearly

{++x;
++x; }

Good: The indentation by two positions is visible clearly

{ ++x;
++x; }

Bad: A large indentation by more than two positions wastes
horizontal space with no additional benefit

{ ++x;
++x; }

Spaces within braces

In mathematics, there are often no spaces at the inner side of
parentheses or braces in expressions, but spaces are used
indeed at the inner side of braces in set notation, when the
braces contain a description (not when they contain a list).

Spaces in set notation

{ x | x > 2 }

This style is adopted here: One space is written at the inner
side of braces.

Spaces at the inner side of parentheses within a block

{ ++x; }

This style is consistent with the indentation by two
positions, because only using this style, corresponding parts
of two lines have the same position.

Bad: No space after the first brace, the two statements are
misaligned

{++x;
++x; }

Good: One space after the first brace, the two statements are
properly aligned

{ ++x;
++x; }

Bad: Two spaces after the first brace, the two statements are
misaligned

{ ++x;
++x; }

There are some exceptions to this rule: No spaces are used
within empty braces "{}" and between two or more closing
braces of the same direction "}}", except, when the first one
of them is part of an empty pair "{} }" (an empty pair of
braces if treated like a single non-braces character).

Unified rules for all Brackets

For simplicity and uniformity, the rules from above apply to
all kinds of brackets, including parentheses, braces (curly
brackets), square brackets, and angle brackets.

Spaces within parentheses and square brackets

{ y = f( x )+ g() + a[ 2 ]; }

Binary operators are sorrounded by a space, but the space is
omitted, when there already is a space on the other side of a
sequence of brackets directly beside the operator: By this rule,
" )+" is written instead of " ) +".

Representation of the Syntactical Structure

A method or function definition consists of a head and a body.
The following representation shows this structure:

Good formatting according to the structure

void alpha() // head
{ beta(); } // body

The following formatting is misleading, because the line break
does not match the structural break:

Bad line break within the body

void alpha() { // head and the beginning of the body
beta(); } // the rest of the body

This formatting also would make no sense for blocks within
blocks. So it is often not used for such blocks. Therefore
even the adopters of this style can not use it uniformly.

Opening Braces Look Like "bullets"

There is a well known style to publish lists in typography
using bullets sticking out on the left, looking like this:

Common list representation with bullets in typography

o This is the first point
of this list, it is written
here just as an example.

o Here is another entry

o This is another example given
just as an example to show
an example

The braces of the beginnings of blocks stand out on the left
just the same, when the formatting being described here is
used, so they look quite naturally as beginning-of-a-block
markers, when one is used to the typographical list notation:

Left braces look like bullets to mark blocks

{ printf(); printf();
printf(); printf(); printf();
printf(); printf(); }

{ printf(); printf(); }

{ printf(); printf(); printf();
printf(); printf();
printf(); }

Neutrality

Someone wrote this C code:

while( fgets( eingabe, sizeof eingabe, stdin ))
if( sscanf( eingabe, "%d", &wert )!= 1 )
fprintf( stderr, "Please enter a number!\n" );
else
summe += wert;

It amazes me that I can add braces by my style conventions
(not changing the meaning of the code)
without the need to change the position of any character of
the given code or need to change the overall number of lines:

The code from above plus braces

while( fgets( eingabe, sizeof eingabe, stdin ))
{ if( sscanf( eingabe, "%d", &wert )!= 1 )
{ fprintf( stderr, "Please enter a number!\n" ); }
else
{ summe += wert; }}

Insofar, my bracing style might be considered non-obtrusive.

Lines per Contents

Lines containing only a single brace waste vertical space, so
less contents fits on the same screen space. Therefore, I usually
avoid them, but sometimes I do use them, when this helps to
increase readability. I also might temporarily use them when editing
a section of code. Of course, they would help programmers paid or
being judged by the lines-of-code productivity.
 
T

Tom Anderson

[...]
int i;
for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
{
/* EMPTY */
}

Another idiom you may encounter for empty loop bodies is

for (i = 1; Character.isLetter(s.charAt(i)); i++)
continue;

Oh, i haven't seen that. I like that one.
... with or without curly braces, according to local style.

Death to braces around single statements!

Actually, whilst i'd write this:

for (i = 1; Character.isLetter(s.charAt(i)); i++) continue;

And i'm fine with this, really:

for (i = 1; Character.isLetter(s.charAt(i)); i++) {
continue;
}

I can't abide this:

for (i = 1; Character.isLetter(s.charAt(i)); i++)
continue;

That's just asking for trouble. Either the loop body is small enough to go
on one line, or it's big enough to be a block.

tom
 
T

Tom Anderson

In my experience, tabs died out a decade ago. All indentation is with
spaces now. I always favored tabs myself, but the world has moved on.
Que sera sera.

Seriously? We use nothing but where i work. In java and XML, at least -
for some reason, in JSP (well, HTML, really) we tend use two spaces. I
think because JSP/HTML tends to have much more deeply nested constructs.

I think our use of tabs must stem from the fact that Eclipse, by default,
uses tabs for indentation. They must have a reason to do that.

tom
 
T

Tom Anderson

Somewhat off topic here, but:

?Karl Kraus was convinced that every little error, albeit
of an importance that was seemingly limited in time and
space, shows the great evils of the world and era. Thus,
he could see in a missing comma a symptom of that state
of the world that would allow a world war. One of the
main points of his writings was to show the great evils
inherent in such seemingly small errors.

Language was to him the most important tell-tale for the
wrongs of the world. He viewed his contemporaries'
careless treatment of language as a sign for their
careless treatment of the world as a whole.?

http://en.wikipedia.org/w/index.php?title=Karl_Kraus&oldid=384906924

Sounds like an interesting chap, thanks for bringing him to my attention.
Karl Kraus died in 1936 and didn't know programming
languages, but compare the above with:

?I have never, ever, ever seen a great software developer
who does not have amazing attention to detail.?

http://www.softwarebyrob.com/articles/Personality_Traits_of_the_Best_Software_Developers.aspx

It's certainly a quality that commands a lot of respect from me.

tom
 
B

BGB / cr88192

Tom Anderson said:
Seriously? We use nothing but where i work. In java and XML, at least -
for some reason, in JSP (well, HTML, really) we tend use two spaces. I
think because JSP/HTML tends to have much more deeply nested constructs.

I think our use of tabs must stem from the fact that Eclipse, by default,
uses tabs for indentation. They must have a reason to do that.

yeah, the editor I use uses tabs as well, but I have the tab-spot set at 4.
an advantage of tabs is that they adjust indent to whatever are ones'
preferences/settings, but at the downside that they don't always keep things
aligned exactly as wanted when moving between editors.

2 spaces for indent seems as too little IMO, as it makes it harder to
clearly see the starts and ends of blocks.
Torvalds had defined his coding style rules as using 8-space tabs.
it was asserted that if one actually ends up using up all the screen space,
then the function should be broken apart anyways.

also, where I put the opening brace tends to depend some on context:

usually, structs/... use the:
stuff... {
}
style, but functions/methods tend to be:
declaration...
{
...
}

except when they are very small, where I may use an inline form:
declaration...
{ ... }

for if/for/... I tend to leave out the braces unless needed, and will also
have them inlined as above if this looks better.

typical formatting for an if block may be:
if...
...
else ...

braces will generally always be used in an if/else chain:
if...
{ ... }
else if...
{ ... }
else if...
....

since it both avoids a syntactic ambiguity, as well as avoids potential
compiler warnings (some compilers will often start complaining about nested
if/else without braces, as it can become ambiguous). also once before I was
faced with a bug which had resulted from the use of nested if-else without
braces, and confusion about which if/block got the else (at the time, I had
thought it was outward-inward, but later realized it was inward-outward).
the result has since been as a general matter of policy to use braces with
if/else chains.


usually exact naming convention in use depends some on the language in use
and on special cases (in C and C++, I use different naming conventions for
internal and external symbols, typically different uses of capitalization
and different treatment of '_').

in Java I have ended up some using a Z prefix some for internal classes
which otherwise can't be private (often these are the classes which include
all the native calls and VM-specific stuff). technically I end up using
public as they may be used from multiple packages within in the classlib
(thus far this is about the only place I have used this convention), but
ideally client code shouldn't touch them (hence the Z). the Z also serves by
tending to put them at the end of the directory listing.

an example is 'Z_ConsoleOutput' which overrides 'PrintStream' for sake of
sending stuff to the console (IOW: "System.out"). there are various reasons
for doing this (I would rename it "ZConsoleOutput" but this is a hassle
since it uses JNI and I would end up having to mess with its JNI stuff...).

other cases mostly just centralize some stuff (WRT JNI interfaces), which
the Sun apparently was intent to spread "all over the damn place" WRT
designing the classlib (sadly, still woefully incomplete, as a lot of even
basic stuff is stubs...).

note: I don't use Classpath due to trying to avoid GPL, and am not using the
Apache libs here due to operating in a slightly non-standard operating
environment (the Apache libs seem to be more designed with running directly
on the OS in mind).

....


I tend not to use extra whitespace between operators except usually in the
case of && and ||, and often complex expressions use extra parens to
disambiguate (or whenever && or || are involved).

exact spacing policy tends to adapt towards surrounding code. but, for my
own code, I tend to avoid extra spaces.

or such...
 
B

BGB / cr88192

Well, and I believe:

while( *p++ = *q++ );

is an idiom known to every C programmer.

<--
Many compilers will give you a warning for the ";" immediately
following the closing parenthesis without at least white space in
between - likelyhood that the semicolon is there by mistake is too
high. For example

while( *p++ = *q++);
count++;

is surely a bug.
-->

not that I have seen...


in most C and C++ code this practice is sufficiently common that it would
make a lot of code start spewing warnings all over the place if compilers
were to warn about it.

but, then again, some compilers will warn about unused local variables,
which is personally a bit annoying.
hence, it becomes fairly common to ignore many warning conditions until they
become errors if the compiler is intent on giving too many warnings over
pointless crap and one doesn't feel like spending all the extra time needed
to go clean them up...

it is better when warnings are confined to more serious issues, like using
an uninitialized variable, ...


warning: implicit conversion from 'void *' to 'Foo *' without a cast; ...
hence the dreaded "casting the result of malloc" event (some people get
really fussy for or against casting the results of malloc calls...).


admittedly, using a while-loop to perform a copy or similar this way is
generally ugly (usually strcpy or memcpy is preferable, and often faster).

much like, 'strcmp()' is the usual way of copying strings, and is generally
preferable to hand-rolled loops.

but, there are many other cases where one will end up using loops like this.
 
M

markspace

Seriously? We use nothing but where i work. In java and XML, at least -
for some reason, in JSP (well, HTML, really) we tend use two spaces. I
think because JSP/HTML tends to have much more deeply nested constructs.


Yes. It seems like any project I've worked on recently, any attempt to
use tabs just resulted in horribly unreadable code. Primarily my own,
when it was used by others. And this I didn't like.

Tabs work when you can standardize. But to do that reliably, you need
to use something like a vi line: /* :se ts=4 sw=4 */ And I don't see a
lot of this in code anymore.
 
A

Arne Vajhøj

Seriously? We use nothing but where i work. In java and XML, at least -
for some reason, in JSP (well, HTML, really) we tend use two spaces. I
think because JSP/HTML tends to have much more deeply nested constructs.

Most coding conventions say spaces instead of tab.

The standard Java coding convention prescribes either
4sp,8sp,12sp,16sp or 4sp,1t,1t+4sp,2t and mixing tabs
and spaces are not really that good an idea.

Arne
 
A

Arne Vajhøj

2 spaces for indent seems as too little IMO, as it makes it harder to
clearly see the starts and ends of blocks.
Torvalds had defined his coding style rules as using 8-space tabs.
it was asserted that if one actually ends up using up all the screen space,
then the function should be broken apart anyways.

He does not code much Java AFAIK, so ...

Arne
 
A

Arne Vajhøj

one would have to question the competence of the programmers to make this
case.

it becomes a matter of habit when and where to place them, and things are
not exactly looking good for the project if the programmers are not skilled
enough to deal with these sort of issues.

so help you if anything actually difficult pops up...

You seem to have missed one of the most basic problems
in software development.

Developers are humans.

Even if they know things, then they can still get it
wrong.

Competent programmers should certainly know the
semantics of that semicolon, but they can still
miss it when reading the code.

The probability for one programmer missing it for
one specific line is very low.

But the probability for at least one programmer
out of a large team missing it for a large code
base is goes towards certainty for larger
teams and code bases.

Arne
 
A

Arne Vajhøj

BGB / cr88192 wrote:

<snip>

nevermind that we disagree on many matters of style...

I still regard that style is largely unimportant in most cases, since the
language does not require it, and it is not like much actually bad will
happen due to violating some stylistic rule, anymore than fire will fall
from the ceiling due to using the wrong silverware or stiring ones' coffee
with a knife or screwdriver or similar...


any rules one follows are usually due to some specific or potential cost of
violating it, and if a rule does not result in some obvious cost, why
bother?

like the braces:
one can leave them out, and save some characters, and if later they need
multiple statements, they can put them back in. usually the savings (in
terms of readability and screen space) save more than the effort needed to
type them back in later if the code is being edited.

in all likelyhood, if one initially is only typing a single statement,
likely they will never need multiple statements anyways, and hence the
screen-space savings will be of a larger benefit.

Well - you have heard so many people tell you that consistent style
is important for readability.

Then you van believe it or not.

If you don't then we can make our conclusions.
I have written and worked on projects in the Mloc range (including 3D
engines and compilers and similar), and in a number of different programming
languages (C, C++, ASM, Java, C#, ...), so I will contest this description.

If you have written MLOC projects, then we do know your true
expertise.

Arne
 
A

Arne Vajhøj

probably, but OTOH, MS has different guidelines for C#, and C and C++ tend
to use slightly other conventions as well...

in all cases though, most of these conventions are immaterial.

the compiler doesn't care, and there is little reason people should care
either, since a human is smarter and more flexible than a compiler, people
should presumably act as such...

what weight do they hold, as they are basically just statements of
preferences by some party, rather than being based strictly on what is most
practical or convinient, or being imposed by a limitation of the technology,
or similar...

Maybe you should read a tutorial.

In most cases coding conventions do not claim that the choice
is better or more logical than any other convention.

The main point in coding convention is consistency.

You pick a convention and stick to it.

Arne
 
A

Arne Vajhøj

And MS meas what to C or C++? They implement a compiler, they don't
issue the standard. If IEEE or ANSI published standards for C coding,
I'd say we should follow them. It happens that they don't (AFAIK), so
coding standards tend to be wild west style.

If you do serious Win32 API/MFC/ALT programming, then you
better stick to MS coding convention.

A lot of that stuff goes way beyond the C and C++ standards
and are truly MS land.

Arne
 
A

Arne Vajhøj


More odd that you were not aware.
only if one can seriously show that most people are actually effected, in
general, by getting confused over matters of braces and whitespace.

You will have to believe in the experience provided here.
if one looks in 'windows.h' and similar, they see it all over the place, as
well as all-caps type-names...

windows.h is MS technology mid 90's.

They have dropped it for .NET (the MS technology of this decade).
people do so already...

"doing what makes sense" is, maybe 75%-90% of the time or more, following
whatever is the common practice in a particular case...

then a person does something different when there is some good reason to do
so, rather than adhering to dogmas even in cases where it would make the
code worse as a result.

Yes.

But you are the one that has emphasized that there are no
material difference in most cases.

And in this case you should stick to the convention.

Arne
 
A

Arne Vajhøj

yes, but as I see it, it shouldn't matter that much...

The people that will have to maintain your code later may disagree.
but, with space-based tabs, it is a matter of if the tab spot is 4 or 8
spaces ().
a lot of older code used 2-space indentation, but I dislike this and think 4
or 8 is better (and except in rare cases have not seen 2-space indentation
in a long time).

The Java standard is 4.

Arne
 
B

BGB / cr88192

Arne Vajhøj said:
Maybe you should read a tutorial.

In most cases coding conventions do not claim that the choice
is better or more logical than any other convention.

The main point in coding convention is consistency.

You pick a convention and stick to it.

and, in the name of consistency, one can stick to the existing practices...

now, if one goes and looks at the Apache class library, one will see that
they don't exactly strictly follow the style conventions in the tutorial
either...


from what I have seen, the style varies from one place to another, from one
codebase to another, ...
and, FWIW, this doesn't matter much...

about the only real piece of style which really matters all that much is the
naming conventions, and even then, this is only likely a minor issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top