2012/01/31

[Book review] Advanced C++ Programming Styles and Idioms

Advanced C++ Programming Styles and Idioms    C++ プログラミングの筋と定石



今回は私がC++の世界にはまった最初の書籍について紹介します。

C++で最も有名な書籍といえば Scott Meyers氏のEffective C++ だと思いますが、
初めてEffective C++を読んだ時には、文章ばかりであまりピンと来ませんでした。
そもそも自分の C++及びオブジェクト指向に対する不勉強のためだったと思います。

そういう状況で見つけたのが本書籍でした。US Amazonのレビューで見つけて、邦訳本が
あることを知らなかったので、洋書で購入して読みました。遠回りしてしまった感もある
にはあるのですが、実際に邦訳本を立ち読みしてみましたが、分かりにくいというか
頭にしっくり入ってこない部分もあったので、時間は掛かったけど洋書で読んで正解だった
かなという風にも思っています。

驚くことにこの本の出版日はなんと 1991年だそうです。もう20年以上も前のこと。著者
の James O. Coplienは 「C++の設計と進化」によると C++の最初のユーザー(正確には
Cfront)だったということですが、その時点でここまで深堀りできているという点に驚き
です。

当然古い出版のため、新しい言語仕様に対する記載(例えば templateや namespaceなど)に
関する説明はなかったり、一部現行仕様と相容れない記述もあるかと思いますが、非常に
参考になるテクニックが記載されています。

しかしながら、この本の多くの技巧のアプローチは、C++の強力なタイプシステムを
いくらか柔軟にして利用するために、Polymorphismを多用したものとなっています。
現在のオブジェクト指向プログラミングでは、なんでもかんでも共通のベースクラスから
派生させるようなスタイルはあまり採用しないと思います。紹介されているテクニック
そのものはプロダクトコードに用いずに、そのテクニックのエッセンスを自身の
プログラミングに利用するというのが良いかと思います。

この書籍では「イディオム」という形で、テクニックやコンセプトに名前を付け、その
コンパクトに名付けられた「イディオム」で、プログラマーが共通認識を持って
ソフトウェアの生産性を上げることを狙っていたものと思われます。この流れが、その後
の「デザインパターン」といったムーブメントへ繋がっていったんでしょうね。

Scott Meyersの More Effective C++では参考文献として、本書は LSD本として紹介
されています。洋書のカバーの色合いと、紹介されている技巧から LSDとは・・・なるほど
言い得て妙だなと感じました。




This time let me introduce a book on C++ programming language which took me into
the deep world of C++.

I wonder the world-most-famous book on C++ should be "Effective C++" by Scott
Meyers. However, when I first read the book, I had a feeling that I didn't fully
understand what the book tries to address very well. I must admit that it was due to
my lack of well-understanding of the language as well as sort of lengthy text in that
book.

In such situation, I found this book. Since I didn't know that Japanese translation was
also available at that moment, I bought English version by checking US Amazon
review. When I realized that Japanese version was available, it is true that I felt like I
went a long way around. However, when I browsed the Japanese version in a
bookstore, I found some portion seems not so intuitive. Now I wonder it deserves
reading English version even if it takes me some more time to complete it.

I was surprised that the publication date was in 1991 - more than 20 years before.
The author James O. Coplien was said to be the first user of C++ (to be precise,
Cfront) according to the book 'Design and Evolution of C++'. Anyway, I'm quite
surprised that he has deeply investigated into that much even in this early stage of
C++.

As this is quite old book, there are some of missing portions in terms of rather
new language specification such as template, namespace etc.. and also you may
find something which is not aligned with the current C++ specification. But it is also
true that it contains a lot of techniques which are very helpful.

However, many of the techniques introduced in this book are sort of one-sided.
I mean they often adopt polymorphism, attempting to lessen the benefit of C++
strong type system to get some more flexibility. I don't think that present way of
doing object-oriented programming is like creating inheritance hierarchy among
almost all the objects from a single commonal base class. It would be better not to
make use of the code technique shown in this book into production code as it is.
Instead, we can make better use of those essences into our daily programming.

In this book, it names various techniques and concepts in the form of 'idioms'.
I believe that those idioms are aimed to be widely shared among programmers
in order to get common understanding via such compact names, which helps to
improve our software productivity and quality etc.. and I also believe this idea must
have had something to do with the later 'design pattern' movement.

