January 23, 2012

오리지널 레고 마인드스톰 부활 프로젝트

아... 이 사이트 너무 놀리는거 같아서 스킨 새 단장하고 글 하나. 참 스킨은 www.plaintxt.org 비슷하게 만들어 봤습니다.

약 12년전에 산 레고 마인드스톰이 있어서 애들한테 어떻게 굴러가게 만드는지 보여주려고 하니 여러가지가 걸리더군요. 일단 박스에 딸려온 소프트웨어는 Windows7에선 안 돌아가고 집에 있는 랩탑엔 씨리얼포트가 없고... 씨리얼포트... 정말 오랫만에 들어보는군요.

예전에 한번 써봤던 NQC로 코딩은 하면 될것 같은데 씨리얼포트는 달리 방법이 없어서 일단 아마존에서 어댑터를 하나 구입했습니다. 어댑터 도착 후 테스트해보니 씨리얼포트는 정상적으로 인식하더군요. 감동... ㅜㅜ  Brix Command Center (BCC)라는 GUI도 있어서 설치해서 사용해보니 한 2%정도 편해지더군요. 이렇게되니 얼마전 쌍둥이들이 잃어버린 터치센서 하나가 무척 아쉬워지네요.

애들은 레고 시티 시리즈를 주로 가지고 노는데 나중에 자동문이나 회전목마같은거나 만들어줘야겠네요.

January 6, 2011

Blogger.com으로 이전했습니다.

근 1년반만에 쓰는 포스팅인데 블로그 이전 메일이군요. ㅎㅎ
워드프레스 호스팅을 사용하고 있었는데 바꿨습니다. 사용한 방법은

  1. 워드프레스 관리 페이지에서 export 메뉴로 xml 파일 다운로드
  2. 이미지 파일의 링크들을 찾아 피카사 앨범에 업로드 (이미지 파일은 10개 이하라 일일이 손으로 피카사 앨범으로 옮겨주었습니다.)
  3. 1번에서 백업했던 xml 파일에서 이미지 파일 링크들을 피카사 링크로 변경
  4. http://wordpress2blogger.appspot.com/ 를 사용하여 블로거 export 파일로 변경
  5. 블로거 관리 페이지에서 import
끝.

이미지 외 일반 파일들은 모두 무시하였습니다. ㅎㅎ

October 12, 2009

arraysizeof macro

다음 macro는 array의 length를 구하는 것인데 해석이 되시나요?
template <typename T, size_t N>
char (&Helper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(Helper(array)))

...

안되신 분들을 위한 설명입니다. ;)

위 코드는 다음 두가지 사실만 알고 있다면 (비교적) 쉽게 이해됩니다.

sizeof는 function call syntax에 대해서도 동작하며 그 function의 return type에 대해 동작한다.
int foo() { return int(); }
...
sizeof(foo()) == sizeof(int)

array type의 reference를 리턴하는 method의 signature는 다음과 같다.
char (&bar)[10]() { ... }

이를 바탕으로 arraysizeof를 만들려면 char(&)[N]을 리턴하는 method를 만들면 되죠. 여기서 N은 array의 length.
template <typename T, size_t N>
char (&Helper(T (&array)[N]))[N] {
...
}

위 함수는 T(&)[N]을 parameter로 받아 char(&)[N]을 리턴합니다. 물론 T(&)[N] parameter는 이 template method의 implicit instantiation이 동작하도록 하기 위해 필요합니다. 마지막으로 sizeof는 method body가 필요없으므로...
template <typename T, size_t N>
char (&Helper(T (&array)[N]))[N];

끝.

참고로 이런 코드가 필요한 이유는 널리 쓰이는 "sizeof(array) / sizeof(*array)" macro가 다음과 같은 경우 compile error없이 오동작하기 때문입니다.
void foo(int array[]) {
int size = sizeof(array);
}

이 코드에서 array는 pointer와 같이 취급되어 sizeof(array)는 sizeof(int*)가 됩니다.

September 25, 2009

Java generic in return context

아래 코드를 보시고 1, 2번 라인중에 어디서 에러가 날 지를 찾아보세요.
interface B {
void doB();
}
class D implements B {
public void doB() {}
}
interface H {
B getB();
}
class HImpl implements H {
public B getB() {
return new D();  // 1)
}
}
...
H h = new HImpl();
D d = h.getB(); // 2)

네, 2번 라인입니다.

그럼 다음 코드는?

