Blog - Coding Style Conventions

I write a lot of code, and here are a few little stylistic things I try to always do now.

1) Mandatory comment when passing boolean flags or weird random args: I learned this one when interning at Facebook. What's the third arg here?

processDatabase(server, "Users", true);

No more mystery flags! You must comment it so I know what the heck I'm passing and what it means.

processDatabase(server, "Users", /*overwrite*/ true);

2) Mandatory comment on an empty block: Sometimes I have an empty block or empty loop or whatever. But here's one in my code; did I mean to do that? Is it a bug? Just comment it and be sure. Got this one from Stanford Prof. Eric Roberts.

// should this ctor be empty?? (bad)
public FluxCapacitor() {
}

// this one is better
public FluxCapacitor() {
    // empty
}

3) Author/version comments on files: It seems like a simple/obvious thing, but just tagging my files with my name and when I last modified them has helped me a lot with "is this the newest copy of this file?" issues. I also put in what I changed between each version.

/**
 * FluxCapacitor.java
 * ........ (description)
 * @author Marty Stepp
 * @version 2015/04/15
 *  - fixed bug with null pointer in query function
 *  - alphabetized function names
 * 2015/01/29
 *  - added getRandomBlerg() method
 * ...
 */

4) Two-letter for-loop counter variable names: It's a nightmare to search for "int i" in your code. How about two letters?

for (int ii = 0; ii < 10; ii++) {
    for (int jj = 0; jj < ii; jj++) {
        ...
    }
}

What are some little style rules that you love to follow in your own code?

Valid HTML Valid CSS
last updated 2019/08/15