![]() |
Stylistic note on loops
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? |
Re: Stylistic note on loops
On Oct 21, 1:34*pm, markspace <nos...@nowhere.com> 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 |
Re: Stylistic note on loops
On Oct 21, 12:34*pm, markspace <nos...@nowhere.com> 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 "-". |
Re: Stylistic note on loops
On Oct 21, 10:58*am, Lew <l...@lewscanon.com> wrote:
> On Oct 21, 1:34*pm, markspace <nos...@nowhere.com> 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 |
Re: Stylistic note on loops
On Oct 21, 1:34*pm, markspace <nos...@nowhere.com> 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 |
Re: Stylistic note on loops
"markspace" <nospam@nowhere.com> wrote in message news:i9ptik$rcd$1@news.eternal-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... |
Re: Stylistic note on loops
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; } |
Re: Stylistic note on loops
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 |
Re: Stylistic note on loops
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 |
Re: Stylistic note on loops
"Lew" <lew@lewscanon.com> wrote in message news:68aa61a9-5999-410a-92d9-909966367f38@w21g2000vby.googlegroups.com... 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. |
| All times are GMT. The time now is 12:14 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.