June 27, 2006

Raw code v fast code v readable code

Writing about web page http://hanuska.blogspot.com/2006/03/raw-code-vs-readable-code-vs-fast-code_23.html


Nice article.

- 6 comments by 2 or more people Not publicly viewable

[Skip to the latest comment]
  1. Chris May

    05     // change first character to lower-case
    06 final int ONE = 1;
    07 String firstChar = attribute.substring(0, ONE).toLowerCase();
    08 return firstChar + attribute.substring(ONE);
    mmm. a constant called ONE. That's much better.

    27 Jun 2006, 14:21

  2. Chris May

    Actually, the more I read the example, the less I like it. It's frightfully procedural; why not have a Method object with isAttributeAccessor() and getAttributeName() methods? Then if you cared about performance you could (a) let the Method object cache it's attribute info internally, and (b) cache Method objects in a map, so that you didn't execute the string–mangling logic too often.

    I wonder idly if there isn't already a class somewhere in the JRE to do this, since it's pretty fundamental to lots of reflection / javabeans stuff. I bet a quick search through the Spring source would find a good way of doing it.

    27 Jun 2006, 14:37

  3. C

    I guess the example is posted because it is a good example of refactoring to improve readability, not because it is the best solution to a relatively common problem.

    I agree with you; the code listing falls down on many fronts, but that is almost irrelevant

    27 Jun 2006, 19:24

  4. Chris May

    The thing is, I don't think it's a good example of refactoring to improve readability. It's improved it a little bit, but for a relatively small investment of effort it could have been so much better. I actually think that constants like ONE and PREFIX_LENGTH decrease readability, not increase it, and just converting a 9-line method into 3 3-line ones really isn't helping. I wonder if the author actually tested the before and after versions with anyone to see if they genuinely did find them easier to comprehend.

    27 Jun 2006, 23:14

  5. Lets agree to disagree then ;)

    I personally think:

    private static String getAttribute(String method) {
    final int PREFIX_LENGTH = 3; // first 3 chars (ie, "get" or "set")
    String attName = method.substring(PREFIX_LENGTH);
    return isPrimaryKey(attribute) ? "" : lowerFirstChar(attribute);
    is much more descriptive, and therefore readable then:
    private String getAttribute(String method) {
    // get past the first 3 chars (ie, "get" or "set").
    String retVal = method.substring(3);
    retVal = checkIfPrimaryKey(retVal);
    if (!"".equals(retVal) && retVal.equals(method.substring(3))) {
    String firstChar = retVal.substring(0,1).toLowerCase();
    retVal = firstChar + retVal.substring(1);
    return retVal;

    The first listing is describing the intention of the code, the second listing is purely the implementation…what does "retVal" mean, what does the magic number 3 represent.

    I absolutely agree that the code could be improved further (being consistent with naming, i.e. getProperty instead of getAttribute if relying on JavaBeans spec, renaming some of the variables), and that as you mentioned earlier, there is almost certainly a library that does this, but in terms of refactoring for readability, I think it was a success ;)

    28 Jun 2006, 08:28

  6. I guess the other point is that if you were to write psuedo–code first, a well known best practice which nobody every does ;) then you would probably end up with pseudo–code that looked very much like the first listing.

    28 Jun 2006, 08:30

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

June 2006

Mo Tu We Th Fr Sa Su
May |  Today  | Jul
         1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30      

Search this blog



Most recent comments

  • Interesting… While I'm not completely convinced in such microbenchmarks, I'm pretty sure that 1ms … by Alexander Snaps on this entry
  • Hello. I bought the book yesterday. I was trying to find the source code for chapter 11 and chapter … by Suleman on this entry
  • http://woosight.net/account/login?username=demo by live mashup demo on this entry
  • Thanks mate ….. This blog was really helpful. by Maaz Hurzuk on this entry
  • Ty. Not directly helpful for my problem, but pointed me in the right direction. You will also get th… by Mike E. on this entry

Blog archive

Not signed in
Sign in

Powered by BlogBuilder