Effective C++, Part 1

看了《Effective C++: 55 Specific Ways To Improve Your Programs And Designs》有段时间了,基本概念是理解了,但是实践起来还是花了我大量时间。最近公司有个branch merge的任务分配给我,要merge的代码分支就是我一年前刚工作时manager分配给我的:把一个组件跨平台化。在我把平台相关代码和业务部分抽象分离开之后,项目就被外包到印度去了。

话说回来,在吸收了这本书的知识经验之后再回过头来看当初写的一些库函数,真是惨不忍睹⋯⋯于是一个没忍住,开始背着老板重新设计一部分代码,主要是XML解析函数,用的主要是三方库Xerces-C。于是用业余时间弄了大概一个星期,主要体会:

1. RAII (Resource Acquisition is Initialization)很强大。自己编了一个 wrapper class,解析得到的DOMDocument资源一旦被创建,就被初始化给wrapper class,比如说XDocPtr,然后使得XDocPtr在DOMDocument使用完毕后自动释放DOMDocument资源。一般来说一个 boost::shared_ptr (newObject) 就可以搞定,但是Xerces-C有自己的内存管理系统,调用delete语句销毁会导致系统崩溃。于是需要给 shared_ptr 指定自己写的释放资源函数。另外,为了使XDocPtr使用起来方便,重写了隐式转换操作符和赋值操作符,这样需要把DOMDocument当参数传递或者进行赋值操作时,XDocPtr就等同于DOMDocument了。如果要做的更彻底,->操作符也可以重写,这样调用参数时两者也等同起来了,这样一来,XDocPtr基本就是个隐形资源管理器。C++类可以存在于栈中的这个特点结合RAII就是C++最实用的技巧之一了。我们可以用这个技巧来管理mutex,在多线程环境下访问共享资源时,调用访问资源的函数时实例化一个mutex的包装类,然后函数结束时mutex就被自动释放了;我们也可以用它来写log;在测试性能时可以用它来计算函数调用耗时,等等⋯⋯⋯⋯

2. 避免使用#define 定义常量,避免使用macro定义函数,尽量多用class forward,少用#include来包含代码文件。道理显而易见,这样使程序体积减小;有利于维持清洁的全局变量;不容易出错;减少代码编译时间等⋯⋯然而class forward这一条真是整的我很无语。常常弄得编译单个文件没问题,合起来编译就出错。而使用inline函数代替macro的时候也碰到一些linker error,搞了挺长时间才弄明白原来inline函数需要在每一个调用的地方出现才行,也就是说需要把它放在头文件 .h 或者 .hpp 文件里。

3. 打算把这个类库编好后给大家分享,于是就需要写测试脚本测试每个方法,确保没有问题。另外还需要使库函数简单、方便易用。在测试过程中发现了内存泄露问题,研究了好久才发现Xerces-C库函数有个别方法存在内存泄露问题,于是得避免调用;有些库方法调用后竟然导致程序崩溃(Xerces-C真让人不放心啊⋯⋯);同时在写测试用例时又发现类库的设计有些不方便使用,于是不断修改,测试,还要在头文件写使用说明,注意事项等等。

4. 实战写过基础工具库函数才发现这不是件轻松的活,同时发现在实践中一些细节还是没掌握好,又来来回回翻看自己的笔记好多回。另外,stackoverflow是个好地方。很多问题不懂直接在上面提问了,几个问题得到了大牛们的秒回复,然后刷刷刷一大串讨论,真是受益匪浅。

5. 在测试中开始逐渐构建自己的测试框架,陆陆续续往里面添加了一些实用函数。

Posted in IT, 开发 | Tagged | 2 Comments

最近看的几部戏

周末看了《炸弹妞》(另外一个翻译名比较雷人,叫《绝世女屌丝》),太欢乐了。

冰与火之歌第二季终于出了,只有一集,果断把它看了。

风行出了iPad版本,于是就在iPad上用风行解决了另外两部电影:《那些年,我们一起追的女孩》和《我愿意》。

《我愿意》比较坑爹,刚开始给人感觉是一个潦倒的中年人(杨年华,孙红雷演)和一个富人(王洋)抢一个高薪高职都市白领单身丽人(唐薇薇,李冰冰演)。杨年华打爱心和关怀牌,王洋打过去感情牌(他是她过去的情人,为了赚钱消失不见7年后回归)。结果到最后杨年华翻身一变成了集团总裁,王洋反而破产了,完全没悬念了,孙红雷长的帅,还比王洋有钱,又是继《北爱》之后又一个高富帅完美击败相对“穷丑搓”的案例。剧情比较让人无语。演出也没啥亮点。

《那些年,我们一起追的女孩》是一个回忆校园爱情的。最让人伤感的是最后看着自己喜欢的女孩嫁人。换成通常狗血剧,剧情一定是到交换戒指那一刻男主角跳出来阻止女主角回心转意最后开开心心过着幸福快乐的日子。这个是沈佳宜同学就真的嫁了,柯景腾同学彻底没戏了,即使他们有那么多美好的过去值得回忆。真实而感人,记录有关青春的记忆里最美好的部分,值得一看。

