网络资源的拷贝粘贴 备份参考之用


28 September 2011

任意一个类型与字符串相加自动转化成字符串

I was doing code review, someone wrote something as the following.
It can totally build and it's the first time I know this.
 

int i = 1;

string s = i+""; //任意一个类型与字符串相加自动转化成字符串。没有直接调用C#类库,隐含间接调用的。

31 August 2011

powershell script to replace strings in a file

ReplaceStringInFile.bat
----------------
echo off

powershell.exe -command "Get-Content %1 | ForEach-Object {$_ -replace \"%3\", \"%4\"} | Set-Content %2"

timeout 5
----------------

28 July 2011

托管与非托管的区别

 
1.托管代码 (managed code)

由公共语言运行库环境(而不是直接由操作系统)执行的代码。托管代码应用程序可以获得公共语言运行库服务,例如自动垃圾回收、运行库类型检查和安全支持等。这些服务帮助提供独立于平台和语言的、统一的托管代码应用程序行为。

托管代码是可以使用20多种支持Microsoft .NET Framework的高级语言编写的代码,它们包括:C#, J#, Microsoft Visual Basic .NET, Microsoft JScript .NET, 以及C++。所有的语言共享统一的类库集合,并能被编码成为中间语言(IL)。运行库编译器(runtime-aware ompiler)在托管执行环境下编译中间语言(IL)使之成为本地可执行的代码,并使用数组边界和索引检查,异常处理,垃圾回收等手段确保类型的安全。
 
在托管执行环境中使用托管代码及其编译,可以避免许多典型的导致安全黑洞和不稳定程序的编程错误。同样,许多不可靠的设计也自动的被增强了安全性,例如 类型安全检查,内存管理和释放无效对象。程序员可以花更多的精力关注程序的应用逻辑设计并可以减少代码的编写量。这就意味着更短的开发时间和更健壮的程序。
 
2.非托管代码 (unmanaged code)

在公共语言运行库环境的外部,由操作系统直接执行的代码。非托管代码必须提供自己的垃圾回收、类型检查、安全支持等服务;它与托管代码不同,后者从公共语言运行库中获得这些服务。

 
3.托管代码和非托管代码的区别
 
非托管代码:一般是直接与OS打交道.
托管代码: 一般是委托中介(如.net framework 等)
 

所谓托管代码:
是指.Net下面编译为msil的一种字节码, 而非传统的编译语言编译入C,C++之流, 直接编译成二进制的可执行机器码.
就是说, 需要一个额外的解释器, 才能执行这种假的奇迹码. 在.net里面, 就是一个jit的编译器, 在执行的时候, 才编译成可执行的目标机器码, 才能真正执行起来.
 
这个概念类似于, java里面的那些.class文件, 需要jdk虚拟机才能执行起来是一个意思.
同样, 这个特性, 造成了一个很好的效果, 就是平台无关的特性.

26 April 2011

Difference between Heap and Stack

堆(heap)和栈(stack)有什么区别??

简单的可以理解为:  
heap:是由malloc之类函数分配的空间所在地。地址是由低向高增长的。  
stack:是自动分配变量,以及函数调用的时候所使用的一些空间。地址是由高向低减少的。

预备知识—程序的内存分配

一个由c/C++编译的程序占用的内存分为以下几个部分
1、栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
2、堆区(heap)   —   一般由程序员分配释放,   若程序员不释放,程序结束时可能由OS回收   。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。
3、全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,   未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。   -   程序结束后有系统释放
4、文字常量区   —常量字符串就是放在这里的。   程序结束后由系统释放
5、程序代码区—存放函数体的二进制代码。

二、例子程序
这是一个前辈写的,非常详细
//main.cpp
int   a   =   0;   全局初始化区
char   *p1;   全局未初始化区
main()
{
int   b;   栈
char   s[]   =   "abc ";   栈
char   *p2;   栈
char   *p3   =   "123456 ";   123456在常量区,p3在栈上。
static   int   c   =0;   全局(静态)初始化区
p1   =   (char   *)malloc(10);
p2   =   (char   *)malloc(20);
分配得来得10和20字节的区域就在堆区。
strcpy(p1,   "123456 ");   123456放在常量区,编译器可能会将它与p3所指向的 "123456 "优化成一个地方。
}  
二、栈的理论知识
2.1申请方式
stack:
由系统自动分配。   例如,声明在函数中一个局部变量   int   b;   系统自动在栈中为b开辟空间
heap:
需要程序员自己申请,并指明大小,在c中malloc函数
如p1   =   (char   *)malloc(10);
在C++中用new运算符
如p2   =   (char   *)malloc(10);
但是注意p1、p2本身是在栈中的。
2.2
申请后系统的响应
栈:只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出。
堆:首先应该知道操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,
会遍历该链表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表中删除,并将该结点的空间分配给程序,另外,对于大多数系统,会在这块内存空间中的首地址处记录本次分配的大小,这样,代码中的delete语句才能正确的释放本内存空间。另外,由于找到的堆结点的大小不一定正好等于申请的大小,系统会自动的将多余的那部分重新放入空闲链表中。
2.3申请大小的限制
栈:在Windows下,栈是向低地址扩展的数据结构,是一块连续的内存的区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的,在   WINDOWS下,栈的大小是2M(也有的说是1M,总之是一个编译时就确定的常数),如果申请的空间超过栈的剩余空间时,将提示overflow。因此,能从栈获得的空间较小。
堆:堆是向高地址扩展的数据结构,是不连续的内存区域。这是由于系统是用链表来存储的空闲内存地址的,自然是不连续的,而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见,堆获得的空间比较灵活,也比较大。
2.4申请效率的比较:
栈由系统自动分配,速度较快。但程序员是无法控制的。
堆是由new分配的内存,一般速度比较慢,而且容易产生内存碎片,不过用起来最方便.
另外,在WINDOWS下,最好的方式是用VirtualAlloc分配内存,他不是在堆,也不是在栈是直接在进程的地址空间中保留一快内存,虽然用起来最不方便。但是速度

