Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pointers

Name: Anonymous 2010-09-16 5:42

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;
}


Which do you prefer?

Name: Anonymous 2010-09-16 14:32

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;
            }
        }
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List