In famous book 'More Effective C++' by Scott Meyers, this book was introduced as
'LSD' book. Now I can relate to, from the coloring adapted by the book cover and
its various techniques introduced in this book.


2012/01/30

Google-code-prettify

今後いろいろとソースコードを貼付けたりして記事を書いてみたいので、ソースコード表示
用のCSSである、google-code-prettify を導入してみる。

参考というか、そのままこちら のサイトを利用させて貰いました。

で、試しにサンプルコードを書いてみる。


#include <memory>

class TestClassImpl;

class TestClass
{
pubilc:
    void someMethod();

private:
    std::auto_ptr<TestClassImpl> pimpl_;
};

きちんと Code prettifyされていそうですね。




Since I would like to post various articles on programming matters, I'm going to
introduce some CSS which improves some readability for source code by making
use of google-code-prettify .

I referred to the this site (in Japanese) while introducing the CSS. That's really
helpful indeed.

The following is my first trial to see if the source code can be in some organized
way.

#include <memory>

class TestClassImpl;

class TestClass
{
pubilc:
    void someMethod();

private:
    std::auto_ptr<TestClassImpl> pimpl_;
};

Now I can see it is quite code-prettified, isn't it?

2012/01/19

Go Kei!!

錦織選手がやってくれました。フルセットの末、3回戦進出。現在全豪オープン真っ最中。
WOWOWなどには加入していないので、試合をみる手段はないですが、日本人選手として
頑張って欲しいですね。



Japanese professional tennis player Kei Nishikori advanced to 3rd round in men's singles in Australian Open Championship tournament. Although I have no chance
or way to watch his game live, I've backed up him indeed. Go Kei!! You can make it!


2012/01/09

Good paper and good pen are better than any other GTD stuffs??



時々自分の仕事の仕方などを見直そうと思って、LifeHack的なアイデアを探したりします。
そして、いくつかを試して軌道に乗ったりもするんですが、結果としてはいつも「長続き」
しないということになってしまいます。結局LifeHackになっていない・・・

結局アナログなやり方が性にあっているのか?現在はこれらを愛用しています。

一つ目は、PilotのFrixion Ball 0.5mmというペンです。知っている方も多いかと思います
が、ペンでありながら熱でインクを消すことができる優れものです。書き味も自分好み。

一度車内にこのペンを置きっぱなしにして、中のインクが完全に消えたことがありましたが、
原理を正しく理解して注意して使うとかなり便利です。

もう一つは、ペンを使って書く側のノートです。前はMoleskinを使っていましたが(これは
これで良かったのですが)、今は RHODIAの Meeting Bookというを使っています。表紙が
比較的固くてずーっと持ち歩いていても中の紙がヘタれないのと、ページの構成が非常に
使い安く気に入っています。

普段はこれを持ち歩いて、電車通勤時に立ったままで、ちょっと思いついたことや ToDo
事項をこれにメモして使っています。個人主観かも知れませんが、このFrixionとRHODIAの
相性が抜群で書きやすい。

いつかは Evernoteとか使いこなそうと思っていますが、現在はアナログ手法で進めます。





I often come to think that I should find some more better way to do getting things
done by making use of some useful LifeHack tools etc., and then try to look for
such good stuffs. Some of them would attract me to use. However, as is often
the case, the result is that after a while, I would gradually become not to use them...
It's not like LifeHacking at all in the end...

The current conclusion seems to be that some more 'analog' equipment suites me.

The first item is a pen, Pentel Frixion Ball. In the top(?) of the pen, it equips with
rubber ball. You can erase the ink by using that rubber (with heat) in spite that it is
a pen! It's quite useful. And also its writing is very well for me.

However, when you use this pen, you are supposed to be careful. I once kept this
pen in my car for a while, which totally disappered all the ink in the pen... The ink
inclusive in the pen is quite weak at heat... So you would need to understand the
rationale when using it.

The other one is paper - a notebook. I used 'Moleskin' before. But now I realized that
RHODIA classical Meeting Book is quite confortable for me to keep on writing. Its
cover pages are appropriately hard and can protect inner pages well. And page
layout adopted by this notebook is really good.

