A Record Class Type: Bad Style?

G

George W. Cherry

Chris Uppal said:
George said:
int[] count = countVowelsConsonantsOthers(str);
System.out.println(str);
System.out.println( "Number of vowels = " + count[0] );
System.out.println( "Number of consonants = " + count[1] );
System.out.println( "Number of others = " + count[2] );

Now that /is/ execrable.

I'm sorry but there's no more polite word for it. I realise that you are
only
exploring the options available to you, rather than seriously proposing
this as
the best solution (as least I /hope/ that's the case ;-), but this passes
beyond sane program design.

That bad, huh? Here it is for the Java Hall of Insane Shame.

public static int[] countVowelsConsonantsOthers(String str) {
int vowels = 0, consonants = 0, others = 0;
for (int j = 0; j < str.length(); j = j + 1) {
char ch = str.charAt(j);
if ( Character.isLetter(ch) ) {
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
vowels++;
break; //end cases for vowels

default:
consonants++;
//end ch is a letter but not a vowel
}//end switch statement
}
else { //ch is not a letter
others++;
}
}//end for loop
assert vowels + consonants + others == str.length();
return new int[]{vowels, consonants, others};
}//end countVowelsConsonantsOthers method

George
 
G

George W. Cherry

Ann said:
George W. Cherry said:
Ann said:
<snip>
I didn't intend to appear to diss your suggestion.
Indeed, I think it has merit. My problem with returning
an int array is that the user does not know which values
int[0], int[1], and int[2] refer to.

I'm sort of jumping into this discussion in the middle
but it seems to me that if you provide a method and the
user doesn't know what is returned that s/he would have
little incentive to invoke it.

Indeed. The problem is that the user knows that the
method returns the number of vowels, the number of
consonants, and the number of other characters in
the supplied string, but wouldn't know which element
of the returned array referred to which count.

Since you are the program author, you can tell her on the phone.

API by telephone, huh?
 
G

George W. Cherry

Ann said:
Since your written explanation in the documentation is insufficient.

Aha! You're right!. Since I don't like to share my telephone number
with strangers, I better improve the documentation (as I have below).

/**
* Returns:
* in return value int[0], the number of vowels in parameter str;
* in return value int[1], the number of consonants in parameter str;
* in return value int[2], the number of other characters in parameter
str.
*/
public static int[] countVowelsConsonantsOthers(String str) {
int vowels = 0, consonants = 0, others = 0;
for (int j = 0; j < str.length(); j = j + 1) {
char ch = str.charAt(j);
if ( Character.isLetter(ch) ) {
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
vowels++;
break; //end cases for vowels

default:
consonants++;
//end ch is a letter but not a vowel
}//end switch statement
}
else { //ch is not a letter
others++;
}
}//end for loop
assert vowels + consonants + others == str.length();
return new int[]{vowels, consonants, others};
}//end countVowelsConsonantsOthers method

What else could I do to make this tiny example less insane?
(Huh, Chris)?

George
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top