FLiFnotes

August 22, 2011

Purely Incremental Development

Filed under: Coding,Projects — Tags: , , , — Deborah Hawkins @ 8:17 am

Usually in incremental development there’s a plan which is subject to change as new features are added on incrementally and users have a chance to respond. For Flif Risc, I’m extending the idea to purely incremental development–a similar idea but without the overall plan. In purely incremental development, everything is subject to change. This may not be a great idea within a company where business justification is always necessary, but for a personal project it’s offers the flexibility needed to keep interest up.

Test-driven development goes hand-in-hand with any type of incremental development. Writing tests to define what you want to achieve in each iteration and then writing the code to pass them is exactly what’s needed to make regular process. As an added benefit, having the tests from previous iterations makes it easy to find out if a new feature’s implementation breaks existing functionality.

For unit testing in PHP, the natural choice in PHPUnit. At first, I had some difficulty getting PHPUnit set up from the installation instructions, but adding a sudo to the beginning of each instruction solved all my problems.

At the very basic level, I want my application to be able to take in a string which is the hex representation of a MIPS machine code instruction and output the instruction in assembly code. I’d like to accomplish this in a class structure, and knowing that the machine code for add $t1, $t2, $t0 is 0×01484820, the first test verifies that this is the result my code gives.

public function testGetAssemblyCodeAdd() {
    $input = '01484820';
    $expected = 'add $t1, $t2, $t0';
    $temp = new MipsInstruction($input);
    $this->assertEquals($expected, $temp->getAssemblyCode());
}

The easiest way to code this to make it work is to hardcode the output:

class MipsInstruction {
    public function __construct($machineCode) {
        // does nothing
    }

    public function getAssemblyCode() {
        return 'add $t1, $t2, $t0';
    }
}

Running phpunit on the one test now results in OK (1 test, 1 assertion), whoo! A commit is in order for this. Unfortunately, the code doesn’t do anything other than pass this one test, so the next step is to add a couple more tests:

public function testGetAssemblyCodeSub() {
    $input = '01484822';
    $expected = 'sub $t1, $t2, $t0';
    $temp = new MipsInstruction($input);
    $this->assertEquals($expected, $temp->getAssemblyCode());
}

public function testGetAssemblyCodeOr() {
    $input = '01484825';
    $expected = 'or $t1, $t2, $t0';
    $temp = new MipsInstruction($input);
    $this->assertEquals($expected, $temp->getAssemblyCode());
}

resulting in:

FAILURES!
Tests: 3, Assertions: 3, Failures: 2

Once again, I’m making these pass the easy way first and can improve the code later once I know it works.

class MipsInstruction {
    private $inst;

    public function __construct($machineCode) {
        $this->inst = substr($machineCode, -1);
    }

    public function getAssemblyCode() {
        switch ($this->inst) {
            case '0':
                return 'add $t1, $t2, $t0';
            case '2':
                return 'sub $t1, $t2, $t0';
            case '5':
                return 'or $t1, $t2, $t0';
        }
    }
}

Now that all tests pass, it’s time to commit and I’m ready to start refactoring.

April 13, 2011

File Uploads over HTTPS with cURL

Filed under: Coding,Projects,UTCS — Tags: — Deborah Hawkins @ 9:27 pm

In some classes when we turned in a project all we got was a grade, but when I got a low score I could usually figure out what was wrong eventually. Working as a proctor in a computer science class for non-majors, though, the students could use a bit more feedback. The problem was, it was tedious manually uploading 75 comments files for each project since the only method of uploading was a web interface.

When I started as a proctor, I didn’t expect this problem. Upload scripts from previous semesters had been passed down and looked promising… but failed.

A quick look over the script was enough to identify the problem. The web addresses in the script were http:// when what I really needed to use was https://. The obvious solution was to update the addresses in the script, which of course still failed.

My next idea was to use replace the whole curl part of the script and instead use wget. However, wget doesn’t support multipart form data so that was another dead end.

At this point, I figured I was stuck using the browser, but maybe a Greasemonkey script would be able to perform bulk uploads. After all, it’s just adding extra forms to a page and submitting them. But no, modern browsers don’t let you auto-fill the path of file input in forms, which in retrospect makes complete sense.

So I was stuck with curl, which I was already able to use to set cookies or to fetch a page or text file. However, downloading zip files also wasn’t working. Time to look up the curl man page and just try anything:

  • Added -i to also include the headers for extra feedback
  • Added -referer, although no one in their right mind would rely on the referrer given
  • Added -A to specify Firefox as user agent, also dubious
  • Used -H 'Accept-Encoding: gzip,deflate' to attempt zip downloads
  • Changed the keep-alive settings

Nothing helped.

I switched between using the -d and -F flags to specify the content type, but only got Error - bad file data and 403 Forbidden respectively. One of these must at least be a step in the right direction, but which was it?

Finally, I figured out how to use --trace-ascii dump.txt to see exactly what was going on but didn’t know what to look for until downloading an HTTP analyzer for Firefox and using that for comparison. At last things finally started making some sense. As noted in a blog post from Ian Dennis Miller, an Expect: 100-continue header was also in the trace.

I added -H 'Expect:' to the command and it finally happened. I uploaded a comment file remotely. Halfway through the semester and finally some down time to get things working. Damn, I love Spring Break.

March 14, 2011

Choosing an editor for pair programming

Filed under: Coding,UTCS — Tags: , , — Deborah Hawkins @ 6:51 pm

Pair programming works for me because:

  • when we’re working with unfamiliar code, we can split up and then guide each other through the parts we know
  • hearing/seeing other people’s thoughts keeps my mind fresh with new ideas
  • I’m not so tempted to be checking my email or playing Mines
  • at least someone knows how to get C/C++ code to compile

However, pair programming does come with its challenges. For college computer science courses, one of the biggest is finding a time you can work together, but the challenge I’m most interested in is agreeing on an editor to use together. Sometimes it’s simple. For Network Security and Privacy, there was barely any coding involved so my partner and I managed to do the whole thing with gedit. (Well, okay, I did type up one short script in vim). In Software Design last year, we were learning how to use tools with NetBeans, so there wasn’t even a choice there.

Vim is better than EmacsIn Operating Systems, though, my partner and I need an editor that’s more sophisticated. Okay, there are plenty of those, no problem, right? But the fact of the matter is that he’s an Emacs afficionado and I’m vim all the way. I prefer simple keystrokes to Meta-combos, and he just doesn’t grok command mode.

I’ll admit it. Honestly, the Esc key often seems kind of far away, and after years of use I’ve only recently started getting the hang of visual mode. (In retrospect, I have no idea why it took me so long.) But he’ll open up Emacs with our project, I’ll have a burst of inspiration, grab the keyboard, and at the end type in :w. Ugh! Other times I’ll open up vim and then he has a burst of inspiration but causes crazy stuff to happen because I wasn’t in insert mode.

By now I think he does know how to get into insert mode and I’ve at least learned how to save in Emacs, but it’s obvious that one editor isn’t going to work for the two of us. We needed a new plan…

So screw the former pair programming paradigm! We now sit happily side-by-side with two different monitors, two different machines, two different keyboards. He keeps Emacs open, and I’ve got my vim. Whoever’s driving at the time gets to use their preferred editor and then check in the code once there are any changes or we want to switch off. And whenever we’re absolutely stuck on something, we both get to fiddle around with the code individually until one of us finds something that looks like progress.

It’s kind of weird, though. The last time we were working on a project together, I was using DVORAK and having to switch back to QWERTY regularly to pair program. If we managed to agree on an editor, I’m sure we’d find something else crazy to disagree on. Then again, anything’s better than fuv.

April 20, 2010

How to refactor Wikipedia articles

Filed under: Coding — Tags: , — Deborah Hawkins @ 11:36 pm

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.

January 23, 2010

Using Reflection in Java

Filed under: Coding — Tags: , — Deborah Hawkins @ 1:15 pm

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

May 1, 2009

Blogging in DVORAK

Filed under: Coding — Tags: , , — Deborah Hawkins @ 1:49 pm

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.

April 20, 2009

Oracle to Buy Sun

Filed under: Coding — Tags: , , — Deborah Hawkins @ 8:41 pm

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

Full press release on Oracle.com.

April 13, 2009

goto in PHP

Filed under: Coding — Tags: , , — Deborah Hawkins @ 9:05 pm

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.

Powered by WordPress