也最灵活
2.5堆和栈中的存储内容
栈:   在函数调用时,第一个进栈的是主函数中后的下一条指令(函数调用语句的下一条可执行语句)的地址,然后是函数的各个参数,在大多数的C编译器中,参数是由右往左入栈的,然后是函数中的局部变量。注意静态变量是不入栈的。
当本次函数调用结束后,局部变量先出栈,然后是参数,最后栈顶指针指向最开始存的地址,也就是主函数中的下一条指令,程序由该点继续运行。
堆:一般是在堆的头部用一个字节存放堆的大小。堆中的具体内容有程序员安排。
2.6存取效率的比较

char   s1[]   =   "aaaaaaaaaaaaaaa ";
char   *s2   =   "bbbbbbbbbbbbbbbbb ";
aaaaaaaaaaa是在运行时刻赋值的;
而bbbbbbbbbbb是在编译时就确定的;
但是,在以后的存取中,在栈上的数组比指针所指向的字符串(例如堆)快。
比如:
#include  
void   main()
{
char   a   =   1;
char   c[]   =   "1234567890 ";
char   *p   = "1234567890 ";
a   =   c[1];
a   =   p[1];
return;
}
对应的汇编代码
10:   a   =   c[1];
00401067   8A   4D   F1   mov   cl,byte   ptr   [ebp-0Fh]
0040106A   88   4D   FC   mov   byte   ptr   [ebp-4],cl
11:   a   =   p[1];
0040106D   8B   55   EC   mov   edx,dword   ptr   [ebp-14h]
00401070   8A   42   01   mov   al,byte   ptr   [edx+1]
00401073   88   45   FC   mov   byte   ptr   [ebp-4],al
第一种在读取时直接就把字符串中的元素读到寄存器cl中,而第二种则要先把指针值读

edx中,在根据edx读取字符,显然慢了。

 

2.7小结:
堆和栈的区别可以用如下的比喻来看出:
使用栈就象我们去饭馆里吃饭,只管点菜(发出申请)、付钱、和吃(使用),吃饱了就走,不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作,他的好处是快捷,但是自由度小。
使用堆就象是自己动手做喜欢吃的菜肴,比较麻烦,但是比较符合自己的口味,而且自由度大。

堆和栈的区别主要分:
操作系统方面的堆和栈,如上面说的那些,不多说了。
还有就是数据结构方面的堆和栈,这些都是不同的概念。这里的堆实际上指的就是(满足堆性质的)优先队列的一种数据结构,第1个元素有最高的优先权;栈实际上就是满足先进后出的性质的数学或数据结构。
虽然堆栈,堆栈的说法是连起来叫,但是他们还是有很大区别的,连着叫只是由于历史的原因。

25 March 2011

双程计

 从google reader上看到的,挺逗的一篇

http://jacquesmattheij.com/A+tale+of+two+programmers

A tale of two programmers

When MSX and Atari ST were still 'hot', I contracted for a short while for a game company in the Netherlands, called Aackosoft, in Leiderdorp, a small town near The Hague. The reason it was only short was because the company failed spectacularly (the financial director came in one night and started shredding documents, I just packed my bag and left). Other than the management, the people working there were great.

We wrote interesting programs, the pay was OK (assuming you got paid), and the amount of knowledge floating around there was amazing. The designers were as good as I've ever seen, given the limited display capabilities of the various platforms available. For me, two people stood out: Steve and Chris, both from the UK. Because most of us had a very long commute, we slept 'on campus', one section of the building was something that is best described as a college dorm. After work we'd get together, order pizza or Thai food, talk shop and play games, sometimes our own games (Indy500, FlightDeck), sometimes those by competitors (does anybody even remember Gauntlet?).

Steve and Chris were as unalike as I've ever seen. Steve would toss out reams of code, sometimes creating the skeleton of a game in a few manic nights of coding and then he'd run out of steam to become slow as molasses. At roughly this point in time, Chris would enter the picture. He'd take the pile of work that Steve had done, and bit by bit, he'd clean it up and make it reliable and efficient. They knew each other so well that they didn't actually discuss the code much, it just got passed back and forth in this fashion until the job was done, usually in record time.

Their secret was obviously their complimentary characters and the fact that they'd grown up together, and had gotten to rely on the other guy 'having your back', as opposed to spending endless hours on transferring the knowledge Chris had been through this so many times that he knew quite well what to expect.

Today we'd probably call this 'pair programming', but it was pair programming in a way that was far more than the sum of its parts. Chris wouldn't be able to come up with an original work if his life depended on it. Steve would not be able to finish a job if you threatened to fire him. But as a team, they worked out splendid. They typically had their releases based on the same storyboards ready before we'd had the skeleton fleshed out.

Over the years, Steve had collected a whole encyclopedia of useful chunks of assembly code and he would beat these in shape just long enough for Chris to find his way.

Co-dependency amongst programmers. I've never seen it afterwards, and I don't really expect to see it ever again, it was one of those small things that remain as unique on the day that I first came across it as it is today.

