Coding 101, by John Keklak

After my CS411 class this week, I am completely inspired to post what must be made clear as a message to the world courtesy of the teachings of one John Keklak. And that is coding standards. So here we go:

  1. Indentation with discipline. Not only must you indent every time you start a new line, but you want to make sure your code makes the person who reads it feel like they’re being punished by reading the code. After all, we don’t just write code. We go through a process. For example:
  2. int getNumTasks(table){
    int a = numRows(table);
    return a;
    }

    Now this, who can read this? I mean it’s a bunch of gibberish and really, what do I get out of it? But take this example:

    int getNumYOUREANIDIOT(GO TO THE CORNER OF THE ROOM){

    int YOURENOTALLOWEDTOWATCHTVTONIGHT = YOURE PATHETIC!;

    return YOUREGOINGTOBESERVEDTOTHESHARKS;

    }

    Not only is that readable, but it tells you a message. And it disciplines the reader. I can’t tell you how much that makes coding in real projects go much quicker. Pink slips have disappeared thanks to this method.

  3. Match your curly braces and make sure that braces have their own lines to add whitespace. You cannot even begin to understate the importance of white space. The program is much more readable when you have much more white space. Take this example:
  4. int getRow(int mat[][], int entry){
    for( i = 0; i < N; i++){
    for( j = 0; j < N; j++){
    if(mat[i][j] == entry){
    return i;
    }
    }
    }
    }

    Not only is this not only not indented, but man is that hard to read. And those braces, can we get them a new line please? They’re much more important than the actual code written below them. Let’s try again:

    int getRow(int mat[][], int entry)
    {

    for( i = 0; i < N; i++)
    {

    for( j = 0; j < N; j++)
    {

    if(mat[i][j] == entry)
    {

    return i;

    }

    }

    }

    }

    Wow, that is so much better. In fact, I think I’m starting to like your coding. But we are still not readable enough yet. I mean, I can still very easily glance over minute insubstantial details that I don’t need to know how they work because of this. Let’s look at this example:

    int getRow(int mat[][], int entry)
    {
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace

    for( i = 0; i < N; i++)
    {
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace

    for( j = 0; j < N; j++)
    {
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace

    if(mat[i][j] == entry)
    {
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace

    return i;

    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    }

    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    }

    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    }

    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    whitespace
    }

  5. Now we know that it is of utmost important to have organized code. But we also want to push down any irrelevant information. We must oppress that information, because obviously it is not as worthy of being in the code. We could define r = arc->radius; but that’s just very unclear compared to r = arc->getRadius();. Now I know you’re looking at this and saying “Gee, they seem to be the same.” But that is where you are mistaken. We never want to directly reference an object data member because we just don’t know what people put in them. It’s like a guy’s pornography collection. When I worked for this Russian guy, he had a porno stash that was so vile I can’t even begin to describe what was in it. So instead we just assume that the getRadius() function is much more supreme and flawless and reference that.
  6. Finally, we must decouple our logic and our objects. I won’t even define what it means to decouple, I take it for granted that you know what I mean. We want our ship and message handler to be separate. The ship’s not the one making the messages. Let’s say we have a function on a list of objects, and in this function, for each object, we save the object then we free it. Are you just going to write the function like that? The answer is no. If you have not found a way to decouple the functions so much that you can no longer recognize what does what in your program, you have failed to write good code. Remember, if we don’t have at least 5 functions to do 1 line of code, then you haven’t decoupled enough.

And that, students, is coding 101 from your CS411 professor.

(It was very tough to remain in his voice. I had to resist many many sarcastic and smart-aleck comments.)

About these ads

3 Responses to Coding 101, by John Keklak

  1. […] I came across this WordPress blog post today. The author breaks down some fundamental guidelines for coding. To summarize: You should take as much care formating code as you would with an essay. This is good advice. I’m still a beginner when it comes to heavy lifting with Objective C, but I have already learned this lesson the hard way. It is much easier to find mistakes when you have the proper punctuation. Just imagine writing an essay without carriage returns, indentation, page breaks or punctuation. It would be pretty tough finding where you forgot to put that closing quote. […]

  2. Hi!

    What do you think about love? >:)

  3. Idetrorce says:

    very interesting, but I don’t agree with you
    Idetrorce

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: