Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Stylistic note on loops

Reply
Thread Tools

Stylistic note on loops

 
 
markspace
Guest
Posts: n/a
 
      10-21-2010
I'm doing some text parsing and I'd appreciate some input on code styles
for loops.

A lot of the processing I'm currently doing involves finding a
particular character, and then doing something with the text up to that
point. For example:

int i;
for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
{}
// do something with i and s here

This marches ahead until it find a character that isn't a "letter",
then, with i set to the offset of the last letter+1, is able to do
something with that group of letters in string s.

My question is on the code style I used here. The for loop looks a
little funny. I think it's the best because all the terms (is that
right? terms = the bits between the semicolons) of the for statement do
the right thing and are in the standard position.

However, the body is empty, which is weird, and i has to be declared
outside of the for loop so I can use it later, which is a little weird.

Any thoughts on how to format this? Would a while loop be better or
more readable here?

int i = 1;
while( Character.isLetter( s.charAt( i ) ) ) {
i++;
}

That while loop wasn't tested, but should do the same thing as the for
loop above. Any ideas?

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      10-21-2010
On Oct 21, 1:34*pm, markspace <(E-Mail Removed)> wrote:
> I'm doing some text parsing and I'd appreciate some input on code styles
> for loops.
>
> A lot of the processing I'm currently doing involves finding a
> particular character, and then doing something with the text up to that
> point. *For example:
>
> * * *int i;
> * * *for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
> * * *{}
> * * *// do something with i and s here
>
> This marches ahead until it find a character that isn't a "letter",
> then, with i set to the offset of the last letter+1, is able to do
> something with that group of letters in string s.
>
> My question is on the code style I used here. *The for loop looks a
> little funny. *I think it's the best because all the terms (is that
> right? terms = the bits between the semicolons) of the for statement do
> the right thing and are in the standard position.
>
> However, the body is empty, which is weird, and i has to be declared
> outside of the for loop so I can use it later, which is a little weird.
>
> Any thoughts on how to format this? *Would a while loop be better or
> more readable here?
>
> * *int i = 1;
> * *while( Character.isLetter( s.charAt( i ) ) ) {
> * * * i++;
> * *}
>
> That while loop wasn't tested, but should do the same thing as the for
> loop above. *Any ideas?
>


Use what you like, that's why it's called "style". I strongly
recommend against single-letter variable names like "i" and "s",
though.

Personally I prefer the 'for' version. There's nothing "weird" about
the index being declared outside the loop body since that's driven by
the actual scope needed. The only thing "weird" is to declare a
variable for a different scope than it needs, but you're not doing
that.

There is equally nothing "weird" about an empty loop body. It's done
all the time. In fact, that's part of why the 'for' syntax is what it
is, going back to 'C' days and beyond - to support compact expression
of a loop.

Go with your 'for' loop, it's bog standard.

--
Lew
 
Reply With Quote
 
 
 
 
bcr666
Guest
Posts: n/a
 
      10-21-2010
On Oct 21, 12:34*pm, markspace <(E-Mail Removed)> wrote:
> I'm doing some text parsing and I'd appreciate some input on code styles
> for loops.
>
> A lot of the processing I'm currently doing involves finding a
> particular character, and then doing something with the text up to that
> point. *For example:
>
> * * *int i;
> * * *for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
> * * *{}
> * * *// do something with i and s here
>
> This marches ahead until it find a character that isn't a "letter",
> then, with i set to the offset of the last letter+1, is able to do
> something with that group of letters in string s.
>
> My question is on the code style I used here. *The for loop looks a
> little funny. *I think it's the best because all the terms (is that
> right? terms = the bits between the semicolons) of the for statement do
> the right thing and are in the standard position.
>
> However, the body is empty, which is weird, and i has to be declared
> outside of the for loop so I can use it later, which is a little weird.
>
> Any thoughts on how to format this? *Would a while loop be better or
> more readable here?
>
> * *int i = 1;
> * *while( Character.isLetter( s.charAt( i ) ) ) {
> * * * i++;
> * *}
>
> That while loop wasn't tested, but should do the same thing as the for
> loop above. *Any ideas?


I frequently use a for loop to empty a table something like:
for (;table.getRowCount() > 0;table.removeRow(0));
notice, you don't even need the body {}, if your body is empty or one
line, you can use just a ; to end.

You mention though that you are looking for a particular character,
maybe you should use "some-string-is-here".split("-"). This will give
you an String[] of strings without the -. Another option is "some-
string-is-here".firstIndexOf("-") will tell you the position of the
first "-".
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      10-21-2010
On Oct 21, 10:58*am, Lew <(E-Mail Removed)> wrote:
> On Oct 21, 1:34*pm, markspace <(E-Mail Removed)> wrote:
>
>
>
>
>
> > I'm doing some text parsing and I'd appreciate some input on code styles
> > for loops.

