I'm finally reading my K&R, and one thing strikes me: some of these examples are pretty darn obtuse. Please tell me shit like this is frowned upon in the real world:
// Paraphrasing from the ``find'' program in 5.10
#include <stdio.h>
int main(int argc, char *argv[])
{
char c;
while (--argc > 0 && (*++argv)[0] == '-')
while ((c = *++argv[0]))
/* code */
return 0;
}
Seriously, /prog/, seriously? IMO the following is much clearer, although obviously much longer:
#include <stdio.h>
int main(int argc, char *argv[])
{
char c, *s;
int i;
for (i = 1; i < argc; i++) {
s = argv[i];
if ((c = *s) == '-')
while ((c = *++s))
/* code */
else
break;
}
return 0;
}
I scoff at your NEWBIE JAVA PROGRAMMING STYLE, and hereby submit my own.
public class MyFirstJavaWebApplication
{
public final static Integer ZERO = 0;
public final static Integer ONE = 1;
public final static Integer INCREMENT_ONE = 1;
public final static Integer EXCEPTION_LINE_FIRST = 25; // change this if you alter the code
public final static Integer EXCEPTION_LINE_SECOND = 33; // also change this to keep the error messages up to date so the user if not left in the dark
public static void main(String[] myArguments)
{
Integer myArgumentCounter = ZERO; // set myArgumentCounter to zero (0)
final Integer myLastArgumentIndex = myArguments.length; // save a copy of myArguments's size into myLastArgumentIndex so we can use it in the for loop
Boolean myArgumentsForLoopFinished = Boolean.FALSE;
for(myArgumentCounter = ZERO;
myArgumentCounter < myLastArgumentIndex && myArgumentsForLoopFinished;
myArgumentCounter = myArgumentCounter + INCREMENT_ONE // this may be confusing, but it increases myArgumentCounter. I strongly advise learning a language like Go, which uses the much more sensible ``:='' for assignment of variables
) // close bracket for the for loop
{
// executed once for each argument in the array of Strings ``myArguments''
//
if(myArgumentCounter < ZERO || myArgumentCounter > myLastArgumentIndex){
System.out.println("Program bug occured at line " + EXCEPTION_LINE_FIRST + "!!!!");
System.exit(ONE); // VERY IMPORTANT to tell the Operating System that we did not successfully execute
}
char myArgumentsFirstCharacter;
try{
myArgumentsFirstCharacter = myArguments[myArgumentCounter].charAt(ZERO);
}catch(IndexOutOfBoundsException myIndexOutOfBoundsException){
System.out.println("Program bug occured at line " + EXCEPTION_LINE_SECOND + ", incorrect index!!!");
System.exit(ONE);
return; // this must be here in case the above function call to System.exit fails, so that myArgumentsFirstCharacter is always initialised and we have no undefiend behaviour
}
String myArgumentsFirstCharacterString = "" + myArgumentsFirstCharacter;
String myArgumentsFirstCharacterStringComparator = "" + '-'; // this creates a string containing a single hypen
if(myArgumentsFirstCharacterStringComparator.equals(myArgumentsFirstCharacterString)){
// they are equal, put the code here
// code
}else{
myArgumentsForLoopFinished = Boolean.TRUE;
}
}
}
}
>>1
The first is immediately clear to me. The second takes me longer to digest, but I'd never write like either of them. In the first version, those two while()s make fine examples of one another.
Also, OP, you missed a bracket: while (--argc > 0 && (*++argv)[0] == '-'))
Name:
Anonymous2010-09-16 21:10
>>1
That is one hell of a beautiful use of sequence points, operator precedence, and statement grouping rules. I should probably read K&R again and look at the code more carefully.
>>15
Parenthesis, paren is also acceptable.
() - parenthesis (sing. parenthesis) or parens (sing. paren)
[] - brackets ("square brackets")
{} - braces ("curly braces", "curly brackets")
# - number sign (pound sign, hash, crosshatch, octothorpe)
" - quotation mark (double quote)
' - apostrophe (single quote, "prime")
NOTE: prefer U+2018-U+201F where possible instead of
double and single quotes, or U+2032-U+2034 as
[double/triple] prime, minutes, seconds, feet, and
inches
& - ampersand (and)
* - asterisk (times)
NOTE: U+00D7 and U+2A2F are multiplication operators,
and an "asterisk operator" is available at U+2217
/ - slash (solidus, virgule, "stroke")
NOTE: U+2215 is a more specific division sign but
it is not necessary.
` - grave accent ("backtick")
NOTE: not generally acceptable as an opening single quote
(that is, except in TeX)
| - pipe (vertical line, vertical bar)
NOTE: box drawing characters are also available
starting at U+2500
~ - tilde
< - less than - greater than
NOTE: prefer U+2329 and U+232A as angle brackets,
where possible
- - hyphen
NOTE: prefer U+2010-U+2015 for dashes, and prefer
U+2212 for minus signs. An en dash is an
acceptable substitute for a minus sign.