Posted in 观后感 | Tagged | 4 Comments

Steve Jobs

ARTIST

He was an artist and master in product.

He had a strong urge to express his idea in product. This is pretty much a trait of artist. Because of this, he tended to ignore the customers’ voice. He believed that “the customers don’t know what they want until we’ve shown them”.

Even after he started Apple and involved in business, the goal was always product, it was never to beat the competition , or to make a lot of money. It was to do the greatest thing possible, or even a little greater.

He had a great sense of Aesthetics. In all of Jobs’s products, technology would be married to great design, elegance, human touches, and even romance. The most legendary design feature is simplicity and minimalism.

Another feature is the use of material. Material is important to product, it gives people special touch and visual feelings, like anodized aluminum, gorilla glass used in Apple products.

He liked design of Bauhaus, Raymond Loewy, Herbert Bayer style, which he thought was great at that time.

He also indulged his sense of aesthetics in building designs, like the design of Apple Store.

He also loved music, Bob Dylan and the Beatles are his all time favorite musicians.

CREATIVITY

Jobs believed that creativity and great ideas came from spontaneous meetings, from random discussions. “You run into someone, you ask what they are doing, you way ‘Wow’, and soon you are cooking up all sorts of ideas”.

He didn’t invent many things outright, but he was a master at putting together ideas, art, and technology in ways that invented the future.

He designed Mac after the power of graphical interfaces in a way that Xerox was unable to do.

He created the iPod after grasping the joy of having a thousand songs in pocket in a way that Sony never could accomplish. However, he was the person invented iTune and changed the music industry.

He didn’t create any movies, but he integrated technology with movie industry, supported and helped those artists in Pixar (He was the largest stakeholder and also the CEO in Pixar, later he sold Pixar to Disney and became the largest shareholder in Disney at that time). As a result, Pixar made many blockbusters, opened up the miracle of digital imagination.

He didn’t invent the tablet, but he hated tablets with stylus and knew what a real tablets would look like.

He didn’t invent smartphones, but he knew what a great smartphone would look like: touch screen centered, easy to use, etc.

By the way, he hated the idea of App Store because he don’t want third party to screw up his products, but eventually he was convinced by the Apple board.

CONTROL

“If you have an extreme passion for producing great products, it pushes you to be integrated, to connect your hardware and your software and content management. You can take full advantage of the specific OS and hardware feature. It makes the product easy to stand out and make a difference from others.

If you want to allow your products to be open, you can mass scale the production and easily gain large market share, but you have to give up some of your vision.”

You also loose focus and get distractions because the need to consider different hardwares, vendors, compatibility, portability. Besides, you loose control on some parts of the products.

However the fact that Apple controls everything in its product annoys a lot of people. They can’t customize the theme, user interface, they can’t watch porn content, they can’t even change a battery on their own.

BINARY and PERFECTIONISM

His view toward the world was binary: A person was either hero or shithead, something was either “the best thing ever”, or shitty, brain-dead, inedible. He would declare a design “completely suck” until he suddenly pronounced it “absolutely perfect”.

FOCUS, INTENSITY and MIND FILTER

He was able to dive into great depth of things he was interested, and he willfully filter out anything else as distraction.

COMPULSION and REALITY DISTORTION FIELD

Jobs distorted reality not only to himself, but also others, this was so powerful that people could get something done which he/she originally thought impossible.

OTHER

“Just because he feel it this way today doesn’t mean he feel it tomorrow. If you tell him a new idea, he’ll tell you that the thinks it’s stupid. But then if he actually likes it, exactly one week later, he’ll come back to you and propose your idea to you, as if he thought of it.”

Jobs was masterful at cajoling, stroking, persuading, flattering, and intimidating people.

He was good at making great presentations.

He knew exactly what your weak point is. It’s a common trait in people who are charismatic and know how to manipulate people.

He was impatient because he can’t restrain his will and desire of expressing his feeling.

He was mean to people in a lot of times.

“I came of age at a magical time,” he reflected later. “Our consciousness was raised by Zen, and also by LSD.”

He was a vegetarian, when he was young, he learned that he could induce euphoria and ecstasy by fasting.

About college, Jobs said: “I had no idea what I wanted to do with my life and no idea how college was going to help me figure it out. And here I was spending all of the money my parents had saved their entire life. So I decided to drop out and trust that it would all work out okay.”

PS: 571 pages, 1455 new words found in the book. It took me more than 2 months to finish reading it, starting in the end of December 2011, ended on March 9th, 2012. Painful but great learning experience, good practice for english learning.

(Notes after reading Steve Jobs by Walter Isaacson)

Posted in English, 读书笔记 | Tagged , , , , | 1 Comment