>
> > A lot of the processing I'm currently doing involves finding a
> > particular character, and then doing something with the text up to that
> > point. *For example:

>
> > * * *int i;
> > * * *for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
> > * * *{}
> > * * *// do something with i and s here

>
> > This marches ahead until it find a character that isn't a "letter",
> > then, with i set to the offset of the last letter+1, is able to do
> > something with that group of letters in string s.

>
> > My question is on the code style I used here. *The for loop looks a
> > little funny. *I think it's the best because all the terms (is that
> > right? terms = the bits between the semicolons) of the for statement do
> > the right thing and are in the standard position.

>
> > However, the body is empty, which is weird, and i has to be declared
> > outside of the for loop so I can use it later, which is a little weird.

>
> > Any thoughts on how to format this? *Would a while loop be better or
> > more readable here?

>
> > * *int i = 1;
> > * *while( Character.isLetter( s.charAt( i ) ) ) {
> > * * * i++;
> > * *}

>
> > That while loop wasn't tested, but should do the same thing as the for
> > loop above. *Any ideas?

>
> Use what you like, that's why it's called "style". *I strongly
> recommend against single-letter variable names like "i" and "s",
> though.
>
> Personally I prefer the 'for' version. *There's nothing "weird" about
> the index being declared outside the loop body since that's driven by
> the actual scope needed. *The only thing "weird" is to declare a
> variable for a different scope than it needs, but you're not doing
> that.
>
> There is equally nothing "weird" about an empty loop body. *It's done
> all the time. *In fact, that's part of why the 'for' syntax is what it
> is, going back to 'C' days and beyond - to support compact expression
> of a loop.
>
> Go with your 'for' loop, it's bog standard.
>


It is suspicious that you start the loop at the second character
(i=1). This will likely cause problems when s is empty or the first
character is not a letter.
--
Fred K
 
Reply With Quote
 
Tom McGlynn
Guest
Posts: n/a
 
      10-21-2010
On Oct 21, 1:34*pm, markspace <(E-Mail Removed)> wrote:
> I'm doing some text parsing and I'd appreciate some input on code styles
> for loops.
>
> A lot of the processing I'm currently doing involves finding a
> particular character, and then doing something with the text up to that
> point. *For example:
>
> * * *int i;
> * * *for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
> * * *{}
> * * *// do something with i and s here
>
> This marches ahead until it find a character that isn't a "letter",
> then, with i set to the offset of the last letter+1, is able to do
> something with that group of letters in string s.
>
> ...


> Any thoughts on how to format this? *Would a while loop be better or
> more readable here?
>


While I don't have any problem with the style for the problem as
stated, I think it tends to degrade when there are more conditions,
and I often find there are. E.g., if you aren't guaranteed that there
is any non-alphabetic character then you need:

for (i=1; i<s.length() && Character.isLetter(s.charAt(i)); i += 1)
{
}

This I would prefer to see as:

for (i=1; i<s.length(); i += 1) {
if (Character.isLetter(s.charAt(i)) {
break;
}
}

The extra test is also needed if, as another poster noted, the string
may only be one character long.

This separates the loop structure from the logic test which I find
easier to follow.

Regards,
Tom McGlynn
 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      10-21-2010

"markspace" <(E-Mail Removed)> wrote in message
news:i9ptik$rcd$(E-Mail Removed)-september.org...
> I'm doing some text parsing and I'd appreciate some input on code styles
> for loops.
>
> A lot of the processing I'm currently doing involves finding a particular
> character, and then doing something with the text up to that point. For
> example:
>
> int i;
> for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
> {}
> // do something with i and s here
>
> This marches ahead until it find a character that isn't a "letter", then,
> with i set to the offset of the last letter+1, is able to do something
> with that group of letters in string s.
>
> My question is on the code style I used here. The for loop looks a little
> funny. I think it's the best because all the terms (is that right? terms
> = the bits between the semicolons) of the for statement do the right thing
> and are in the standard position.


we call these 'expressions'.
not to be confused with 'statements', which are what the variable
declaration and for loop are.


> However, the body is empty, which is weird, and i has to be declared
> outside of the for loop so I can use it later, which is a little weird.
>


actually, the braces can be omitted here, hence:
for( i = 1; Character.isLetter( s.charAt( i) ); i++ );

braces generally only really serve the purpose of lumping multiple
statements together into a single statement block, and so are typically left
out when there is only a single statement.

further more, if there is no statement, a single ';' can be used instead,
which is usually understood as being an "empty statement" or "null
statement", ane may be used with 'for' and 'while' loops, or wherever else
they may be useful.


note:
declaring variables outside the loop is also fairly common, and in fact many
people use this as their primary style (it is declaring variables inside the
for loop which is itself unusual...).

anyways, functionality is more important than some perception of style.