I usually take these stuffs while commuting (by train) and then whenever anything
comes up to my mind, I'm supposed to take notes in the book by using the pen.
Although this is my personal thought, these two items go together very well.

I'm also thinking to use some cool tool like Evernote in the near future. But I'm rather
content with such analog stuffs so far.



2012/01/07

2 books about recent brain research

単純な脳、複雑な「私」- 池谷 裕二

ゆらぐ脳 - 池谷 裕二

今回は池谷 裕二さんという脳科学者が著した2つの本を挙げます。

1冊目は筆者の母校(高校)の生徒達に、現在の脳科学の最先端トピックや人が自然と不思議
に思うであろう様々な話題で行われる講義の実況中継のような形で進められます。

よく陥る様々な「脳のクセ」や「意識と無意識」についての話題などどのページを捲っても
知的好奇心をかき立てるような話題の連続です。

個人的に興味深かったのが、「アルファ波をモニタすることさえできれば、アルファ波を
抑制させることが出来る。必要なものはフィードバック。」といったところ。自分の生活
や業務に当てはめてみても、メタなレベルでこの例が示唆することは重要ではないかと
思います。

自分はソフトウェア開発をやっていますが、アジャイル・リーン原則といったものは「迅速
なフィードバックを得、改善を絶えず行っていく」姿勢や実践にあります。これも極論
すれば人間の脳に必要なフィードバックを得て、人間が物事をコントロールしていくことに
つながるんですかね・・・。そのフィードバックを実現するためにも計測は必要ということ
でしょうか。

2冊目は2者の対談という形式で綴られた、科学者のあるべき姿を追求するような内容に
なっています。といっても脳科学の最新の話題なども取り扱われて、困難な話題がすんなり
と読者に入っていくような思慮が為されていると思います。

あらゆる物事を「考える」とき、脳を使ってそれは実践される。脳科学では、その「考える」
対象が脳であり、その中での発見が、自身の「考える」姿勢、思想・哲学感にも影響を
与える・・・なんだか再帰的で、複雑そうに思えてくるものもあります。

また仮説 - 検証型の研究に対する疑問というか批判についての下りは昔に読んだ福岡伸一氏
の本の中にも似たような記述があったような気がします(野口 英世の研究についてのくだり
だったかと思います)。

私は研究者ではないですが、業務・生活の中で下すべき決断について良く仮説-検証型の手順
を踏んで物事を進めることが多いので、また別の視点を入れてみるようなことを考えて
みようかという気にさせてくれました。




This time let me introduce 2 books written by Japanese brain researcher Yuji Ikegaya
(Gaya), titles of which are something like the following

  • The brain - the simple, Myself - the complicated
  • Continuously fluctuating brain


The first book features his teaching at his old high school, where he was once
welcomed as a guest. And its way of proceeding with stories is like as if it were live
coverage.

It features various interesting topics related to brain such as what kind of 
interesting and/or funny habit the brain is having, how consciousness and
unconsciousness are working mutually etc.. All of them inspired my intellectual
curiosity quite a lot.

What is quite interesting to me is what he mentions in this book as follows.
"If we can continuously monitor alpha wave, then we can take all the control of
our alpha wave. All the things we would need here is just a feedback."

I now believe, if we can apply this rationale to a higher-order level, what he meant
here should be applicable in various situations no matter what it be our job, our daily
life etc..

I'm now engaged in software development and then strive to apply Agile and Lean
principles there. These principles should be obviously based on the attitude that we
pursue "prompt feedback and continuous improvement". Now I wonder this attitude
should well fit into human mental model, just like the brain gets such feedbacks and
then make use of those feedbacks to take control of things…. In order to get such
feedback, it seems that continuous measurement should be the key to any success,
isn't it?

The second book is in the form of dialog between the author and an interviewer and
features what should be required for scientists. However, of course, it also features
various hot topics about recent brain researches and definitely should attract 
readers in this way.

When human tries to think something, its action/bahavior will be performed by
making use of the brain. In brain research, its object to 'think' should be, of course, 
the brain. Whatever discovered in the research, it will have a quite good deal of
impact on his way of thinking afterwards, his thought and even his philosophy...
This cycle is a sort of recursive, makes me complicated in a way… but quite 
interesting.

