• Stop fighting

    JavaScript is a amazing language, and it continues to amaze me. But I learned it in the hard way because I was trying to translate it into classic OO concept, and I failed. Douglas Crockford is my JavaScript idol, here is what he says how he finally understands JavaScript.

    Eventually I figured out what was going on. After a lot of struggle, I eventually figured out that it was a functional language, and at that moment I stopped fighting it. I remember when that moment occurred, I was bicycling. I had just read the ECMAScript standard, which is a really difficult thing to understand. But I read through it, and then I had this epiphany when I was miles away from a computer. Oh, it's got functions in it, there are lambdas — I can do this now. It completely changed the way I thought about the language. In the end, the story ended successfully. We finished on time and on budget, Turner shipped it, and everything went great.

    The lessen that I learned is to stop fighting against new idea with my old knowledge. New idea may be odd, but it will be appreciated if you follow along, and it expresses similar semantics in the way you never thought of. I love the philosophy of Chinese philosopher Zhangzi, here are two pieces of excerpt that I feel helpful to understand new idea.


    The Ruler of the Southern Ocean was Shu, the Ruler of the Northern Ocean was Hu, and the Ruler of the Centre was Chaos. Shu and Hu were continually meeting in the land of Chaos, who treated them very well. They consulted together how they might repay his kindness, and said, 'Men all have seven orifices for the purpose of seeing, hearing, eating, and breathing, while this (poor) Ruler alone has not one. Let us try and make them for him.' Accordingly they dug one orifice in him every day; and at the end of seven days Chaos died.


    Paoding was cutting up an ox for the ruler Wen Hui. Whenever he applied his hand, leaned forward with his shoulder, planted his foot, and employed the pressure of his knee, in the audible ripping off of the skin, and slicing operation of the knife, the sounds were all in regular cadence. Movements and sounds proceeded as in the dance of 'the Mulberry Forest' and the blended notes of the King Shou.' The ruler said, 'Ah! Admirable! That your art should have become so perfect!' (Having finished his operation), the cook laid down his knife, and replied to the remark, 'What your servant loves is the method of the Dao, something in advance of any art. When I first began to cut up an ox, I saw nothing but the (entire) carcase. After three years I ceased to see it as a whole. Now I deal with it in a spirit-like manner, and do not look at it with my eyes. The use of my senses is discarded, and my spirit acts as it wills. Observing the natural lines, (my knife) slips through the great crevices and slides through the great cavities, taking advantage of the facilities thus presented. My art avoids the membranous ligatures, and much more the great bones. A good cook changes his knife every year; (it may have been injured) in cutting - an ordinary cook changes his every month - (it may have been) broken. Now my knife has been in use for nineteen years; it has cut up several thousand oxen, and yet its edge is as sharp as if it had newly come from the whetstone. There are the interstices of the joints, and the edge of the knife has no (appreciable) thickness; when that which is so thin enters where the interstice is, how easily it moves along! The blade has more than room enough. Nevertheless, whenever I come to a complicated joint, and see that there will be some difficulty, I proceed anxiously and with caution, not allowing my eyes to wander from the place, and moving my hand slowly. Then by a very slight movement of the knife, the part is quickly separated, and drops like (a clod of) earth to the ground. Then standing up with the knife in my hand, I look all round, and in a leisurely manner, with an air of satisfaction, wipe it clean, and put it in its sheath.' The ruler Wen Hui said, 'Excellent! I have heard the words of my cook, and learned from them the nourishment of (our) life.'
  • Static class vs Singleton

    Design pattern question is often asked in interview for developer. In an interview, I was asked to describe one design pattern that I am familiar, except singleton. Maybe the interviewer think that Singleton is too easy to answer. Yes singleton is a very simple, a sample is as follow.


    class Program
    {
     static void Main(string[] args)
     {
      Printer.Instance().Print();
     }
    }
    
    
  • Defensive programer , code analysis, and code review.

    I was born in China, English is not my native language, and I still have difficulties in writing in English. Once, I sent an email to my boss. In his reply he highlighted my typo and grammatical error. At first I felt embarrassed, but I immediately appreciated his effort of doing so. I am sure sometimes my email is confusing, but nobody has ever done that to me before. I guess it is because they don't want to hurt my feeling or save the time to correct me. I do use spelling check and grammar check functions of email app, and I never felt embarrassed, isn't that strange?


    I have been a programmer for years, I have made all the programming mistakes that can be made, and I still make, but less. Some developers correct my error, some don't. I felt embarrassed in early time, but I gradually accept the fact that my code sucks and appreciate their effort. Compilers also correct my mistake, I was frustrated in earlier time, but I never felt embarrassed by compilers.


    As I got more experience, I found that it was never easy to tell my fellow developers about their mistakes in coding or design. I was working in a software company. One of the senior developer resigned for a new job and my boss asked me to take over his project which I never touched. Firstly, I reviewed his code, I felt sick and wondered how a senior developer can write such crap. During the later knowledge transfer, I asked lots of critical question, I knew I hurt his feeling and he was unhappy. Personally, I think he is nice and funny guy, and I regret about that. From then, I try to be careful about my words when I express my opinion of others' code. Even then, it is still inevitable to hurt someone's feeling some time, if my opinion is too radical to him.


    Is this just my unique experience. In the book Debugging Microsoft .NET 2.0 Applications, author John mentions his experience in Chapter 3, "Assert, Assert, Assert, Assert". He argued with his boss about a section of code, which misused "Assert", and he said "Whoever wrote this needs to be fired! I can't believe we have an engineer on our staff who is this incredibly and completely stupid!". His boss got very quiet, grabbed the paper out of his hands, and quietly said, "That's my code." . And John resigned from the company later.


    Although it is not so new book, I find that it is still very useful. The author discusses some proactive tools to improves code quality, one of them is code analysis, and a chapter 8 is dedicated to topics "Writing Code Analysis Rules". I think code analysis is quite effective, because no matter how defensive you are as developer, you seldom can be embarrassed by a machine. Machine always reports the warning or error if you break the rules.


    For a while, I suspected the effectiveness of code review. My previous experience tells me that developers tend to be defensive for themselves. Why? If there is a large gap in coding quality and experience between the author and the expectation of the reviewer, the reviewer may ask the question like "How can a senior developer write such crap?", this make the author looks incompetent, so it is natural to for him to be defensive. In such case, code review will not be necessary. Maybe it will be more effective for the company to send the author for some crash course to close the gap, or the recruitment process needs to be reviewed to to find out why this gap is not caught in the first place. If the gap is small, generally, the code review is powerful software quality tool. It has been adopted by many good software companies. It is said that some company go to the length that code developed by junior developer can not be merge into trunk until it has been reviewed by senior developer. I am not sure if that is true, but code review not only improve quality but also transfers knowledge, and it can become an company culture that attract people. However we developers are still human, we should be very clear that, code review is review of code, but not performance review of employee. And we should not use words that target for people but not code. One senior developer once reviewed my code and said "You don't understand what object oriented programming is." I was upset for a while. Am I too vulnerable? Maybe, I am a human.

  • Import msbuid files

    The import construct is the re-usability mechanism in msbuild. When an import element is encountered, these steps take place


    1. The working directory is changed to that of the imported project file.

    2. Project element attributes are processed.

      If a value is already assigned to DefaultTargets attribute, then this is ignored, otherwise it becomes the value of DefaultTargets. If InitialTargets attribute is present, then the list of targets will be appended to the current list of InitialTargets.



    3. Project element nodes are processed.

    4. The working directory returns to importing the previous value.
  • DefaultTargets and InitialTargets

    DefaultTargets attribute is a list of targets to execute if no target is specified. The InitialTargets attribute is a list of tarets to be executed before any other targets. In the following example, the i1, i2, t1, t2 will be run in order

    <?xml version="1.0" encoding="utf-8" ?>
    <Project ToolsVersion="3.5" DefaultTargets="t1;t2" InitialTargets="i1;i2" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Target Name="t1">
        <Message Text="t1 is running" />
      </Target>
      <Target Name="t2">
        <Message Text="t2 is running" />
      </Target>
      <Target Name="i1">
        <Message Text="i1 is running" />
      </Target>
      <Target Name="i2">
        <Message Text="i2 is running" />
      </Target>
    </Project>