interface B1 {
void doB1();
}
interface B2 {
void doB2();
}
class D implements B1, B2 {
public void doB1() {}
public void doB2() {}
}
interface H {
<T extends B1 & B2> T getB();
}
class HImpl implements H {
public <T extends B1 & B2> T getB() {
return new D();  // 1)
}
}
...
H h = new HImpl();
D d = h.getB(); // 2)

네, 1번입니다. 잘 이해가 되질 않아 사내 메일링 리스트에 물어보니
I think you're seeing and interpreting it as "this method can return anything that implements both interfaces". What it's actually saying is "the caller is going to tell you a specific class that implements both interfaces, and you must return one of those".

이랍니다. 즉 T 타입이 아직 결정되지 않은 상태라 D와 T는 compatible한 타입이 아닌거죠.

강제로 casting을 해서 컴파일이 되게 만들면 그 코드는 runtime error (ClassCastException)를 발생하게 됩니다. 다음 코드를 보세요. 즉 이런 방법으론 도저히 type-safe한 코드를 작성할 수 없습니다.

class D2 implements B1, B2 {
public void doB1() {}
public void doB2() {}
}
...
D2 d = h.getB(); // ClassCastException!!

이 같은 상황을 방지하기 위해 1번 라인에서 에러를 내 주는 것이죠.

코딩 가이드라인을 만들어 보자면 "method의 parameter로 사용되지 않는 type variable을 사용하면 안된다"입니다. 다음과 같은 경우는 parameter로도 사용되고 있으므로 문제가  없는 경우들입니다.

<T> T makeInstance(Class<T> cls) { return cls.newInstance(); }
...
<T extends Comparable<? super T>> T min(T a, T b) {
return a.compareTo(b) < 1 ? a : b;
}
위의 예제 코드같은 경우 아래와 같은 방법으로 type-safe하게 작성될 수 있습니다.
interface B1 {
void doB1();
}
interface B2 {
void doB2();
}
class D implements B1, B2 {
public void doB1() {}
public void doB2() {}
}
interface H  <T extends B1, B2> {
T getB();
}
class HImpl implements H<D> {
public D getB() {
return new D();  // 1)
}
}
...
H<D> h = new HImpl();
D d = h.getB(); // 2)

사실... 아직도 잘 모르겠어요. Java generic. =(

September 22, 2009

Access level of methods in package-private class

Java에서 (C++도 마찬가지지만) 권장되는 access level 사용법은 다음과 같습니다.
Use the most restrictive access level that makes sense for a particular member.

쉽게 얘기해서 private으로 충분하면 private을 쓰고 다음으로 package-private, protected, public 순으로 쓰라는 얘기.

그런데 C++와는 달리 Java는 class에도 public과 package-private 두 종류의 access level이 있습니다. 이 중 public class의 method들은 위의 권장 사항을 따르면 될 듯 한데 package-private class일 경우는 어떨까요?

Package-private class에서는 실제 사용될 수 있는 조건으로 봤을 때 method들이 public이건 protected건 모두 package-private과 같은 조건을 가집니다. 즉, 아무리 method의 access level을 public이나 protected로 해봐야 다른 package에선 이 method를 사용할 수가 없습니다. 따라서 the most restrictive한 level을 사용하라는 위 권장 사항을 따르자면 package-private class의 method들은private이 아니라면 모두 package-private access level을 사용해야 합니다.

그런데 코딩을 하다보니 이 방법엔 한가지 문제가 있더군요. 가끔 package-private class를 public class로 만들어야 할 경우들이 생기는데 이때 method들이 모두 package-private이면 다른 package에서 사용할 수가 없습니다. 그래서 다시 method들의 access level을 조정해야 할 필요가 생기는데 이 시점은 이미 코드를 작성했을 때와 멀어서 자칫 잘못된 access level을 지정할 수 있게 됩니다.

그래서 생각한건데... package-private class의 method들을 만들 때 access level은 이 class의 access level을 public으로 가정하고 작성하는 것이 어떨까요?

September 16, 2009

A History of CLU

Checked exception 관련 글들을 찾아 보다가 읽게 된 문서입니다. CLU라는 프로그래밍 언어를 개발하게 된 배경과 과정에 대한 내용인데 정말 재밌네요. 참고로 문서가 좀 길어보이지만 한 1/3은 부록입니다. :)

A History of CLU

