Friday, December 12, 2014

Writing Documents in English

These days, I'm often asked to write documents in English. As I'm not a native English speaker, I tend to struggle with even a simple sentence. In order to be an effective writer, I purchased the following book about English grammar and usage yesterday.

Amazon
"Grammar and Usage, Naturally" by Larry Barkley and Christine Sandoval

After reading a few sections, I'm expecting this book to give me a lot of ideas to improve writing. So, I'll try to write new posts on this blog in English and translate the old posts into English.

If you find any mistakes, feel free to let me know them.

Sunday, September 28, 2014

Temporary Folders in JUnit Test Cases

On writing test cases, we sometimes need a temporary directory to store test resources.

In Java, System.getProperty("java.io.tmpdir") returns the path string to the temporary directory used by the Java Virtual Machine. We can create any temporary resources in the java.io.tmpdir, but we need to check if the name of each resource to be created has been used and clean it up after testing.

File testDir = new File(System.getProperty("java.io.tmpdir", "test20140928012345"));
testDir.mkdirs(); // The given pathname might have been used in java.io.tmpdir.
...
testDir.delete();

JUnit provides a class TemporaryFolder with the @Rule annotation (as of the version 4.7), which helps us manage temporary resources much easier.

...
import org.junit.rules.Rule;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class FileUtilsTest {
    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();

    @Test
    public void testWrtieStringToFile() throws IOException {
        final File dir = tempFolder.newFolder("subfolder");
        final String expected = "deadbeef";
        final File outfile = new File(dir, "a.txt");
        FileUtils.writeStringToFile(outfile, expected);
        assertEquals(expected, FileUtils.readFileToString(outfile));
    }
    ...
}

After invoking each @Test block, the JUnit runner cleans up all the resources under the temporary directory created by an instance of TemporaryFolder.

Saturday, August 23, 2014

Learning Algorithms on Coursera

"Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne" is one of the great books for anyone who wants to learn algorithms and data structures.

Amazon
Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne

We can also browse a website which covers most parts of the textbook, some parts are under construction though. In addition, the website offers some presentation slides and source codes written in Java.

Online Textbook
http://algs4.cs.princeton.edu/home/

It might be difficult to figure out complex things only by reading text. We have a chance to take some online courses on Coursera. I’ve taken and completed the following 2 courses.

The video lectures performed by Prof. Robert Sedgewick are really great. Some effective animations makes it easier to understand how each algorithm works, and some interesting stories about algorithms made me smile.

If you are an intermediate student who is not familiar with basic algorithms and data structures but the Java programming language, I strongly recommend that you take these courses on Coursera.

Make sure that no certificates, statements of accomplishment or any credentials will be awarded in connection with these courses. I was a bit disappointed to know that after I had completed all quizzes and programming assignments.

Saturday, February 15, 2014

Learning Scala on Coursera

I’ve taken the following online course on Coursera.

In this course, Prof. Martin Odersky, the creator of the Scala programming language, teaches us functional programming using Scala.

As I’m not very good at English, I was little nervous whether I would get them right. However, after I’ve finished watching the first video lecture, I found it not so hard. Students can download the slides corresponding to each video lecture, so they helped me keep track of what is going on in the lectures. Each video lecture includes some quizzes, which are effective for students to confirm each learning step. The course consists of 8 lessons divided into 10-20 minute sections.

After every lesson, students submit their solutions to the programming assignment using SBT, and then the grader, which is something like a CI server, sends back a submission score automatically. Students can keep fixing until getting a 100% score by the hard deadline. After the class ends, the students that complete the class will receive a certificate in PDF format.

Some program assignments can be challenging, so you might be stuck with them. In the honor code, no one can write up any solutions, nor can they tell people about the detail of the lessons. If you need any help, the discussion forum will help you solve problems.

Before I joined this course, I had been studying Scala by myself, searching keywords on Google, reading the texts written by early adopters, tinkering a bunch of Scala API, staring at mysterious DSL codes curiously, and so on.

After a while, I was aware of the fact that my Scala code was merely something like a better Java or a type-safe Ruby. In order to know what the FP (Functional Programming) is, I tried to learn Haskell with the book “Learn You a Haskell for Great Good”, and to read the document “All About Monads”. That helped me know benefit of functional programming, but I had not realized how to add essence of functional programming in Scala. Furthermore, I was confused why I should use Scala as a FP language because Scala doesn’t ensure referential transparency and has interoperability with Java libraries in OOP (Object-Oriented Programming).

Through the lessons, I got to know the concept of Scala as a scalable OOP + FP language. The following quote is the most impressive part for me in the book “Programming in Scala: A Comprehensive Step-by-Step Guide”, many parts of which are followed in the lessons.

