Wednesday, November 26, 2014

The "Conversation" of Computer Science: How Grice's Maxims Make You a Better Coder

Grice’s maxims are an excellent way to try and make sense of how speakers and listeners convey information efficiently and reliably. Does this apply to all languages? Like, say, the Java programming language?  For those who have taken CS classes here, you know how much lecturers and section leaders emphasize “style,” which is following unwritten rules of sorts so others can read your code easily, an industry-must that allows others to go and interpret your code later for maintenance or adding features. Thus, the “speaker” will be the person writing the code, and the “listener” is the person interpreting the code. That being said, how can we relate Grice’s maxims to CS?

Maxims of Quality: How do you lie in code? It’s quite simple really. As a programmer, it’s your responsibility to write names of methods and variables that relate to their actual functionality. For example, if you were to write:

            //This adds one to a number and returns it
public int addOne(int number) {
     int newNumber = number + 2;
     return newNumber;
}

This clearly adds two to a number, not one, thus blatantly lying to the “listener.” Thus, good style is to always put relevant and truthful function names and comments.

Maxim of Relevance: This one can be much more subtle and interesting. Let’s start off with the “while” loop and the “for” loop. The while loop goes on until a condition is met:

            while(conditionIsMet == false) {
                doStuff();
            }

The while loop doesn’t stop until the condition is met hypothetically, and who knows how long that will be. A “for” loop, on the other hand, has a preset number of iterations. For example:

for (int i = 0; i < 5; i++) {
    doStuff();
}

will loop exactly five times. Now, if I wanted an infinite loop, the common way to do this is like:

while(true) {
    doStuff();
}

Another way to do this same thing is like:

for (;;) {
    doStuff() ;
}

The maxim of relevance allows people to make inferences by a particular “utterance.” Using the while loop is relevant as it tells the listener that the loop does not have a preset iteration value. However, using the “for” loop is not relevant as it is not used for its intended purpose, making the “conversation” harder and making the code much harder to interpret, especially for much more complicated algorithms.

Maxims of Quantity: This one actually gets broken all the time. Not naming variables as informative as required can make an interpreter’s job very difficult. Look at the following code:

            int variable1 = doSomething();
            int variable2 = doSomethingElse(variable1);
            int variable3 = handleStuff(variable2, variable1);

What does it do? It’s not lying, as in those methods probably do, in fact, “do something” and “handle stuff,” and those variables are, in fact, variables. But the amount of information is not as informative as required to let the reader know what is going on in the code, making it much harder to follow the "conversation."

Maxims of Manner: Look at the following code:

            int x = 3, y = 5;
            int xor, and, temp;
            and = x & y;
            xor = x ^ y;
            while (and != 0) {
                and <<=1;
                temp = xor ^ and;
                and &= xor;
                xor = temp;
            }
            int result = xor;

Do you have any idea what this does? What if gave you this instead:

            int result = 3 * 5;

Both of these do the same exact thing! The second piece avoids ambiguity, avoids obscurity of expression, is brief and is very orderly compared to the first. The first actually isn’t a violation of quantity because the first piece is “too wordy” as opposed to “giving too much information,” since both pieces give the “same amount of information,” or do the same task. Likewise, the first piece also uses methods that are difficult to understand when there is a much easier way to get this result.

These were all extreme examples, but they illustrate my point. Programmers often make more subtle violations of these maxims, and when it becomes a habit, it makes it incredibly difficult to interpret one’s code, just like bad speaking habits can make it difficult to listen to a speaker. Good “style” in coding is important, and treating programming like a conversation can have such a positive effect on how readable your code is for others.



Followup question: I also thought of a couple of examples of “flouting” maxims in CS, but I ran out of space in my blog. Any ideas about this? Any more examples? Agree or disagree?

5 comments:

  1. This is a pretty entertaining blog post, thanks for writing it. I think Grice would be on the same page with you. Programming languages are highly expressive languages, and although I can't really think of a hypothetical scenario where one would want to obfuscate the true purpose of code, these examples show that the maxims apply to any content that can be expressed. Interestingly, it's important to remember that the language itself is just an abstraction away from what's happening on your processor. In the case of your last example related to manner, what you have written is basically pseudocode for the assembly that would be generated by the compiler after being exposed to the multiplication glyph. That is, the computer has to DO the same thing regardless, its just the readability that is changed. I bring this up because this shows how languages (including Java) have syntactic features, as well as semantic ones, that allow for more dynamic expression in fewer morphological units.

    ReplyDelete
  2. Interesting! In one of my other classes, we are discussing how apparent violations of Grice's maxims give way for implicatures. According to Grice, seeming violations of the maxims allow for implications because they require the listener to either rationalize why the maxim has been violated or discover a second, non-literal meaning of the utterance that allows the maxim not to be violated. For example, if Bob asks why Mary is not at the party, and Sue replies that Mary has a fever, then there is an implicature that the fever is the reason for Mary's absence; this is because Sue's comment would violate the Maxim of Relevance unless her point is that Mary's fever is the reason for her not being at the party. It's interesting to think about the idea of implicatures in the context of computer science. Can we analyze apparent violations in code as possible forms of implicatures? Can we analyze violations in the code comments to expose implicatures within the code?

    ReplyDelete
  3. I have to say, reading this post reminds me of all the API code I've had to slog through just to understand how some arbitrary computer programmer had a death wish for any future CS hopefuls. It also reminds me of my own CS 106A code (woops!). I promise, I was slowly learning the language! It was like I was a baby again!

    Maybe we should give Grice's maxims as a requirement for all intro programmers. I have to say, it might improve style in all CS everywhere.

    But this is why I write Python. You can just read it out loud.

    ReplyDelete
  4. I'm kinda in love with this blog post - so funny and so true. I think giving these maxims to intro programmers would be incredibly useful. Often when I'm teaching young students (or even college students) programming, they'll ask me "why can't I name this variable 'variable1'"? Or they'll want to do something a long convoluted way because then they don't have to learn a shortcut they don't for it. My reasoning for them is always "You want to make your code easy for others to understand."
    Relating CS to Linguistics reminds people that when you write code, you're not just writing it for yourself, you are still writing to communicate with other people. It may serve a more functional purpose than an essay, but it still has to be understandable to others.

    ReplyDelete
  5. Great blog post especially considering I empathize with you. I feel that coding over time makes one more specific in their language or in some other way showing clarity in thought. Just as a conversation will not run smoothly if you violate one of the maxims, neither will your code. Hence, I feel that this aspect of coding is what makes it partly an art because it takes creativity as well as a proclivity for perfectionism to yield successful results. Also I am glad you made me aware of this connection as now I can use this to better remember and apply Grice's Maxims in linguistics as I can relate it to coding.

    ReplyDelete