거의 30~40년전 이야기인데 현재 사용하고 있는 언어들이 여기서 앞으로 많이 나간것 같아 보이지 않네요.
As mentioned, the concept of data abstraction arose out of work on structured programming and modularity that was aimed at a new way of organizing programs. The resulting programming methodology is object-oriented.
A keystone of the methodology is its focus on independence of modules.
Achieving independence requires two things: encapsulation and specification.
Specifications are needed to describe what the module is supposed to do in an implementation-independent way so that many different implementations are allowed. (Code is not a satisfactory description since it doesn’t distinguish what is required from ways of achieving it. One of the striking aspects of much of the work on object-oriented programming has been its lack of understanding of the importance of specifications; instead the code is taken as the definition of behavior.)
Specifications also allow code that uses the abstraction to be written before code that implements the abstraction, and therefore are necessary if you want to do top-down implementation.
In essence, having a language enforce encapsulation means that the compiler proves a global property of a program; given this proof, the rest of the reasoning can be localized.
CLU favors program readability and understandability over ease of writing, since we believed that these were more important for our intended users.
We worked on the implementation in parallel with the design. We did not allow the implementation to define the language, however.

이 글을 읽고나니 Kent Beck이 세미나에서 언급했던 Structured Design이라는 책이 읽고 싶어지네요. Kent Beck도 이 책을 언급하며 수십년전에 쓰여진 책인데 필요한 내용이 전부 있다... 뭐 이렇게 소개했었던 것 같은데...

September 15, 2009

Checked exception, 이거 써도 되나?

mkseo님 블로그Best Practices for Exception Handling에 관한 글이 올라왔는데 댓글을 읽고 쓰고 하다 궁금한게 생겼습니다.
Checked exception, 이거 써도 되나?
mkseo님이 링크를 걸어준 The Trouble with Checked ExceptionsDoes Java need Checked Exceptions?, Java's checked exceptions were a mistake (and here's what I would like to do about it)등의 글을 읽다 보니 현재까지의 결론은 "java의 checked exception실험은 실패다"정도인 것 같더군요.

몇가지 이유로는
  1. 원래 language designer의 의도와는 달리 많은 exception이 무시되고 있다. 원래 의도는 compile-time에 에러를 냄으로써 개발자들에게 이 예외를 지금 처리하거나 아니면 위로 전달해야 한다는 메시지를 보내는 것이었다. 하지만 예외를 전달하기 위해서는 method의 signature를 수정해야 한다. 개발자들로서는 이 상황을 벗어날 수 있는 가장 쉬운 방법을 찾게 되고, 이게 바로 catch 후 무시하는 것인데 대부분의 코드가 이런 식으로 작성되고 있다. catch (...) {}
  2. 원래 exception의 목적중 하나는 예외가 발생한 지점과 처리할 지점의 분리이다. 즉 low-level에서 발생했지만 거기서 처리가 불가능한 exception들을 위로 전달하여 처리하게 한다... 인데... checked exception은 그 사이에 있는 모든 코드가 이 exception에 대해 알아야만 하게 강제한다. 즉, 전달되는 과정이 투명하지 않다.
  3. Versionability - 어느 method가 A, B라는 checked exception만을 던진다고 선언되었다면 client code들은 이 예외만을 처리하거나 위로 전달하도록 작성된다. 나중에 이 method의 다음 버전에서 C라는 exception을 던질 필요가 생기면 client와의 contract이 깨지게 된다.
  4. Scalability - A, B, C, D라는 method들이 각각 4개씩의 checked exception을 던지고 U라는 method가 이 것들을 호출하는데 예외들을 처리하지 않는다면 U는 총 16개의 checked exception을 던진다고 선언해야 한다. 상위로 올라갈 수록 선언해야 할 exception의 개수는 기하급수적으로 늘어나게 된다.
등이 있습니다. 4번의 경우에는 각 레벨에 맞는 exception을 정의해서 사용하면 해결할 수는 있을 것 같습니다. 즉, A, B, C, D가 던지는 16개의 exception을 U가 던질 때는 자기의 context에 맞는 예외 두어개로 mapping하여 던진다는 식이지요.

암튼.. 그럼 왜 여지껏 저는 checked exception이 매우 그럴 듯해 보였던 걸까요? C#의 한 language designer가 이렇게 얘기했다네요.
Examination of small programs leads to the conclusion that requiring exception specifications could both enhance developer productivity and enhance code quality, but experience with large software projects suggests a different result -- decreased productivity and little or no increase in code quality.
하지만 몇가지 경우엔 checked exception이 유용할 수 있다고 합니다.
  • 예외가 significant boundary를 넘어가는 경우. 예를 들어 CORBA의 machine boundary같은... 이런 경우 좀 더 명시적인 예외 스펙이 도움이 된다.
  • package 내부에서 사용되나 절대 외부로 넘어가서는 안되는 예외의 경우 checked exception을 사용하면 컴파일러가 이를 감시하게 할 수 있다.

예외에 관해 참고할 만한 글들입니다.