Functional on the “outside”: … You saw that lists are purely functional on the "outside" but have imperative implementation using list buffers on the "inside". This is a typical strategy in Scala programming: trying to combine purity with efficiency by carefully delimiting the effects of impure operations.

If you have never carefully read this book written by Prof. Martin Odersky, you might misunderstood a lot of things. I’ve heard bad impressions about Scala several times, but in most cases I think they were completely beside the point.

  • Do you believe that imperative programing is always wrong?
  • Do you believe that functional programing is always right?

If your answer to either of the above questions is YES, please take a look at the introduction page of the Haskell official site, which says “When C is better”.

Now it looks so obvious that I no longer hesitate to use Scala. I can write more, but not too much, functional code in Scala and also improve error-prone code in imperative programming languages like Java.

Monday, July 29, 2013

Showing Off My Favorite Quotes on Goodreads

Recently, I signed up for Goodreads, which is a SNS for readers and book recommendations.

Goodreads
http://www.goodreads.com

The site provides several APIs that allow developers to access their resources and offers some useful widgets to make it easier. I tried to add the quote widget below the title of this blog. The widget shows one of my favorite quotes randomly. It looks good, like a blog description.

Wednesday, March 13, 2013

Android 端末の不思議なパスワード記憶を autocomplete="off" で回避

Android 端末で見つけた不思議な症状です。以下のようなログインフォームでの送信時に、入力したパスワードを端末に保存している場合に発生します。

<h1>Sign in</h1>
....
<label>E-Mail</label>
<input type="email" name="email">
<label>Password</label>
<input type="password" name="password">
....

記憶したパスワードが保存元ページで補完されるのは理解できますが、他のページでも、input[type="password"] なフィールドに、パスワードが補完されるようになってしまいました。さらにやっかいなことに、直前の input フィールドに、パスワードとペアのメールアドレスが補完されてしまいました。

<h1>Editing User</h1>
....
<label>E-Mail</label>
<input type="email" name="email">
<label>Nickname</label>
<input type="text" name="nickname" value="(直前のフィールドに記憶されたパスワードとペアのメールアドレスが補完されてしまう!)">
<label>Password</label>
<input type="password" name="password" value="(保存されたパスワードが補完される)">
<label>Password Confirmation</label>
<input type="password" name="password_confirmation">
....

以下のように input[type="password"] のフィールドに autocomplete="off" 属性を付与することでパスワード/メールアドレスとも補完を無効にできました。

<label>Nickname</label>
<input type="text" name="nickname">
<label>Password</label>
<input type="password" name="password" autocomplete="off">
<label>Password Confirmation</label>
<input type="password" name="password_confirmation" autocomplete="off">

明らかにバグのように思えますが、パスワード記憶 + オートコンプリートとはこのようなものなのでしょうか?

Wednesday, February 27, 2013

bundle コマンドで "Could not find rubygems-bundler" エラー

rvm + bundler な環境で、以下のようなエラーが出るようになってしまいました。

Error loading RubyGems plugin ....: Could not find rubygems-bundler (>= 0) amongst [....] (Gem::LoadError)

https://rvm.io/integration/bundler/ を見ると、rubygems-bundler なる gem がデフォルトで有効になったらしく binstubs の shebangruby から ruby_noexec_wrapper に切り替わっていました。

% head -n 1 `which bundle`
#!/usr/bin/env ruby_noexec_wrapper

--path vendor/bundle のようなローカルのディレクトリに gem をインストールしているケースでは rubygems-bundler は入っていないわけで、当たり前ですが Could not find rubygems-bundler となることがわかりました。他の環境で ruby_noexec_wrapper に切り替えてみると、同様のエラーが再現しました。

# rubygems-bundler によって追加された regenerate_binstubs を実行
% gem regenerate_binstubs
...

% head -n 1 `which bundle`
#!/usr/bin/env ruby_noexec_wrapper

% bundle update
Error loading RubyGems plugin ....

rubygems-bundler は bundle exec を使わずに済む gem のようですが、Bundler がまともに動かなくなるのは意味がないため、rubygems-bundler@github にある通り rubygems-bundler-uninstaller というコマンドで無効にしました。元の ruby に書き代わり、エラーも出なくなりました。

% rubygems-bundler-uninstaller
....
% head -n 1 `which bundle`
#!/usr/bin/env ruby

個人的には、確実に Gemfile で指定した gem を使うために、面倒でも bundle exec をつけるなり、必要な binstub だけ設置するほうが良いと思います。