> Any thoughts on how to format this? Would a while loop be better or more
> readable here?
>
> int i = 1;
> while( Character.isLetter( s.charAt( i ) ) ) {
> i++;
> }
>
> That while loop wasn't tested, but should do the same thing as the for
> loop above. Any ideas?
>


again, the braces are optional.

while( Character.isLetter( s.charAt( i ) ) ) i++;
or, taken further:
while(Character.isLetter(s.charAt(i++)));

or, if Java were C or C++...:
while(isalpha(s[i++]));
or:
while(isalpha(*s++));
(the above will not work in Java, but are meant more as illustration).


but, to a large degree, style is irrelevant...
it is much more important that code work than that it adhere to any
particular sense of aesthetics, or at least within the realm of basic sanity
(for example, using indentation and not organizing code into a single big
lump of characters and similar...).

even then, there are cases where it is infact nicer to lump a bunch of
statements onto a single line, and other cases where it is best to insert
extra whitespace, and so no simple rules are always ideal.


someone here also advocated the use of longer / "descriptive" variable
names, but again, this is another one of those points of conflict, and is
ultimately irrelevant and depending primarily what one is doing. the main
cost of longer names is that they take more screen space and more mental
letter-space to store and work with, which is a drawback, and also longer
names can become particularly awkward.

so, single letter names are convinient and usually "obvious enough", which
leaves longer names (words, multi-words, ...) mostly for cases where the
usage of a variable is not immediately obvious or driven by convention (most
single-letter variable names are somewhat driven by informal conventions, so
it is understood what the names are regardless of their brevity).

in some cases, one may also have reason for using "hungarian notation",
although people get into arguments over this as well.

but, ultimately, it is all pointless.
do what makes sense in a given situation, and this is good enough...



 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      10-21-2010
On 10/21/2010 11:10 AM, bcr666 wrote:

> You mention though that you are looking for a particular character,
> maybe you should use "some-string-is-here".split("-"). This will give
> you an String[] of strings without the -. Another option is "some-
> string-is-here".firstIndexOf("-") will tell you the position of the
> first "-".



This is good advice. split() isn't exactly a good match here but
firstIndexOf() will be useful, thanks for reminding me.

Here's another one, designed to skip over a set of characters. I
thought I could implement the inner loop with skip.contains( s.charAt(
cursor) ) but contains only takes CharSequence, not char. Bummer.

public static int skip( String s, int offset, String skip ) {
int cursor = offset;
testing_string:
for( ; cursor < s.length(); cursor++ ) {
for( int i = 0; i < skip.length(); i++ ) {
if( s.charAt( cursor ) == skip.charAt( i ) ) {
continue testing_string;
}
}
break testing_string;
}
return cursor;
}


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-21-2010
BGB / cr88192 wrote:
> actually, the braces can be omitted here, hence:
> * * *for( i = 1; Character.isLetter( s.charAt( i) ); i++ );
>
> braces generally only really serve the purpose of lumping multiple
> statements together into a single statement block, and so are typically left
> out when there is only a single statement.
>


"... are typically left out ..." - you make that sound like such a
universal rule when in fact most places mandate the opposite, and most
writers recommend the opposite, that the braces are needed even for a
single body statement to prevent anomalies due to obfuscatory
indentation during a refactoring or enhancement activity.

I suggest that the best practice of using curly braces even for single-
statement bodies is what is "typical". If not, it should be.

> further more, if there is no statement, a single ';' can be used instead,
> which is usually understood as being an "empty statement" or "null
> statement", ane may be used with 'for' and 'while' loops, or wherever else
> they may be useful.
>
> note:
> declaring variables outside the loop is also fairly common, and in fact many
> people use this as their primary style (it is declaring variables inside the
> for loop which is itself unusual...).
>


Huh? Wha...?

This isn't a popularity contest. You declare a variable for the scope
for which it applies. If a variable has no use outside the loop body,
it should be declared inside the loop body. What patzer programmers
do notwithstanding.

Where are you getting your statistics, anyway? Declaring variables
inside a loop body is darn-near universal. How in the seven worlds do
you call that "unusual"?

> anyways, functionality is more important than some perception of style.
>


Precisely, so declare variables outside the loop if, and only if,
they're used outside the loop.

markspace wrote:
>> * int i = 1;
>> * while( Character.isLetter( s.charAt( i ) ) ) {
>> * * *i++;
>> * }

>


BGB / cr88192 wrote:
> again, the braces are optional.
>


By the rules of the language, not by best practice.

> while( Character.isLetter( s.charAt( i ) ) ) i++;
>


Ewwwww!

> or, taken further:
> while(Character.isLetter(s.charAt(i++)));
>


Do you enjoy making your code difficult for others to read?

> but, to a large degree, style is irrelevant...
>


Nope.

To a large degree, style is relevant - it's to the degree that people
need to work comfortably together.

> it is much more important that code work than that it adhere to any
> particular sense of aesthetics, or at least within the realm of basic sanity
> (for example, using indentation and not organizing code into a single big
> lump of characters and similar...).
>


I thought from your code snippets that you were in favor of "a single
big lump of characters and similar...". I'm pleased to be mistaken.

> even then, there are cases where it is infact nicer to lump a bunch of
> statements onto a single line, and other cases where it is best to insert
> extra whitespace, and so no simple rules are always ideal.
>


But simple principles are ideal - make code easy to read and
understand, and hard to misconstrue or screw up.

> someone here also advocated the use of longer / "descriptive" variable
> names, but again, this is another one of those points of conflict, and is
>


It's only a "conflict" between those who care about maintainability
and those who utterly lack professionalism. The former have no issue
with using longer, more readable names. The latter have no clue why
it's important.

> ultimately irrelevant and depending primarily what one is doing. the main
> cost of longer names is that they take more screen space and more mental
> letter-space to store and work with, which is a drawback, and also longer
> names can become particularly awkward.
>


What you're saying is true for names that are "too long", but not
names that are "longer". Descriptive names are not perceived
psychologically as "letter-space", whatever the freak that is supposed
to mean, but as morphemes and memes - i.e., the mind chunks the
information, so your claim about "more mental letter-space" is as
bogus as the terminology.

> so, single letter [sic] names are convinient and usually "obvious enough", which
>


Sez you! You aren't really correct, but it's a nice opinion.

Single-letter names are only "convenient" for the lazy author.
They're a lot of work for the diligent maintainer. I In a word,
they're selfish.

> leaves longer names (words, multi-words, ...) mostly for cases where the
> usage of a variable is not immediately obvious or driven by convention (most
>


or for where you want clarity, lack of error, maintainability,
professionalism, ...

> single-letter variable names are somewhat driven by informal conventions, so
> it is understood what the names are regardless of their brevity).
>


"It is understood" - you are fond of using the passive voice to make
your minority opinions sound like Universal Truth.

> in some cases, one may also have reason for using "hungarian notation",
> although people get into arguments over this as well.
>


Probably because there's no use for it in Java, and it violates the
well-founded and well-understood principles of encapsulation and
information-hiding.

> but, ultimately, it is all pointless.
>


Now we're talking about life in general.

> do what makes sense in a given situation, and this is good enough
>


Spoken like an amateur.

--
Lew
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      10-21-2010
On 21-10-2010 13:34, markspace wrote:
> I'm doing some text parsing and I'd appreciate some input on code styles
> for loops.
>
> A lot of the processing I'm currently doing involves finding a
> particular character, and then doing something with the text up to that
> point. For example:
>
> int i;
> for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
> {}
> // do something with i and s here
>
> This marches ahead until it find a character that isn't a "letter",
> then, with i set to the offset of the last letter+1, is able to do
> something with that group of letters in string s.
>
> My question is on the code style I used here. The for loop looks a
> little funny. I think it's the best because all the terms (is that
> right? terms = the bits between the semicolons) of the for statement do
> the right thing and are in the standard position.
>
> However, the body is empty, which is weird, and i has to be declared
> outside of the for loop so I can use it later, which is a little weird.
>
> Any thoughts on how to format this? Would a while loop be better or more
> readable here?
>
> int i = 1;
> while( Character.isLetter( s.charAt( i ) ) ) {
> i++;
> }
>
> That while loop wasn't tested, but should do the same thing as the for
> loop above. Any ideas?


I would go for the while loop in this case.

It is logically a while operation and not a for operation.

Arne
 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      10-21-2010

"Lew" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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.


> in some cases, one may also have reason for using "hungarian notation",
> although people get into arguments over this as well.
>


<--
Probably because there's no use for it in Java, and it violates the
well-founded and well-understood principles of encapsulation and
information-hiding.
-->

it is useful in those cases where otherwise it would be difficult to come up
with good non-clashing variable names, or in cases where several variables
are closely related but represent different entities. hungarian notation
allows the basic-name to be used multiple times.

granted, it is pointless for methods given the existence of overloading...


> but, ultimately, it is all pointless.
>


<--
Now we're talking about life in general.
-->

well, more specifically, about language style and debating over language
style.
what really does it matter?


> do what makes sense in a given situation, and this is good enough
>


<--
Spoken like an amateur.
-->

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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Stylistic note on loops Stefan Ram C Programming 161 11-21-2010 02:08 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Stylistic question -- initialization lists and vectors Denis Remezov C++ 4 04-30-2004 05:10 PM
stylistic use ofexplicit this. P.Hill Java 13 04-23-2004 04:21 AM
stylistic prototype question Mantorok Redgormor C Programming 5 10-30-2003 01:51 PM



Advertisments