Thinking beyond source code

Recording my throughs


  • 首页

  • 关于

  • 归档

  • 标签

10 Rules of programming

发表于 2007-08-30 | 分类于 未分类

By Malcolm McLean

 

  • The rule of one

    Thou shalt have one copy and one alone of any item under edit.

    Never fork development. It becomes virtually impossible to keep more than one version of a program in synch. A bug is fixed in one program, but the matching file in the other program has some subtle changes essential to make it work. So the fix may or may not be posted in, it may not even be relevant. Very quickly you will arrive at a situation in which no one knows what is happening.

    The rule also applies to program objects that can be edited by the user. The user expects that the interface will show a snapshot of the real state of his data. This is very hard to achieve if every menu item is caching data. Unfortunately it is also a difficult problem to fix; you need to impose rigorous mangement on non-modal meus especially. This is particularly true if efficiency considerations make it impossible to do a general refresh with every data value entered.


    The rule of two

    Thy names shall be binomial.

    In most cultures people have two-part names, such as Christian name / surname. When Linnaeus developed his system of biological nomenclature, scientists quickly simplified it to a binomial system, e.g. Pan troglodytes for a chimpanzee, Homo erectus for primitive humans. Three-part or higher names are not names but descriptions.

    The rule is of great importance in variable naming. In many languages variables are usually a member of a named structure or object. This means that both the member and the enclosing instance must have single-part names for the whole to be readable. Eg candidate.name not candidate_trainee.firstname . Names longer than two parts can of course be used in simple expressions. The rule applies when variables must be embedded in complex expressions.


    The rule of three

    Thou canst cope with only three levels of indirection.

    A human can just about read a mathematical expression with three levels of nested parentheses. Go to four and understanding breaks down. Similarly humans can cope with three levels of loop nesting, or arrays with three dimensions, or pointers to pointers to pointers.

    These things are all related to the fact that we are adapted to living in a three-dimensional world. Hence a three dimensional array has a physical analogue. Looping, indirection and parentheses are all used to support multi-dimensional arrays, hence really part of the same rule.


    The rule of four

    Thou shalt pass only four parameters to a function.

    If you expect your caller to remember the parameters a function takes, their order, and what they do, then you can pass a maximum of four parameters. Five is slightly too many to remember without effort, or to scan unconsiously. Four parameters is also the threshold for machine optimisation on may platforms.


    The rule of five

    Thou shalt have only five levels in a tree.

    A binary tree with five levels has sixteen members at the leaves. This is about the most that can fit onto a single diagram. Your call tree will of course be much bigger than five levels deep in a large program. However it can be broken down into files, with no more than five levels within each file, and no more than five levels of files in each library, and no more than five levels of libraries in each program. Or in object-oriented languages, no more than five levels of methods within each class, and no more than five levels of class dependencies.


    The rule of six

    Thou shalt use up to six letters in a minor identifier.

    This one is imposed by Fortran 77, which only allows six letters in identifiers. However it is a good rule to keep to for minor variables. Generally a one letter convention will exist for many variables in a function, such as N for an integer total and i for a counter, x, y and z for Cartesian co-ordinates, and so forth. The problem comes when we have source x, y and destination x, y. We can't use x, y for both. However sourcex, destinationx will make expressions far too long. srcx, destx is more acceptable. The limit is about six characters.

    Six digits is also the maximum that a human can take in without trouble. Unfortunately this rule is impossible to enforce in a computer programming context, because frequently greater precision is required for technical reasons. However it tells us that the rule of six also applies to names made of arbitrary characters, like "memptr". Identifiers which are English words are obviously perfectly readable. That's why the rule of six mentions "minor identifiers".


    The rule of seven

    Thou shalt pass only seven parameters to a function.

    Humans can take in about seven objects without counting them. This means that seven parameters is the limit for a readable function call. The rule of four was the rule for a scannable function call. The difference between scanning and reading is that the scanned call can be accepted as a single unit, the read call has to be worked through parameter by parameter. More than seven arguments and the reader will lose track of which is which and have to count arguments, matching them to the manual. So the code becomes non-readable, and is only acceptable if the reader already knows what the function does without looking at its argument list.


    The rule of eight

    Thou shalt have no rule of eight. A foolish consistency is the bugbear of little minds.


    The rule of nine

    Thou shalt hardcode only nine explicit conditions.

    In a two dimensional array each non-edge cell has eight neighbours, plus itself to make nine. This is about the limit for coding each condition explicitly. It is also the number of directions in common use � North, South, East, West, North-east, North-west, South-east, South-west, plus one for "stay still". OK, I am running out of ideas; this could be the rule of eight or the rule of ten. An if ... else ladder of nine is not unacceptable. If we go to cubic arrays we have 27 cells in each 3x3x3 block, and we must use loops to index them. Similarly if we want more than the eight compass directions, it is time to code as degrees rather than by name.


    The rule of ten

    Thou shalt have only ten commandments in a list of rules.

    That one was obvious.



  • --
    Shit happens!

    物价飞涨,给80后们看的省钱指南[转]

    发表于 2007-08-05 | 分类于 未分类

    本文由 xwsxws 在 2007-8-4 09:51 发表于: 倍可亲.美国 ( backchina.com )

    物价飞涨,在老板涨工资还遥遥无期的情况下,节流即是开源。

          找出一张纸,把你的各项支出列出来,好好分析你的消费支出,从战略,战术层面来节约。你每月的支出至少可以减少20%。

          1.不要在网上到处泡MM,这个开宵很大,一个月至少在500,上不封顶,约会支出是很恐怖的,吃饭,泡吧,礼物,开房间,买套套这些费用都很高。如果还没有找到合适的女生,不得不约会MM,至少大家先视频一下,不要看她N年之前的旧照,或者艺术照,大家或者先电话聊聊。免得见面大失所望,碍于男人面子,你又不能开溜,只好忍痛请恐龙吃饭。即浪费青春,又腰包出血。最好的办法是参加些网友活动,什么爬山啊,打球啊,先见面,感觉不错再
    发展。

          2.年青人最大的一笔开宵就是谈恋爱。如果她动不动就去什么高档的消费场所,赶快甩了她。别癞哈蟆想吃天鹅肉,梦想找一个富婆,这个跟买彩票一样,还是踏实点,找一个朴实点的女生,门当户对。两人同居,房租可省,两人自己做饭,伙食费用可省。

          3.如果可以,把各种各样的打折卡全部带上,至少每月能帮你省1-200元。去饭店吃饭,如果没有带打折卡,跟老板要发票,再问不开票有没有打折,老板一般会给你打个9折。

          4.如果每个月要用一打安全套,建议你批量采购,可以到淘宝上去采购,一个才4毛钱,100个才40元,足够你用半年,每个月至少可以省20元。

          5.尽量买最便宜的药来吃,贵的药与便宜药化学分子式相同,效果是一样的,不必要为生产商付广告费。

        比方说感冒药,不必吃20多元的康泰克,服点1元的朴感敏,效果一样,都是起缓解感冒症状的,还是要靠自身的抵抗力。医生给你开完处方,如果你有当医生的朋友,问一问或者上网咨询,很多药并不是必要的都可以省略掉。

          6.把该打的疫苗全部都打上,比方说什么乙肝疫苗,如果可能每一两年,做一次体检.经常做做锻炼,生活有规律,保持身体健康,是你应对生活压力,通货膨胀,节省意外支出的最好药方之一,以上几项做到至少能帮你节省80%的医药费。

          7.晚上打的11:00以后一般都会有加班费,你可以跟司机说,不收加班费。司机一般都会同意。

          8.小心选择手机套餐,否则是套你没商量。有一种套餐会设定几个免费号码,把你的死党,GF,家庭号码加进去。如果可能,最好用 QQ,msn语音聊天。没事别去相信免费下载手机铃声之类的东西,也别拨打不熟悉的声讯电话.
     
    ----邮件发布

    & lt;/p>

    高高低低

    发表于 2007-07-16 | 分类于 未分类

    今天好像过山车,从最高处跌落到谷地。

    同时,明白了许多。
    那些是你最在乎的东西,
    就不要让她一点点变坏。& lt;br/>感情, 还是自己最清楚。
    自信,要自信。
    一起走出去。

    & lt;/p>

    心虚

    发表于 2007-07-02 | 分类于 未分类

    绵绵雨,
    绵绵情,
    断藕喋喋能续否。
    曾记起,
    似今夜。
    & gt;情意淳淳与世绝。
    也是。
    狂雷作!
    & gt;风骤起。

    < br />

    六级完后

    发表于 2007-06-28 | 分类于 未分类

    六级考完了,虽然说自己希望很渺茫,但是还是希望认真的对待,认真对待的意思呢,其实就是在考六级前不要有其他活动,不表示要复习之类的。很懒阿。

    有人考完四级,有人考完六级,考完以后干什么阿。考后综合症?

    需要一个计划了阿,假期也不知道能找到地方不。找不到该怎么办?扎实点吧。现在经常上上travian玩玩,感觉这个游戏很轻松,照顾照顾吧。

    电子书下载点

    发表于 2007-06-26 | 分类于 未分类

    ftp://202.96.64.144/pub/books 好地方。


    Technorati : chm pdf books

    python's language success

    发表于 2007-06-26 | 分类于 未分类

    Today i finish Learning Python, I think next step is Programming Python or Python manual, Python's success mostly is attributed to it's smart core and Introspection .

    What is introspection ?

    Everything is object, manipulate in unique way, Python know what it is, using a few set of build-in functions , such as type, str , dir , getattr.

    By keeping a tiny core, enclose everything else into module, leading Python a very healthy language, unlike the big , complex beast language (eg, Perl)

    Why smart ?

    • for loop is so smart , look this code ,

      methodList = [method for method in dir(object) if callable(getattr(object, method))]

    #a list generator

    • and or , and or is very useful condition judgment, like a Bash syntax .
    • 3 mainly data structure was selected, covered most of area of a language should be .

    推荐一款blogger离线编辑器

    发表于 2007-06-26 | 分类于 未分类

    可以贴图片。可以选tag可以加关键字。还可以帮着他们赚点小钱花花。不错的东西。他叫 Zoundry..

    >> Zoundry 网志编辑器™ 是一款全功能的编辑器,它将使您的网志发布过程更简单,更快捷,使用它就像使用文字处理器一样简单。此外我们还提供了额外的工具,使链接,标签,照片,音乐和视频文件等资源的添加更简单。下载试用一下吧!< /a> 看一看有了 Zoundry 之后,您能够在撰写和管理网志的工作中节约多少时间 - 无论在线还是离线。

    这种情况下,大学生应该

    发表于 2007-06-26 | 分类于 未分类

    场景: 有人和你对峙,并且还以特鄙夷的眼神说,就这样还大学生呢。。。
    以前我不知道怎么回答这个问题,好像觉得理亏一样,现在知道了。
    如果看着对方还像有点文化的,
    就回他, 你的意思是说? 自己没文化? (好奇的眼神)
    绝对噎死他。

    结论,不要老拿大学说事。

    GOOGLE maps 发送到手机?

    发表于 2007-06-23 | 分类于 未分类

    GOOGLE.CN 一向印象不好。
    不过今天发现一个很好的功能,你在maps.google.cn 上面查到一个地方,
    比如说查 "电子科技大学成都学院"
    然后我们在地图找到了这个地方,成都市郫县百叶路1号,好难记,
    发到手机上吧,发送到手机,然后输入我的手机号,没一会就收到了。
    +497501 这个号码够奇怪吧。

    而且这个服务是免费的哦。
    不知道google日历上的发到手机现在能用了不。能用就严重安逸了。

    1…1011
    Jiejing Zhang

    Jiejing Zhang

    110 日志
    11 分类
    24 标签
    © 2017 Jiejing Zhang
    由 Hexo 强力驱动
    主题 - NexT.Pisces