Category: Coding

How to refactor Wikipedia articles

One of the first topics we discussed in Software Design was refactoring–making small, incremental changes to code that wouldn’t affect the behavior of the program but would make the code more readable and easier to modify.

I considered it at first to be something like the fictional writer Joseph Grand in Camus’s The Plague. Grand had started a novel some time back but hadn’t gotten past the first sentence because he wanted to find the perfect words and was never satisfied. And it somewhat reminds me of myself trying to get back in the habit of writing these blog entries. Knowing when not to refactor really is the hardest part. Sometimes you have to just complete something even knowing that you could write it so much better.

Looking for examples of refactoring in prose writing rather than code, I stumbled across the history of Wikipedia’s article on code refactoring which includes such gems as the Adding Quotation (to Make the Topic Less Dry) refactoring or the Change Word (“Metric” to “Measurement”) refactoring.

Keep in mind, though, that not everything counts as a refactoring. There are some transformations such as Remove Excess Comma which are still just good ole’ bug squashing.

Using Reflection in Java

Last semester, my two main programming courses were CS 315 and CS 310 where we learned to use Lisp and LC-3 assembly code respectively. In both, programs and data were of the same format, so modifying a program was as simple as modifying data. In Java, however, it’s a bit more awkward. Sure, you can treat a .java file as text and modify it, but what if you only have the .class file? Or what if you just want an easier way? Fortunately, Java provides a solution called reflection.

In our Software Design course, our first assignment of the semester is to use reflection to create Java programs called adaptors. I’ve just started with the assignment by playing around with the Java reflection library and figuring out how to use these tools, but I thought I’d share a simple example which may be much quicker to understand than the article linked to above.

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) { 

        // select the class to look into
        Class myclass = String.class;

        // check how many methods the class declares
        Method[] methods = myclass.getDeclaredMethods();
        System.out.println( "Number of methods in "
                + myclass.getName() + " class: " + methods.length + "\n" );

        // select a method from the class
        int methodNum = 52;
        Method mymethod = methods[methodNum];
        System.out.println( "Checking method #" + methodNum + ":" );

        // name of method
        System.out.println( "Name: " + mymethod.getName() );

        // modifiers such as protected or static
        // note: getModifiers() returns an int that you need the Modifier
        //       class to makes sense of
        System.out.println( "Modifiers: "
                + Modifier.toString(mymethod.getModifiers()) );

        // return type of method
        System.out.println( "Return Type: " + mymethod.getReturnType().getName() );

        // list of method's parameters
        Class[] mytypes = mymethod.getParameterTypes();
        for (Class c: mytypes)
            System.out.println( "Parameter: " + c.getName() );
    }
}

The output for this program is:

Number of methods in java.lang.String class: 68

Checking method #52:
Name: substring
Modifiers: public
Return Type: java.lang.String
Parameter: int
Parameter: int

Blogging in DVORAK

Why would anyone who can type 71 wpm in QWERTY want to switch to a different keyboard layout? There are many cases like the guys at DVzine.org who have improved their typing speed by switching, but their speeds in QWERTY were significantly lower than mine. And after a week an a half of using mostly DVORAK when typing, my speed is only around 19 wpm. But then again, switching back to QWERTY occasionally probably doesn’t help.

The hardest part is what to do about all the keyboard shortcuts that were designed for right-handed people with QWERTY. Quite frequently, I’m using Firefox with my right hand on the touchpad. I want to open a link in a new tab, but my left hand still on the keyboard is nowhere near the “t”. And the thought of relearning the muscle memory for all my VIM commands is just painful. That’s why I’ve been switching back and forth a bit too much for the good of my learning.

Oh well, school’s almost out for the summer, at which point I’ll have the extra time to take things slow and see if I really can improve my typing speed with less stress on my hands.

Oracle to Buy Sun

… and I have no idea what this means for MySQL, so I guess this is a short one.

Full press release on Oracle.com.

goto in PHP

xkcd

I had a class in middle school where we did a little bit of programming. I don’t remember much about it, and the programs we wrote probably weren’t more than 25 lines. The one thing I do remember is that we used gotos. No object-oriented programming, not even functional programming.

The reason I mention this now is that I was talking one of my colleagues today, arguing PHP’s case, as he supported Python, and I mentioned that as of PHP 5.3, PHP now has a goto operator. He was really hoping that was a joke, and now probably takes the language significantly less seriously.

There are plenty of criticisms of goto, not the least important of which is Edsger Dijkstra’s Go To Statement Considered Harmful. The crux of most arguments, though, seems to be that goto statements are not inherently semantic and as such aren’t appropriate for modern structured languages.

I’m not using PHP 5.3 yet, so I can’t say that I’ve used goto already, but I don’t see why having a goto operator can make a language intrinsicly bad. There’s some code that looks gacky however you try to write it, and in some cases a simple goto may actually make it easier to read and understand, even if it also has its share of bad uses.

Looking over some comments on goto use, one of the concerns I find most interesting is that of the kitchen sink mentality. Personally, I enjoy PHP because I can have one language and use it for procedural, functional, or object-oriented code–whatever I feel is most appropriate at the time. But, yes, this can also lead to poor coding standards and huge misunderstandings between, say two developers who are both writing code in PHP but with two completely different programming paradigms.

How well a language like this works for you depends on how you use it, and I think I can use it effectively, but if others prefer to work with just one subset at a time–that’s totally fine too. Anyhow, I plan to take a look this summer at this awe-inspiring Python that everyone around me keeps evangelizing (except for Jared, who loves Rails), so who knows? Maybe I’ll be enlightened then.

Ringbinder theme by Themocracy