And also this book raises some question or even criticism towards most
conventional approach to solve any problem - making hypothesis and verification if
the hypothesis is true. I wonder his point is that if we always take the approach, 
something quite important may be overlooked. Although I'm not a researcher, I must
admit that I often tend to adopt such approach (hypothesis and verification), I now 
have a feeling that I would need have some other view on this matter.


Book about C++ template metaprogramming

   Advanced C++ Metaprogramming - by Davide Di Gennaro

C++のテンプレート、その中でもTMP(Template Metaprogramming)に関する書籍って
本当に少ないですね。日本語の書籍となるとまた限られるので、思い切って洋書を購入。
昨日到着しました。

US AmazonのBook reivewで、本の体裁が悪い(スタイルなどが醜いのと、Errataも多数)というのが気になりました、テーマとして貴重だと思ったので購入したものです。

まだ読み始めた最中ですが、読み終わって何かえるものがあったらこのブログに掲載しよう
と思います。

----------

   Advanced C++ Metaprogramming - by Davide Di Gennaro

As you may know, there are quite few books available which talk about C++ template metaprogramming. The situation should be much worse in case of such books available in Japanese... So I've decided and purchased this English book. (It should be a sort of tall order for me to complete this book...)

According to US Amazon book review, there are a few bad comments on its styles and numerous errata which once hesitated me to buy it. However, I've bought the one since the theme this book is handling is quite valuable, I thought.

I haven't yet completed to read this book since it's just arrived. I'll later on post what I  could find very valuable in this book in some day...


Googlemock - how to handle the case multiple overloaded member methods are present

Googlemock使ってますか?私は使い始めて間もないですが、非常に便利です。単体テスト
を実施したい場合にネックとなるのが、他のクラスなどへの依存性ですが、これを
Googlemockを使用してモックオブジェクトを差し込んで、テストの実施およびテストの
網羅度を向上させることができます。

下記のように同じ名前のメンバ関数を持つようなクラスのモック化ではまってしまったので
メモしておきます。

class SomeClass
{
public:
    ...
    virtual void someMethod(int i);
    virtual void someMethod(std::string& str);
    ...
};



このようなクラスをMock化して使用する場合には、EXPECT_CALL()マクロ使用において、
Matcherの記述において引数の型を明示する必要があるため ::testing::Matcher<> というの
を記載する必要があります。

class MockSomeClass : public SomeClass
{
public:
    ...
    MOCK_METHOD1(someMethod, void(int));
    MOCK_METHOD1(someMethod, void(std::string&));
};
....

// Test code
MockSomeClass mock;
...
// In case that we would expect latter method is invoked here.
EXPECT_CALL(mock, someMethod(::testing::Matcher<std::string&>(::testing::_)))
...
// instead of
// EXPECT_CALL(mock, someMethod, ::testing::_);
// ...

Are you using Googlemock? Although I've just started playing with it, I found it is
quite useful. Whenever trying to perform unit testing, what can be some obstacle to
achieve unit testing would be dependency on other classes etc.. However, we will be
able to eliminate such dependency to outside by introducing mock object instead of
the actual class object. This is the way that we can achieve unit testing and even
improve test coverage toward developed software. The other day I had some hard
time to handle the following case for a while in handling some polymorphic class
where the same name of member methods are defined.

class SomeClass
{
public:
    ...
    virtual void someMethod(int i);
    virtual void someMethod(std::string& str);
    ...
};

In case of trying to mock such class, we would need to have a bit special treatment
when using EXPECT_CALL() macro. When you write down matcher portion, you are
supposed to use ::testing::Matcher<> to specify the argument type which is being
expected here. Otherwise, EXPECT_CALL() can be treated as ambiguous...

class MockSomeClass : public SomeClass
{
public:
    ...
    MOCK_METHOD1(someMethod, void(int));
    MOCK_METHOD1(someMethod, void(std::string&));
};
....

// Test code
MockSomeClass mock;
...
// In case that we would expect latter method is invoked here.
EXPECT_CALL(mock, someMethod(::testing::Matcher<std::string&>(::testing::_)))
...
// instead of
// EXPECT_CALL(mock, someMethod, ::testing::_);
// ...