Software internationalization is difficult under the best of circumstances, but it always amazed me how often one particular country came up in discussions of internationalization problems: Turkey.
I've been tracking a really funky bug in my West Wind Web Store application that seems to crop up only very infrequently in my error logs. In a previous post I mentioned that I had instituted some additional logging features – specifically making sure that I would also log the locale of the user accessing the application.
Well, three bug reports later I noticed that all errors occurred with a Turkish (tr) browser. So I changed my browser's default language to Turkish and sure enough I could see the error occur.
I had blogged earlier about a bug in dasBlog that affected Turkish users. When a Turkish browser reported an HTTP Accept-Language header indicating Turkish as the preferred language, no blog posts would show up. As fix, I suggested that users change their blog templates, but I knew that wasn't an appropriate fix.
I understand that Turkish prisons are not to be trifled with, but the question remains: why do Turkish people take such cruel and perverse delight in breaking our fine software? What's wrong with Turkey?
As with so many other problems in software development, the question shouldn't be what's wrong with Turkey, but rather, what the hell is wrong with software developers? Some of this is sort of obvious if you have any cultural awareness whatsoever.
In the United States, we would typically format today's date as 3/14/2008. In Turkey, they format it as 14.3.2008.
In the United States, we use commas to group digits, like so: 32,768. In Turkey, they group digits using a period, so the same number would be entered as 32.768.
These minor formatting differences are usually not a big deal for output and display purposes, but it's a whole different ballgame when you're parsing input. You'd naturally expect people to input dates and numbers in the format they're used to. If your code assumes that input will be in typical American English format, there will be.. trouble.
Most languages have this covered; there are functions that allow you to read or write dates and numbers appropriately for various cultures. In .NET, for example, it's the difference between these two calls:
Because no culture is specified, the first call will parse the number according to the rules of the default culture that code is running under. Let's hope it's running under a Turkish version of Windows, so it can parse the number correctly. The second call, however, explicitly specifies a culture. The "invariant" culture is every American programmer's secret dream realized: we merely close our eyes and wish away all those confusing languages and cultures and their crazy, bug-inducing date and number formatting schemes in favor of our own. A nice enough dream while it lasts, but instead of rudely asking your users to "speak American" through the invariant culture, you could politely ask them to enter data in ISO international standard format instead.
Anyway, point being, this kind of culture support is baked into most modern programming languages, so all you need to do is make sure your developers are aware of it-- and more importantly, that they're thinking about situations when they might need to use it.
But all that date and time formatting stuff is easy. Or about as easy as i18n ever gets, anyway. Strings are where it really starts to get hairy. Guess where this code fails?
switch (myType.ToLower())
{
case "integer" : ;
}
If you guessed Turkey, you're wrong! Just kidding. Of course it fails in Turkey. When we convert the string "integer" to upper and lower case in the Turkish locale, we get some strange characters back:
It's sort of hard to see the subtle differences here unless we ratchet up the font size:
I → lowercase → ı
i → uppercase → İ
There's obviously no way these strings are going to match "integer" or "INTEGER" respectively. This is known as the Turkish I problem, and the solution should feel awfully familiar by now:
That will produce the expected output, or at least, the output that matches the comparison in the original code snippet.
This is, of course, only the tip of the iceberg when it comes to internationalization. We haven't even touched on the truly difficult locales like Hebrew and Arabic. But I do agree with Jeff Moser-- if your code can pass the Turkey test, you're doing quite well. Certainly better than most.
If you care a whit about localization or internationalization, force your code to run under the Turkish locale as soon as reasonably possible. It's a strong bellwether for your code running in most-- but by no means all-- cultures and locales.
======================================================================== 같은 소스를 컴파일해도 터키에서는 돌아가지 않는단다. 언어의 문화적 차이 때문이다. 소수점을 마침표(.)와 쉼표(,)로 구분하는 차이, ToLower, ToUpper등 언어의 인식 차이(미쿡에서는 ToUpper를 소문자에서 대문자로 바꾸는 것으로 인식하지만 터키에서는 글자의 크기를 크게 만드는 것으로 인식한다.) 같은 코드를 컴파일하면 에러가 발생하는데 그나마도 브라우저를 터키어로 맞추지 않으면 볼 수 없단다. .NET에서는 그러한 문화적 차이를 표시해주는 코드가 포함되어 있는데 'Invariant'가 그것이란다.
한국도 마찬가지다. 많은 한국인들이 다분히 미쿡을 동경하지만 언어에 있어서는 한국어를 사랑한다. 일상생활에서 영어를 능숙하게 말하고 다니면 '어머 웬 잘난척? 즈질이야~'정도의 말을 듣는다.(나도 그런말 들어보고 싶다.) 한국어를 좋아하고 주로 사용하는 것은 좋은 현상이다. 그런데 상황에 맞게 사용해야 한다. 프로그래밍을 할 때 시간표시나 숫자표시등이 일치되지 않는 부분이 꽤 있다. 시간표현은 표현방식이 아주 다양하다. 24시표현, AM/PM, 오전/오후 시:분, 시/분, 시-분, 시 분 이렇게 다양한 표현을 모두 한국 문화에서 사용한다. 숫자표현은 어떤가? 그냥 숫자를 바로 사용하기도 한다. 그건 세계 공통이다. 하지만 숫자의 자릿수를 좀 더 명확히 하기위해 ,로 세자리 단위씩 끊어서 표현한다. 그리고 읽을 땐 4자리 단위로 읽는다. 한국에서 사용하는 숫자의 단위는 4단위다. 일, 만, 억, 조, 경, 해... 그런데 숫자는 3단위다. 미쿡에서는 숫자의 단위가 3단위다. 어렸을 적 새로운 단위가 제대로 정착되는데 혼란이 없다면 표기단위를 4단위로 하는 것이 좋을 것이라고 생각했었다.
글에서 문화적 차이를 뛰어넘으려면 ISO표준을 따르라고 권하고 있다.
물론 국제표준을 따르는 것이 가장 좋다. 그리고 그렇게 하지 못할 땐 입장을 확실하게 해야 한다. 무슨 말이냐면 한국식으로 하고싶으면 해당 scope내에서는 확실히 한국식으로 하고, 미쿡식으로 하고싶으면 확실히 미쿡식으로 하고 ISO대로 하고싶으면(ISO표준이 미쿡식은 아니다) ISO에 맞추어서 하란 말이다. 가끔 실무에서도 변수 이름으로 한국어발음을 억지로 영어로 적어놓은 경우를 자주 볼 수 있다. (실제 이런 이름을 쓰진 않겠지만 굳이 예를 든다면 show, view, print등의 내용을 bogi(보기)와 같이 억지로 쓰는 경우가 있다.)
상대가 정 모르면 사전을 찾아서라도 알 수 있으면 그건 괜찮다. 자신이 있지도 않은 단어를 만들어버리는 도대체 어떻게 알아보란 거야? 이도 저도 아닌 어중간한 지점에서 타협하지 마라!!
역시 내 머리는 체계적이지 못해서 'Code Craft'란 책의 힘을 빌리기로 했다. 그런데 이거 원서를 읽기는 부담스럽고 번역서를 읽자니 번역이 엉망이다. 일단 샀으니 열심히 보겠다만 번역한 사람이 어디서 굴러먹던 프리렌서인지 몰라도 번역 후 자기 글을 얼마나 열심히 읽었는지 알 수가 없다. 내용은 좋은 책이니 나한테 필요한 부분만 골라서 열심히 정리해보자
기본 습관 : 에러체크, 테스트, 디버깅 에러체크 : 코드를 작성하는 도중에 발생가능한 에러를 체크하고 막는 과정 테스트 : 발생가능한 에러가 있는지 찾아보는 과정(일단 테스트할 부분은 완성되었다고 가정) 디버깅 : 찾아낸 에러를 수정하는 과정이다.(말 그대로 버그잡기, 글로 따지면 퇴고다.)
코드 작성을 서두르지 마라. gotcha : 문법적으로는 맞지만 원하는 것과 다르제 작동하는 것. ==를 =로 잘못 타이핑 하는 것등.
아무도 믿지마라. 당신의 코드가 사용되는 곳은 다음과 같다 * 우연히 엉터리 데이터를 입력하거나, 프로그램을 잘못 작동하는 정직한 사용자 * 의식적으로 프로그램에 오작동을 일으키는 악의적인 사용자 * 잘못된 파라미터(주1)를 가지고 함수를 호출하거나 모순된 데이터를 입력하는 클라이언트 코드 * 프로그램에 적절한 서비스를 제공하지 못하는 실행환경(OS등) * 오작동을 하거나, 당신이 의존하는 인터페이스 규격을 따르지 않는 외부 라이브러리
짧은 코드가 아니라 명료한 코드를 작성하라 코드는 한눈에 들어와야 한다. 함수나 method는 20~30줄을 넘지 않는 것이 좋다. 그렇지만 짧다고 좋은 코드는 아니다. x += ++y; 이딴 식의 복잡한 수식은 절대 쓰지마라. ++y; x = x + y; 이렇게 두줄이면 충분할 것을 괜히 줄인다고 복잡한 수식을 사용하면 알아보기가 어렵다. 당신이 연산자 우선순위를 잘 알고있다는 것은 자랑할 수 있지만 아무도 같이 일하려 들지 않을 것이다. 극단적인 경우 지나치게 복잡한 연산식 때문에 컴파일러가 잘못된 코드를 생성할 수도 있다.(최적화 과정에서 발생하는 에러는 이러한 경우가 많다. 컴파일러를 거치고 난 어셈블러를 분석해보면 원래코드랑 조금 다른 것을 볼 수 있다. 그건 최적화를 거쳐서 그렇다.)
어설프게 만지면 안되는 것은 아무도 못 만지게 하라 * 객체지향언어에서는 맴버변수를 모두 private로 한다. * 모든 변수는 그 변수가 속할 수 있는 가장 좁은 범위(scope)안에 둬라. -- 무지하게 잘난 인간이 아니면 전역은 쓰지마라. -- 루프안에 선언해도 되는 변수는 루프안에 선언해라. -- method 내에서만 사용해도 되는 변수를 맴버변수로 올리지 마라.
경고를 무시하지 마라. 컴파일하면 경고(warning)이 뜨는 경우가 있다. 위험하다고 경고하는 거다. 그냥 넘어가면 안된다. 만약 특정 경고가 당신에게 중요치 않더라도 그대로 두지 마라. 나중에 그로인해 정말 중요한 경고가 눈에 안 띄게 될수도 있다.
요즘 세상은 메모리가 심하게 부족하지도 않고 컴퓨터가 무지 느리지도 않다. 그리고 프로젝트이 규모가 점점 커지고 있어 혼자서 프로젝트를 기획하고 완성시키는 경우는 거의 없다. 이런 세상에서 훌륭한 코드란 알아보기 쉬운 코드다. 물론 기본이 되는 것은 정확성이다. 제대로 작동하지 않으면 아무런 소용이 없다. 여기서 말하는 것은 어떤 프로그램을 만드는가가 아니다. 이곳은 프로그램이 돌아가는데는 큰 지장을 주지 않지만 알아보기 쉽고 유지보수에 편리한 습관이 무엇인지에 대해 고민하는 곳이다.
이름이 생명이다. 이름만 잘 지어놔도 설명할 것이 거의 없어진다. 특히 영어에 익숙하지 않은 사람들은 한영사전이 필수다. 그럼 어떻게 이름을 지어야 한단 말인가? 1. 변수 -> 명사로 함수 -> 동사 or 동명사로 짓는다. 변수(멤버변수, 속성)는 data의 의미를 가지고 있기 때문에 명사로 이름짓는다. 함수(멤버펑션, 메쏘드)는 동작하는 하나의 기능을 나타내기 때문에 동사로 짓되 힘든 경우 동명사로 짓는다.
2. 대문자 혹은 _로 단어를 구분한다. numOfValuses 혹은 num_of_values // getData(...) 혹은 get_data(...) 이런 식으로 사용하는 것이 원칙이다. 나의 경우엔 변수는 _로 구분하고 함수는 대문자로 구분하여 이름만으로 변수인지 함수인지도 구분할 수 있도록 사용하고 있다.
3. 알파벳으로 시작하라. C#이나 JAVA같이 유니코드를 사용하는 언어는 한글로 변수화 함수를 작성해도 무관하다. (난 가끔 한글로 작명하기도 한다.ㅋㅋ) 하지만 한국에서만 사용되는 source로 만들고 싶지 않다면 아무래도 영어를 사용하는 것이 좋다. 영어로 작명을 하는 경우 이름 중간에 공백이나 특수문자를 사용할 수 없고 숫자로 시작할 수 없다는 대 원칙만 지키면 이름으로 사용할 수 있지만 그럼에도 주의할 것은 있다. _(under bar)로 시작하는 이름은 system변수와 이름이 같을 수도 있으니 주의해야 한다. 당신이 무지 잘난 인간이 아니라면 사용하지 말기를 권장한다. 대문자로 시작하는 것은 class다. class의 경우 반드시 대문자로 시작해야 한다. 그러니 다른 것(변수,함수)들은 소문자로 시작하는 것이 좋다.