Chartjunk is ink that does not
 tell the viewer anything new.




Maximize the data:ink
ratio.


                        Kill the frills and
                        get to the point!
The present letter is a very long
       one, simply because I had no
       leisure to make it shorter.




Code is read much more
often than it is written.




             It's harder to read
             code than to write it.
eliminate codejunk

maximize the data:ink ratio
      in your code
"junk" is context-dependent
•   Language
•   IDE
•   Project type
•   Project size
my context
•   Language: Java
•   IDE :          Eclipse
•   Project type : application (not library)
•   Project size : small
codejunk: unnecessary braces



for (int i : array) {   for (int i : array)
    sum += i;               sum += i;
}
codejunk: gratuitous super()


public class Foo {
                          public class Foo {
    int n;
                              int n;

    public Foo(int n) {
                              public Foo(int n) {
        super();
                                  this.n = n;
        this.n = n;
                              }
    }
                          }
}
codejunk: superfluous this



public void increment() {     public void increment() {
    this.total++;                 total++;
    this.notifyListeners();       notifyListeners();
}                             }
codejunk: warts



private String fName;   private String name;
int[] aiValues;         int[] values;
codejunk: split variable declarations




int n;
                    int n = 1;
n = 1;
codejunk: pointless comments

/**
 * @return the bar
 */
public int getBar() {           public int getBar() {
    return bar;                     return bar;
}                               }

/**                             public void setBar(int bar) {
 * @param bar the bar to set        this.bar = bar;
 */                             }
public void setBar(int bar) {
    this.bar = bar;
}
codejunk: commented-out code


int three() {
                               int three() {
    int i;
                                   int i;
    i = 1;
                                   i = 1;
    //System.out.println(i);
                                   i += 2;
    i += 2;
                                   return i;
    return i;
                               }
}
codejunk: unused variables


int three() {
                      int three() {
    int i;
                          int i;
    int n;
                          i = 1;
    i = 1;
                          i += 2;
    i += 2;
                          return i;
    return i;
                      }
}
codejunk: scope end labels



for (int i : array) {   for (int i : array) {
    sum += i;               sum += i;
    count++;                count++;
} // end for            }
codejunk: excessively specific
            variable names
public int countCapitals(String mixedCaseString) {
  int count = 0;
  for (char c : mixedCaseString.toCharArray())
       if (Character.isUpperCase(c))
              count++;
  return count;
}


public int countCapitals(String string) {
  int count = 0;
  for (char c : string.toCharArray())
       if (Character.isUpperCase(c))
              count++;
  return count;
}
codejunk: change comments



for (int i : array) {
                              for (int i : array) {
    // changed 01/20/09 cjm
                                  sum += i;
    sum += i;
                                  count++;
    count++;
                              }
}
codejunk: unnecessary annotations



@Override
                             public String toString() {
public String toString() {
                                 return "something";
    return "something";
                             }
}
codejunk: long temporary variable names



for (int index = 0; index < array.length; index++)
    sum += array[index];




for (int i = 0; i < array.length; i++)
    sum += array[i];
Let your IDE help
Eclipse detects:
• unused imports
• unused variables
• unread variables
• unnecessary else statements
• unnecessary casts
my point
It's not that "anyone        It's that you can benefit
who uses braces is           by being conscious of
evil", or that "you should   codejunk and
not be writing what I        methodical in its
consider codejunk."          definition and
                             application to your
                             projects.

Codejunk Ignitesd

  • 1.
    Chartjunk is inkthat does not tell the viewer anything new. Maximize the data:ink ratio. Kill the frills and get to the point!
  • 2.
    The present letteris a very long one, simply because I had no leisure to make it shorter. Code is read much more often than it is written. It's harder to read code than to write it.
  • 3.
    eliminate codejunk maximize thedata:ink ratio in your code
  • 4.
    "junk" is context-dependent • Language • IDE • Project type • Project size
  • 5.
    my context • Language: Java • IDE : Eclipse • Project type : application (not library) • Project size : small
  • 6.
    codejunk: unnecessary braces for(int i : array) { for (int i : array) sum += i; sum += i; }
  • 7.
    codejunk: gratuitous super() publicclass Foo { public class Foo { int n; int n; public Foo(int n) { public Foo(int n) { super(); this.n = n; this.n = n; } } } }
  • 8.
    codejunk: superfluous this publicvoid increment() { public void increment() { this.total++; total++; this.notifyListeners(); notifyListeners(); } }
  • 9.
    codejunk: warts private StringfName; private String name; int[] aiValues; int[] values;
  • 10.
    codejunk: split variabledeclarations int n; int n = 1; n = 1;
  • 11.
    codejunk: pointless comments /** * @return the bar */ public int getBar() { public int getBar() { return bar; return bar; } } /** public void setBar(int bar) { * @param bar the bar to set this.bar = bar; */ } public void setBar(int bar) { this.bar = bar; }
  • 12.
    codejunk: commented-out code intthree() { int three() { int i; int i; i = 1; i = 1; //System.out.println(i); i += 2; i += 2; return i; return i; } }
  • 13.
    codejunk: unused variables intthree() { int three() { int i; int i; int n; i = 1; i = 1; i += 2; i += 2; return i; return i; } }
  • 14.
    codejunk: scope endlabels for (int i : array) { for (int i : array) { sum += i; sum += i; count++; count++; } // end for }
  • 15.
    codejunk: excessively specific variable names public int countCapitals(String mixedCaseString) { int count = 0; for (char c : mixedCaseString.toCharArray()) if (Character.isUpperCase(c)) count++; return count; } public int countCapitals(String string) { int count = 0; for (char c : string.toCharArray()) if (Character.isUpperCase(c)) count++; return count; }
  • 16.
    codejunk: change comments for(int i : array) { for (int i : array) { // changed 01/20/09 cjm sum += i; sum += i; count++; count++; } }
  • 17.
    codejunk: unnecessary annotations @Override public String toString() { public String toString() { return "something"; return "something"; } }
  • 18.
    codejunk: long temporaryvariable names for (int index = 0; index < array.length; index++) sum += array[index]; for (int i = 0; i < array.length; i++) sum += array[i];
  • 19.
    Let your IDEhelp Eclipse detects: • unused imports • unused variables • unread variables • unnecessary else statements • unnecessary casts
  • 20.
    my point It's notthat "anyone It's that you can benefit who uses braces is by being conscious of evil", or that "you should codejunk and not be writing what I methodical in its consider codejunk." definition and application to your projects.