But what I have seen is 'halves'. I've seen Steve's (I'm fairly strongly in his 'camp' when it comes to personality, when the engine works, I usually find it very hard to motivate myself to continue, that was the interesting bit for me), and I've seen Chris's.

Typically they wander around big companies looking for their soulmate, but they never ever run into them in a way that they recognize each other, or manage to hit it off on a personal level. But it makes me wonder if there isn't a use for a 'programmer dating service', where the Steve's and the Chris's of this world can meet up and achieve miracles that they alone would not be able to realize.

中文原文地址

http://www.aqee.net/2011/03/22/a-tale-of-two-programmers/

当MSX和Atari ST还很‘火’的时候,我在荷兰的一家叫做Aackosoft的游戏公司里短暂的就职过一段时间,这个公司位于Leiderdorp —— 离海牙不远的一个小镇。之所以短暂,原因是这个公司神奇的倒闭了(一天晚上财务主管一进来就开始粉碎各种文件,我只好拿起公文包离开了)。除了管理方面的问题外,这里工作的人都很不错。

要开发的程序非常有趣,这里的薪水还行(假如你是拿薪水过日子的),开发过程伴随着大量的知识学问,让我惊叹不已。这里的设计人员都非常的优秀,他们让这不通用的显示效果能够在各种平台上使用。对于我,有两个人格外的吸引我:Steve 和 Chris,他们都是英国人。我们大部分人下班后都会一起坐一段很长的路程,我们住在“校园宿舍”里 —— 因为对这栋建筑的这个部分最好的描述就是校园宿舍。下班后我们就待在一起,我们叫了匹萨或泰国食品,聊天、玩游戏,有时是我们自己的游戏(Indy500, FlightDeck),有时是竞争对手的(是否还有人记得Gauntlet?)。

Steve 和 Chris 这两个人极不相似。Steve 讨厌大量的编码工作,他有时会疯狂的花上几个昼夜的时间把一个游戏的框架搭建起来,之后他就会像泄漏气的脾气,行动慢慢腾腾,像个蜗牛。而大概就在这个节骨眼上,Chris入场了。他捡起Steve已经完成的那一大堆代码,一个字节一个字节的,规整清理,使之可靠、高效。他们之间是如此了解,根本不需要讨论哪段代码是干什么、为什么这样写,只是用这种方式来回交替的进行,直到任务完成,通常都是迅速顺利的搞定。

这其中的奥秘显然是得益于他们值得称赞的性格,长期共处培养出来的融洽,以及形成的一种依赖于对方的习惯,而不是相反的用大量的时间来相互传授自己的知识和用意。Chris 已经无数次的这样配合Steve,已经十分清楚的知道Steve想干什么。

如今我们也许可以称这为“结对编程”,而这种结对的方式产生的效果远超了他们两个作为单独个体的总和。Chris 如果一直依赖于这种工作方式,那他将不会有自己的原创作品。而Steve一旦失去了Chris,将不能完整的完成任何一个工作。可作为一个团队,他们做出了出色的东西。就像是他们在搭起骨架,填充内容之前,脑海中有了共同的图纸,这是他们能成功完成任务的基础。

数年里,Steve已经积攒了犹如大百科全书那样丰富的有用的程序代码,这些足够Chris用来发现他的思维轨迹。

这是程序员中的合作依赖。之后我再也没有遇到这种情况,我也并不是真的想盼望看到这样的组合出现,这只是那些日子里能让我感到独特、至今回忆的一件小事,就像发生在昨天。

我所看到的是一种‘热情’。我看到了Steve的 (从个性上来讲,我更喜欢他,但当我发现有趣的事情时,我却不能像他那样富有激情的工作)。我看到了Chris的。

他们曾徘徊在各大公司里寻找他们的精神伙伴,但从来没有遇到这样能够相知、能超出工作范畴、从个人角度上相互接受的人。这让我产生奇想,也许应该有个“程序员约会服务系统”,像Steve和Chris这样的人能够遇到一起,一起合作创造出他们各自独自根本无法想到的奇迹来。

 

30 January 2011

求最大公约数(欧几里德算法)

求最大公约数的欧几里德算法,也称作辗转相除法,如下:

int GetGCD(int m, int n)
{
if (n = 0)
{
return m;
}
return GetGCD(n, m % n);
}
(GCD = Greatest Common Divisor)

比如
GetGCD(18, 24)
-> GetGCD(24, 18)
-> GetGCD(18, 6);
-> GetGCD(6, 0);
-> return 6;

----------------------------------------

另外,最小公倍数 = 两数之积 / 最大公约数
所以 GetLCM(m, n) = m * n / GetGCD(m, n)
(LCM = Least Common Multiple)

比如
GetLCM(18, 24) = 18 * 24 / 6 = 72

24 January 2011

The two egg problem solution (copy-pasted from http://classic-puzzles.blogspot.com)

Google Interview Puzzle : 2 Egg Problem


My intention here is not to trouble Google interviewers. I was just collecting some classic puzzles and found this one and a small Google search showed me that this is a Google interview puzzle to my pleasant surprise. But many of the answers I found were either wrong or totally twisted. I am making no surety of the answer I give and I am open to your remarks or suggestion or corrections.


The Standard Problem in simple writing goes like this:

* You are given 2 eggs.
* You have access to a 100-storey building.
* Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100 th floor.Both eggs are identical.
* You need to figure out the highest floor of a 100-storey building an egg can be dropped without breaking.
* Now the question is how many drops you need to make. You are allowed to break 2 eggs in the process



If you are one of the people who likes to solve a puzzle before seeing the answer you must quit the blog now and come back later for checking the answer.


Now that this is a Google interview question I am taking the normal "Interview-Style" of solving a problem. Simply saying thinking aloud through the solution from worst to the best correcting the flows optimizing the solution or taking the 5-minute hard thinking acting pause to a problem, which you know already and just want to make your interviewer think that you are a challenge lover.


Solution

Drop the first egg from 50.If it breaks you can try the same approach for a 50-storey building (1 to 49) and try it from 25th floor. If it did not break try at 75th floor. And use linear search with the remaining portion of storey we need to test. For example if the first egg breaks at 50 we need to try all possibilities from 1 to 49.

Now this looks a feasible solution. In computer student's jargon do a binary search with first egg and linear search with the second one. Best case is log (100) and worst is 50.


Now the optimal solution for the problem is that you figure out that you will eventually end up with a linear search because you have no way of deciding the highest floor with only one egg (If you broke one egg and you have to find the answer among 10 all you can do is start from the lowest to the highest and the worst is the total number of floors). So the whole question grinds up to how to make use of the first egg to reduce the linear testing of the egg.


(For strict computer science students, well this problem can be solved using binary search on the number of drops needed to find the highest floor.)

Now let x be the answer we want, the number of drops required.

So if the first egg breaks maximum we can have x-1 drops and so we must always put the first egg from height x. So we have determined that for a given x we must drop the first ball from x height. And now if the first drop of the first egg doesn't breaks we can have x-2 drops for the second egg if the first egg breaks in the second drop.

Taking an example, lets say 16 is my answer. That I need 16 drops to find out the answer. Lets see whether we can find out the height in 16 drops. First we drop from height 16,and if it breaks we try all floors from 1 to 15.If the egg don't break then we have left 15 drops, so we will drop it from 16+15+1 =32nd floor. The reason being if it breaks at 32nd floor we can try all the floors from 17 to 31 in 14 drops (total of 16 drops). Now if it did not break then we have left 13 drops. and we can figure out whether we can find out whether we can figure out the floor in 16 drops.

Lets take the case with 16 as the answer

1 + 15 16 if breaks at 16 checks from 1 to 15 in 15 drops
1 + 14 31 if breaks at 31 checks from 17 to 30 in 14 drops
1 + 13 45 .....
1 + 12 58
1 + 11 70
1 + 10 81
1 + 9 91
1 + 8 100 We can easily do in the end as we have enough drops to accomplish the task


Now finding out the optimal one we can see that we could have done it in either 15 or 14 drops only but how can we find the optimal one. From the above table we can see that the optimal one will be needing 0 linear trials in the last step.

So we could write it as

(1+p) + (1+(p-1))+ (1+(p-2)) + .........+ (1+0) >= 100.

Let 1+p=q which is the answer we are looking for

q (q+1)/2 >=100

Solving for 100 you get q=14.
So the answer is: 14
Drop first orb from floors 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100... (i.e. move up 14 then 13, then 12 floors, etc) until it breaks (or doesn't at 100).

Please feel free to correct or post any comment on the solution or the answer.

0/1 KnapSack problem in C#


The following program find a best solution in 0/1 KnapSack problem, but it does not pick all the best solutions.

        public static int CalculateBagProblem(int BagVolumn, List<int> items, out List<int> chosenItems)

        {

            chosenItems = new List<int>();

            if (items.Count == 0)

            {  

                return 0;

            }

            else if (items.Count == 1)

            {

                if (items[0] <= BagVolumn)

                {

                    chosenItems.Add(items[0]);

                    return items[0];

                }

                else

                {

                    return 0;

                }

            }

            else

            {

                // without the first item

                List<int> remainItems = items.GetRange(1, items.Count - 1);

                List<int> chosenItemsInRemainItems = new List<int>();

                int withoutFirstItemResult = CalculateBagProblem(BagVolumn, remainItems, out chosenItemsInRemainItems);

                

                // with all the items

                int withFirstItemResult = 0;

                List<int> chosenItemsInItems = new List<int>();

                if (BagVolumn >= items[0])

                {

                    withFirstItemResult = CalculateBagProblem(BagVolumn - items[0], remainItems, out chosenItemsInItems) + items[0];

                    chosenItemsInItems.Add(items[0]);

                }

 

                // select max from the two results               

                if (withoutFirstItemResult >= withFirstItemResult)

                {

                    chosenItems = chosenItemsInRemainItems;

                    return withoutFirstItemResult;

                }

                else

                {

                    chosenItems = chosenItemsInItems;

                    return withFirstItemResult;

                }

            }

        }

 



30 December 2010

Example of the Locus Control Test

Locus of control refers to how a person perceives the cause of life events. Someone with an internal locus of control would generally perceive himself or herself as responsible for certain occurrences (his or her actions would have a direct bearing on the result). On the other hand, a person with an external locus of control would most often blame (or thank) fate, destiny, luck, society, or some other force beyond his or her control.
Snapshot Report
Success Orientation
 51
You have a mixed attribution style when it comes to success. Sometimes you interpret your success as a result of your skills, intelligence, nice personality, etc. Other times, you attribute it to external factors, such as luck, ease of task, or other people's help. As a consequence, you don't always take the credit you deserve. Your self-esteem, motivation, and general well-being would most likely be improved if you realized that you actively influence positive events in your life.

29 December 2010

How to hide Thinking at Work so that the Non-Programmers don't suspect Slacking?

Better programmers than me can write in essays about walking around with a coffee mug and call it programming. And it's perfectly accepted at a place that knows the business. Or see what Gregory House (TV show "House M.D.") does when he is thinking.

But what about the other places where you are the only programmer?

If you don't stare at boring stuff on the monitor for 8 hours straight, co-workers suspect you being a slacker. Yes, not the managers who see the output. Only the co-workers who see the process and can't relate to this kind of work.

Yesterday I had to explain to a trainee of some other profession that software development is like flying. The explanation from the Hitchhiker's Guide to the Galaxy. I don't think she bought it.

link|flag
88  
Seriously? Why the 3 close requests? At the moment there are 9 answers, with most of them upvoted. People are interested in this question/topic. – stesch May 1 '09 at 13:39
4  
This might be better as a community wiki question? – James McMahon May 1 '09 at 15:25
17  
If it gets closed it's an automatic reopen vote from me. – cletus May 1 '09 at 23:50
5  
Note: according to Jeff this is programming related blog.stackoverflow.com/2009/04/… "What is a programming related question? Here's a solid set of guidelines generated by the SO community itself: ... Questions about social engineering, management, or career building, ergonomics, or other "soft" topics related to development work." – cletus May 2 '09 at 17:28
28  
rofl, to paraphrase the question "how do I hide thinking at work, because thinking is discouraged by my employer" – STW May 2 '09 at 19:24
show 14 more comments

locked by Michael Myers 12 hours ago

35 Answers

1 2
up vote 145 down vote accepted

It's funny you ask this question because just yesterday I had a client blow up at a colleague and I who were discussing Google Analytics and Macbooks. Now this client gets charged for our time and we both exclude such time from our timesheets but such breaks are actually important: you can mentally recharge and it can help to work through problems and so on.

Now I put people into two categories:

  • Process-oriented: these people are concerned with how you do things. Do you turn up for work on time? Is your desk neat? Have you filled out your weekly status report? And so on; and
  • Results-oriented: within reason, these people don't care how you do something, they just care that you do it.

I am firmly in the second category and I have a real problem with people in the first. In my experience, people in the first category seem to suffer from insecurity. It's a fundamental psychological principle that controlling your environment makes you happy and such people, who are faced with something where they feel out of control or don't know how to achieve the best results, resort to controlling their environment by dictating how you do things.

You're right in that this is particularly problematic for non-programmers when dealing with programmers. They often just don't understand what we do and how we do it, like how things can take longer than estimated and so on.

Yesterday's case was even worse because this same guy would sit with a colleague and discuss the house she's buying and other things in the middle of doing his own work (and he's charging his clients for his time and I'm sure he's not charging for such downtime), which I found particularly galling as a real double standard on downtime.

So how do you deal with it? Preventively. Try and associate with results-oriented people by choosing the right job, staying in a good job, leaving a bad one, hiring the right people and so on. There will be a point where you'll simply have to put up with such nitpicking however. You can try and explain how the process doesn't work where you just sit there and stuff happens in some linear fashion but in my experience more often than not non-programmers just don't get it and you're wasting your breath and it'll sound like you're just trying to make excuses for slacking off.

In my case, as a consultant, I'm simply going to finish the current project (which is soon thankfully) and then respectfully decline any future work as I've already got more in the pipeline than I can already do.

link|flag
48  
I completely agree. I used to work for one client that would complain about the amount of times I showed up an hour or two after everyone else, but wouldn't consider the fact that I leave four or five hours after everyone else and that I produced more results in less time than the permanent staff. Results-Oriented people tend to be a joy to work with, process-oriented people [in my experience] tend to nit pick and micro-manage and drive me crazy. If you want the job done exactly your way, by all means go ahead and do it, otherwise go away and let me do what you hired me for. – BenAlabaster May 1 '09 at 13:30
6  
I'm with you both. Also had a month ago a quarrel about me arriving an hour or so after everyone else. When I said I stay correspondingly longer in the evenings they told me that nobody actually sees me do that. Between the lines I got they don't trust me. Suggestions to check out Windows logs or introduce any other kind of observation were declined however. And yes, I'm also basically the only developer there. – User May 1 '09 at 15:38
18  
Fair points, but it smells of a bit of a rant to me. You also exhibit the classic programmer need to divide everything into black/white, 1/0... when talking about Process/Results oriented. If you're dealing with anyone of average or better intelligence, it often becomes very obvious to them that they've been "pigeon holed" and is a sure fire way of degrading a working relationship. In fact, the act of pigoen holing is itself a way of reducing the complexity of human interaction, ie "controlling your environment". Hmmm... Does that make you Process or Results oriented? ;) – Ash May 2 '09 at 6:36
6  
I think it's fine if results-oriented boss leans toward being process-oriented one if results are dissapointing. – Arnis L. Jul 21 '09 at 18:22
2  
while i would lean more towards the second category, i cannot see this as an either-or, mutually-exclusive grouping. yes, granting folks the autonomy and freedom they need to achieve the results is very important as it liberates people. But at the same time, caring on about results alone can lead to compromises to mandatory standards, ethics, security, or safety requirements, etc. Those items belong to the process-oriented category. And i have seen such compromises happen. There has to be a balance for these work practices. – icelava Jul 28 '09 at 6:35
show 9 more comments

One solution could be this:

During their lunch break get them to do a simple task like as a crossword. Ask them to be conscious of the amount of time they spend reading/writing vs. the amount of time they spend thinking [or looking up the answer]. Tell them that this is exactly the same as the time you spend reading/typing vs. the amount of time you spend thinking and looking up answers.

Better still Sudoku because it can involve hours of trying to figure out where you went wrong and why none of your numbers add up. So while the task itself seems relatively easy, after all, it's just numbers in boxes it can take time. Get them to predict how long it'll take them before they start and see if their prediction is right - guaranteed it won't be. You can hit them with a double whammy for why your time expectations never quite add up either.

This is the only way I can really think of to get them to understand the way engineers work. I see lots of "those types just don't get us types" - people are adaptable, people can understand things if presented in a fashion they can understand, it's just that nobody's ever taken the time to present them with an argument they get.

link|flag
23  
I really like this explanation. I'm going to use it when needed. – Sandor Davidhazi May 6 '09 at 22:35
3  
Wicked analogy! +1 – John MacIntyre May 7 '09 at 18:23
1  
That is a fantastic analogy. I'm going to have to remember that. – Colin Cochrane Jul 21 '09 at 17:41
5  
The real question should be: How should non programmers in the office hide gossipping about American Idol and other such crap so that us programmers don't suspect slacking! – BenAlabaster Aug 10 '09 at 16:33
show 6 more comments

alt text

Just let your build scripts run, see lots of command lines spewing thousands of useless trace messages, and you're all good...

link|flag
38  
Must... resist... urge... to... upvote... – Michael Myers May 1 '09 at 15:09
7  
@mmyers - don't worry, I've got ya covered +1 :P – BenAlabaster May 1 '09 at 15:15
11  
BRB- building screensaver of never-ending compiler output – Graphics Noob Aug 26 '09 at 20:53
show 7 more comments

Years ago I had a gig writing some "Business Basic" stuffs. I was teamed up with a rather "brilliant" programmer with a strange work flow..

He would sit at his computer.. for half to 3/4's of the day.. and play doom / quake / wolfenstine 3D (he was really into the FPS). when he had enough, He'd fire up the editor and just type. At the end of the day, whatever he wrote.. was gold.

We had a new account manager start with the company that walked in on him playing one morning and in dealing with her, was not very polite.

She complained to the CEO that "one of those programmers isn't doing any work at all but playing those violent video games". To which the CEO replied to her "YOU DIDN'T DISTURB HIM DID YOU!?!?".

The games helped him think. He designed, wrote, tested and debugged all of the code in his head while he was playing. Once he was done, it was just a matter of brain dumping and getting it all out.

link|flag
44  
That's ridiculous and unprofessional. – Tim May 1 '09 at 14:18
55  
Upvote not because I agree with the attitude, but just because I love the story. :-) – T.E.D. May 1 '09 at 14:19
25  
@Tim - Maybe he gets paid to produce good code (rather than to strictly 'think and code'). If he produces enough golden material, then he's doing his job. – StuffMaster May 1 '09 at 21:40
14  
Thinking isn't something you can do for eight hours straight and still be productive. Downtime is a vital part of the cognitive process -- it's when your brain gets a chance to rearrange what it already knows and spot correlations it couldn't while under load. How many of us have come up with some of our best ideas in the shower? If this programmer knows how and when to take their downtime, more power to them. Sounds like it works. – Joe White May 2 '09 at 19:38
21  
Was this programmer's surname Carmack? – Arec Barrwin Oct 16 '09 at 15:40
show 18 more comments

Why, leave something verbose running on the terminal, of course.

link|flag
33  
I've actually had my boss walk by my desk while doing some SVN dumps and he said "That's what a programmers screen is supposed to look like" It hurt a bit, but now I have an easy "I'm busy!" sign. – Wally Lawless May 1 '09 at 13:41
5  
@Power-coder: You mean a programmer's secondary screen isn't supposed to be permanently etched with a browser window displaying the StackOverflow website? – BenAlabaster May 1 '09 at 14:01
8  
One well written line of code can replace a thousand bad ones. – Sneakyness Jul 26 '09 at 16:16
show 1 more comment

Take a walk

I've found that it's more the appearance of not "working" than anything else. If I leave my desk for a few minutes and walk around outside no one thinks anything of it. Much different than sitting at your desk looking idle.

link|flag
2  
I agree. It's a perception thing. Just sitting is always lazy. But moving either fingers or legs convinces people you're /really/ working. – Matthew Flaschen May 2 '09 at 0:46
3  
Out of sight, out of mind. – Walt Gordon Jones May 2 '09 at 20:22
15  
ps walking around with a piece of paper in your hand = working in most places. – Martin Beckett Jul 8 '09 at 20:30
3  
MGB is right. I got away with wandering the halls in High School for a very large amount of time by simply walking around with paper. – Sneakyness Jul 26 '09 at 16:17
show 1 more comment

Here's a solution from commandlinefu.com:

cat /dev/urandom | hexdump -C | grep "ca fe"

It will make some data scrolling off the terminal.

alt text

link|flag
10  
That's what I appreciate, a command-line solution to any programmer's problem! – thrashr888 23 hours ago
4  
Did you mean hexdump -C < /dev/urandom | grep "ca fe" – Sam 18 hours ago
3  
Add | pv -q --rate-limit 75 for some extra slowness. – Cristian Ciupitu 18 hours ago
show 1 more comment

How about thinking on the lav (the john)?! Of course it limits you to one thought session a day unless you don't mind people thinking you have diarrhea

link|flag
4  
lol +1, I do some of my best thinking on the loo. It's the only time I'm not constantly interrupted by coworkers asking retarded questions I don't have time to answer. – BenAlabaster May 1 '09 at 13:37
10  
Works at home, too. Unless you have toddlers constantly banging on the door. – Even Mien May 1 '09 at 13:40
3  
My office is noisy. I use this technique surprisingly frequently during the daily, shouted political argument going on one aisle over. The important thing is finding the lavatory in the basement or up a floor that's less-frequented. (Fewer distractions.) – Greg D May 1 '09 at 14:21
11  
I had one place where I'd do that multiple times a day, because the handicapped stall was bigger than my cubicle. :-) – T.E.D. May 1 '09 at 14:21
7  
The paper in the lavatory isn't so good for taking notes ... and let's not talk about the pen. – JeeBee May 1 '09 at 15:30
show 6 more comments

Think on paper, i.e., make a note of your mental thoughts. I find diagrams make things a lot clearer for me when thinking anyway, so it's a win-win.

link|flag
15  
Paper is so passé, get a whiteboard... – BenAlabaster May 1 '09 at 13:52
4  
@balabaster, ii actually prefer paper for it is much more portable and scaleable (On a whiteboard you have to erase everything when it's full, on paper you just get a new paper) – Pim Jager May 1 '09 at 14:30
2  
@Pim Jager - Understandably - I think in this day and age, I think we need to come up with electronic white boards that just dump the contents to file and then sync with the computer when you connect it up. Something lightweight enough to carry to the board room if necessary. – BenAlabaster May 1 '09 at 14:39
2  
That's it, now I want one of these: e-beam.com/products/whiteboard.html – BenAlabaster May 1 '09 at 14:43
show 10 more comments

Pull out some paper and scribble technical-looking stuff on it. Then when you're thinking, hold your pen and have one hand on the paper. Occasionally add something to the paper.

If you're working on hard problems, you'll probably need a pen and paper to assist your train of thought anyway.

link|flag
3  
This "pen and paper" you speak of -- can you explain what it is and how it works? – Gabe yesterday
show 1 more comment

Pretending you're working on something that's physical, not mental, is a short term solution, I think.

You'd better teach your co-workers that thinking takes most part of the process of programming. Of course, it's a long term solution that may show no value on the first few weeks - or months - but it will surely pay off.

They'll even leave you alone when they see you're quiet, with a mug of coffee in your hand.

Leave him there, they'll say, he's doing his best.

link|flag
4  
+1 Exactly, why try to pretend you're working. That's generally the stance someone who isn't working takes. Just do what you do! – Robin Day May 1 '09 at 13:24

I honestly don't think you can. Sure, you can "pretend" you're working by scribbling, having technical websites open. But if you're doing that, you're not doing the actual "thinking" bit that gets the work done.

I'd stick to worrying about how you get your work done best and not trying to appear to be working to others. If it matters to them they will know the truth.

link|flag

Why do you even think you need to hide the fundamental part of your job as a programmer, ie thinking, from co-workers?

If a co-worker says "Wow! You really don't seem to do much each day, do you?", just explain, in a good natured way, what you are actually doing when they notice you staring off into the distance.

If they continue to have a problem, WHO CARES! Just let the responsible manager deal with it. Of course this relies on the fact that your output is actually acceptable.

This is exactly how I have dealt with this sitaution myself in the past. If someone just doesn't like you, you are not change their views, but most people are simply curious.

A programmer that believes hiding and being deceptive to colleagues about what it they do, will always have difficulty dealing with non-programmers.

link|flag

Have your eyes closed and when someone walks up to you, say "Amen".

link|flag
2  
and put a crucifix on your wall and get a prayer mat too... or whatever your religion dictates. A sure fire way never to be pestered by anyone ever again. Especially if your religion is of a non-mainstream nature :P – BenAlabaster May 1 '09 at 14:05
4  
May the sauce of the FSM lead you to enlightenment. – nilamo Aug 27 '09 at 5:32
show 1 more comment

I recall the movie The Firm when Gene Hackman tells Tom Cruise that if he is even thinking about a client, it's billable time. If it's good enough for a lawyer...

link|flag
2  
My employer couldn't pay me enough if we would handle it the same way. Programmers are constantly trying to learn and improve themselves. I've ideas in the shower before work, but can't put shower time on the time sheet. – stesch Oct 31 '09 at 13:18
show 1 more comment

Do you really have to do this (I mean explanations)? I know it's tough times, etc. but think of finding some place else when you can.

According to your question several things are wrong with your place of work:

  1. Other people (including peers) should have implicit respect to what you do;
  2. Other people should not be able to observe you all the time;
  3. You being concerned with what other people think of you is counter-productive;
  4. "Flow" is a critical process for software development and you are clearly in environment that doesn't embrace this process.

Pick up Peopleware by DeMarco and Lister to read about these and other things that define and explain right teams and places to work.

link|flag

I worked for a couple years for a small company as the sole developer. I know for a fact that they thought I was just slacking off when they'd pass by my door and see me on the internet looking something up, or reading a book like Pragmatic Programmer; and even when I was actually writing diagrams related to the current workload.

In short, I don't think it is possible for someone unfamiliar with programming practices to understand what we do. Anymore than it is possible for someone unfamiliar with engines to see a mechanic just standing there staring under the hood, and understand that the guy is actually working.

link|flag
8  
Yes, I had negative comments in a job once because coworkers saw me sitting around reading "magazines". Like Dr. Dobbs, Programmer's Journal, Proceedings of the ACM. – Cyberherbalist May 1 '09 at 18:51
2  
@Cyberherbalist: The kind of coworkers who can't learn anything by themselves? – stesch May 2 '09 at 0:30
3  
About reading Pragmatic Programmer at work: it's like an automechanic reading "Basic car care" at work instead of fixing cars. – simon Oct 7 '09 at 17:17
1  
The OP didn't mention reading Pragmatic Programmer, so your comment is a straw man argument. A more appropriate analogy is that of an automechanic reading a reference manual about car engines at work when stumped, and I think it's a very good thing. – Seun Osewa 22 hours ago
show 2 more comments

What I hate is when people see you browsing the internet and think you're slacking off. A key part of expectations or perception management is to not have your monitor facing a busy thoroughfare. I hate people walking behind me as a general rule anyway.

link|flag
show 1 more comment

Program in Java. Then you'll have to do a lot more typing. Or, if you're already programming in Java, well, hmmm...COBOL, maybe?

link|flag
show 2 more comments

Get a whiteboard and scribble some ideas on it while you're thinking. I find it helps me structure my ideas better than just walking around while thinking.

link|flag
1  
I don't have a whiteboard in my cubicle at this client, but I've been pestering them for one for ages. Having a whiteboard is great for diagramming random ideas - a great thought processor. – BenAlabaster May 1 '09 at 13:38
2  
Absolutely. The same managers who demand you work also get very excited when they see people writing things on whiteboards viewing it as a sign of creativity, planning and communication. I once had a manager who when we had clients in demanded that the team sit around a whiteboard pretending to discuss stuff so the environment would seem dynamic... Terrifying but true. Still, in her defence she was generally a pretty good manager other than that. – Jon Hopkins May 1 '09 at 13:45
show 2 more comments

I always come running back from the toilet, and say I got a great idea while I was thinking there.

The worst part: it is even true, I just make a show out of it. Do the same with coffee break, just break of mid-phrase if you get an idea etc.

link|flag

I've seen this one. We have an internal IM system where I work. Last year, word came to us (from our boss, as a friendly warning) that people in other departments were looking at the IM client to see our status (if we were "away" or not) and using that to determine that we were slacking off.

So we set our away timeouts to be very high, so it looks like we're always busy, which we usually are.

Between watching programs run that may take time, reading stuff on screen, talking to other programmers next to you, etc... it can look that way.

I don't worry about, really. That's how I work (and the others around me too). That just the way things work in our department. That metric can work with some jobs (i.e. % of time in a call for a call center employee), but it doesn't work for everyone.

What can you do about it? I wouldn't do much. I like the crossword suggestion above. If you are worried about others opinions, I wouldn't be. If they are complaining/making remarks, you can try being nice (the crossword thing) but if it continues you could simply talk to you manager asking for people to basically be informed "he's doing his job, let him be". It's not the job of the clerk 3 desks over to monitor how hard you're working.

The people who watch this kind of thing are usually the same people who are slacking themselves (or at least worried they are, such as a workaholic).

As long as your boss knows you're doing a good job and doesn't think you're wasting the company's money, you should be fine.

link|flag

Write your thoughts on a paper, so they see that you are working !

link|flag

Usually what I do is I'll leave something on my computer screen (query in progress, report, data table, etc.) and i'll play with my rubik's cube while periodaclly looking at the screen.

link|flag
4  
yes...gotta have screen full of windows with things so complex it always looks like your working. Until they notice web browser and you're typing a comment on SO...ohhh shit. – dotjoe May 1 '09 at 15:26
show 1 more comment

Sometimes when I have a difficult problem, like everything I've tried just isn't working, I lean back in my chair and close my eyes. Guess what that looks like.

link|flag
3  
Some times I do that and wake up slightly alarmed when a co-worker wakes me up with some random question. Closing your eyes is dangerous business. – kch May 1 '09 at 15:01
1  
The last job I worked at, I had the same thing - I was leaned back, eyes closed, thinking. Boss walks into my cube, quietly. She says my name and I jump a little - wasn't expecting the interruption. Later that day, my supervisor calls me over and tells me the boss is going to have her "eyes on me" because of the situation. And she was a programmer in the past. Apparently, she's able to think just fine while coding - different strokes for different folks, I guess. – Knobloch May 1 '09 at 18:30
show 1 more comment

I think it's important to distinguish between accepted forms of "thinking" and less conventional methods like some of the ones above (playing FPS games etc). Most workplaces expect some degree of professionalism, and while playing games might be the most effective method for you, society doesn't generally accept that as professional behaviour (yet ;) ).

Usually I just leave the CLI for a linux server open on one screen - most people see that and run.

link|flag
2  
If you can't do even that, take a few screenshots of the command line on your home computer and bring them in to work. :) – Barry Brown May 2 '09 at 1:08
show 2 more comments

If they don't affect your getting a paycheck, what does it matter what they think?

link|flag
9  
If enough people think you are slacking off, eventually management would be forced to do something, which could affect you paycheck. – Rob Z May 1 '09 at 17:14
4  
Even if management knows you're not slacking, it's often easier for them to force you to change your behavior than force everyone else to mind their own business. – patros May 28 '09 at 16:04
show 4 more comments

If you're the only programmer in your division, it's a pity. Otherwise you could write mini-documents like a model overview - anything high-level - to help you with your implementation thougts.

  • revise your own thoughts
  • provide a written document as basis for possible "stand-up" reviews with fellow developers
  • aggregate artefacts for later detailed documentation purposes (which mostly are required in not-so agile companies)
link|flag

You can run your test cases in selenium IDE. x 100

link|flag

My strategies:

  • Bring up some gnarly code in Emacs, and stare in the general direction of the screen.
  • Take a walk.

The problem with the second approach is that if a thought strikes that requires looking something up, you are often a minute or more away from your workstation. Also, I often bump into someone on the walk I need to talk to. That isn't nessecarily a bad thing, but it doesn't help with the task at hand. Still, you end up learning something that you needed to learn. Consider it a holistic development technique. :-)

link|flag
show 2 more comments
Google