每個(gè)程序員都應(yīng)該知道哪些最基本的常識(shí)?
這個(gè)問(wèn)題只有64個(gè)答案,并沒(méi)有很多人愿意給自己的答案。排名第一的答案7.9k點(diǎn)贊(up-vote),來(lái)自一位有故事的光頭大哥(文章附錄有分享他的故事),2017年寫(xiě)的;排名第二有1.6k點(diǎn)贊,來(lái)自新加坡公司PropertyGuru的高級(jí)軟件工程師,也是2017年的答案。其他的答案點(diǎn)贊的很少。所以我們就看看前兩名的答案吧。
光頭大哥的答案光頭大哥的答案共25句話(huà),附上原文、譯文和一些評(píng)論。1.?If it's not tested, it doesn't work.如果沒(méi)被測(cè)試過(guò),那它就不能正確運(yùn)行。評(píng):測(cè)試自己的代碼是一名程序員的基本良心。
2.?Source control is your friend - make sure you use it.代碼版本控制是你的朋友——請(qǐng)確保你使用它。
3.?Just because you wrote it doesn't mean you own it — don't beoffended if someone else on your team has to change your code.僅僅因?yàn)槟銓?xiě)了代碼不代表你就擁有它——如果你團(tuán)隊(duì)里的其他人必須更改你的代碼,請(qǐng)不要生氣,感覺(jué)自己被冒犯了。
4.?Don't reinvent the wheel, library code is there to help.不要重復(fù)制造輪子,庫(kù)代碼就是為了這個(gè)存在的。
5.?The fastest code is code that's never executed — look for early outs.最快的代碼是從未被執(zhí)行的代碼——查找早期的輸出。
6.?Just because you didn't write it doesn't mean it's crap.你沒(méi)寫(xiě)過(guò)這樣的代碼不代表這是垃圾。
7.?Source code is just a hint to the compiler about what you want to do, it won't necessarily do it (e.g. You might declare a function as inline but the compiler doesn't have to obey).源代碼只是提示編譯器你想做什么,但它未必會(huì)去做。例如,你可能聲明了一個(gè)內(nèi)聯(lián)函數(shù),但編譯器未必會(huì)服從。
8.?Code that's hard to understand is hard to maintain.難以理解的代碼也難以維護(hù)。
9.?Code that's hard to maintain is next to useless.難以維護(hù)的代碼近乎沒(méi)用的代碼。
10.?“Whilst I'm editing this file I’ll just…” is a great way to introduce feature creep and bugs.“下次我編輯這個(gè)文件的時(shí)候,我就會(huì)……”,是一個(gè)引進(jìn)功能退化和bug的好方式。
11.?The neater your code layout, the easier it is to read. The easier it is to read, the easier it is to understand and maintain.代碼布局越整潔,就越容易閱讀。越容易閱讀,就越容易理解和維護(hù)。
12.?Code is not self documenting. Help others by adding comments to guide them. You may understand it now but what about in 5 years time?代碼無(wú)法自描述。請(qǐng)?zhí)砑幼⑨屓椭鷦e人理解。再說(shuō),你可能現(xiàn)在還能理解,但五年之后呢?
13.?Bad Code can and will come back to haunt you.糟糕的代碼會(huì)回來(lái)困擾你的。評(píng):珍惜生命,別留下糟糕的代碼。
14.?There is no such thing as a 5 minute job. It'll always take at least half a day.哪有5分鐘的工作???這至少要花半天的時(shí)間!評(píng):人間真實(shí)。
15.?Magic numbers are bad.使用"神秘?cái)?shù)字"不好。
16.?Constants don't take up storage, they're compile time text substitutions.常量不占用存儲(chǔ)空間,它們會(huì)在編譯時(shí)文本替換。
17.????Project management will always want you to do twice as much in half the time.項(xiàng)目管理總是希望你用一半的時(shí)間做兩倍的事情。
18.?If there is a bug, the user will find it.如果有bug,用戶(hù)會(huì)發(fā)現(xiàn)的。
19.?A code review is not a criticism.代碼審核不是批判會(huì)。
20.?It's not the quantity of code that matters, it's the quality. Any idiot can bang out 40kloc but that doesn't make it fit for purpose.代碼數(shù)量無(wú)關(guān)緊要,重要的是代碼的質(zhì)量。任何一個(gè)白癡都能敲出4萬(wàn)行代碼,但這并不意味能適合于目標(biāo)。
21.?The true cost of poorly written code is in the maintenance.寫(xiě)得不好的代碼的真正成本在于維護(hù)。
22.?Eat your own dog food — fixing bugs in your own code helps you code better and improves your understanding.使用你寫(xiě)的dog food——修復(fù)你自己的代碼能幫助你更好地寫(xiě)代碼和提高你的理解。
23.?Code rots over time.代碼會(huì)隨著時(shí)間腐爛。
24.?If the user didn't ask for a feature, don't add it.如果用戶(hù)沒(méi)要求某個(gè)功能,那就不要添加它。
25.?If it's not tested, it doesn't work (yes, I know I've included that twice but it's really important).如果沒(méi)被測(cè)試過(guò),那它就不能正確運(yùn)行。(是的,我知道我說(shuō)了兩遍,但它確實(shí)重要。)
光頭大哥作為老派程序員,雖然轉(zhuǎn)行了,但觀(guān)點(diǎn)還是挺接地氣的。我們接著看第二個(gè)答案,感覺(jué)來(lái)自一位從業(yè)3-5年的程序員。
1.?Your code will never work the way you want, the very first time you code it up. Accept that. Move on.剛完成代碼的時(shí)候,你的代碼永遠(yuǎn)不會(huì)按你想要的方式工作。請(qǐng)接受這個(gè)情況,并繼續(xù)請(qǐng)進(jìn)。評(píng):所以,寫(xiě)完代碼必須自己先測(cè)試。
2.Frustration. That’s your best buddy from now on. Get to know each other well.挫折,從現(xiàn)在開(kāi)始是你最好的朋友。請(qǐng)相互好好認(rèn)識(shí)了解。
3.?If patience is not your virtue, well ... better make it one! You are going to need it a lot.如果耐心不是你的美德,好吧……你還是最好擁有它。你會(huì)非常需要它的。
4.?Debugging. Learn it soon. Coz you shall be spending more time debugging than coding. If only there is something that converts our designs into bug-free code…調(diào)試。你要快些學(xué)習(xí)它。因?yàn)槟銓⒒ㄙM(fèi)更多時(shí)間在調(diào)試而不是編碼。要是有什么東西能把我們的設(shè)計(jì)轉(zhuǎn)換成無(wú)bug(bug-free)的代碼就好了……
5.?Stay out of the battles like Tabs vs Spaces, Vim vs Emacs, Windows vs Linux, C vs Java vs Python, Atom vs Eclipse vs Sublime vs … X!遠(yuǎn)離技術(shù)選擇優(yōu)劣對(duì)比的戰(zhàn)爭(zhēng),比如Tabs vs Spaces, Vim vs Emacs, Windows vs Linux, C vs Java vs Python, Atom vs Eclipse vs Sublime vs,等等。評(píng):沒(méi)有最厲害的技術(shù)或工具,只有自己當(dāng)下最合適的技術(shù)和工具。
6.?People are going to look upto you to help them code. And look down at you to fix their PC.人們會(huì)指望你來(lái)幫助他們編碼;然后,他們低下頭看你在修他們的電腦。
7.?Do not bother with premature optimization. If people can code in languages like Python, a few microseconds shaved off, is not that big a deal.不要為不成熟的優(yōu)化而煩惱。如果人們會(huì)用Python之類(lèi)的語(yǔ)言去開(kāi)發(fā),也就省下幾毫秒。這沒(méi)什么大不了的。
讀完之后,你認(rèn)同這些觀(guān)點(diǎn)嗎?整體來(lái)說(shuō),我覺(jué)得他們的觀(guān)點(diǎn)都挺好的,接地氣。從質(zhì)量和范圍看,光頭大哥的略勝一籌。
我的答案是什么?過(guò)去寫(xiě)過(guò)《平凡卻又深刻的編程理念》系列的文章,算是當(dāng)初我能給出的模糊答案,重新描述的話(huà),即
- 先運(yùn)行起來(lái)再改善(Make it Work then Make it Better)。
- 程序就是現(xiàn)實(shí)世界的數(shù)字世界的抽象。
- 一切事物都有其生命周期。
- 迭代即速度和質(zhì)量。
過(guò)去讀張一鳴的一篇文章——《越是高級(jí)人才,越要看一些基本素質(zhì)》,我增加了另一條關(guān)于程序員成長(zhǎng)的觀(guān)點(diǎn):技術(shù)知識(shí)決定了你的起點(diǎn)和速度,而技術(shù)修養(yǎng)決定了你的加速度!第一,你的職業(yè)起點(diǎn)和發(fā)展速度,極大地取決于你的技術(shù)知識(shí)。而且,越難或者需要越久才能理解的技術(shù)知識(shí),越是有價(jià)值。如果你擁有的技術(shù)知識(shí),別人能看一篇文章或者花幾個(gè)小時(shí)甚至幾天就能掌握,那么這樣的技術(shù)知識(shí)沒(méi)什么競(jìng)爭(zhēng)力的。第二,你的持續(xù)發(fā)展能力更多取決于你的技術(shù)修養(yǎng)。如果技術(shù)修養(yǎng)較差,那也是會(huì)減緩甚至停滯了你的發(fā)展。特此共勉!
最后,你的答案會(huì)是什么?歡迎在留言區(qū)留下你的答案。
附錄:光頭大哥的故事
光頭大哥叫Gavin Thornd,從發(fā)量看,想必是一名超級(jí)資深的程序員!然而,點(diǎn)擊他的簡(jiǎn)介頁(yè)面,發(fā)現(xiàn)他是一位攝影師!而且沒(méi)有一個(gè)關(guān)鍵詞表示他和程序員有什么關(guān)系??!同時(shí),從自我介紹上看出來(lái)這大哥還有點(diǎn)小幽默。
我想,他一定是一個(gè)有故事的人!于是我簡(jiǎn)單搜索一下他。在bing.com搜索他的名字,第一個(gè)結(jié)果是他的個(gè)人網(wǎng)站 https://gavinthorn.com/。然后,在網(wǎng)站介紹了發(fā)現(xiàn)光頭大哥的故事——一個(gè)技術(shù)人跨界轉(zhuǎn)型的勵(lì)志故事。
簡(jiǎn)單地翻譯介紹他的經(jīng)歷:
他自小熱愛(ài)攝像。在18歲生日收到一臺(tái)單反相機(jī)后更是激發(fā)了他的創(chuàng)作熱情,之后投入大量時(shí)間和金錢(qián)搞攝影。然而,在面臨職業(yè)選擇的時(shí)候,他還是聽(tīng)從周?chē)说囊庖?jiàn),選擇了一個(gè)“好”工作,最后成為了一位通信系統(tǒng)工程師。然而,工作多年之后,他覺(jué)得自己還是更愛(ài)攝像,而且當(dāng)時(shí)的工作已經(jīng)讓他心力交瘁了——身體快被壓力弄垮了,沒(méi)時(shí)間陪伴家人也讓他很苦惱。于是他辭職了,弄攝影了,每天可以多陪伴家人,身體也恢復(fù)健康了,慢慢走向人生巔峰。嗯,看來(lái)過(guò)去的程序員日子也不好過(guò)啊。而且,從這位大哥的經(jīng)歷看,人還是得有點(diǎn)興趣愛(ài)好的。萬(wàn)一哪天不想在現(xiàn)在的職業(yè)發(fā)展了,還能有一條后路。





