如何優雅地編程?
(中文翻譯: 如何優雅地編程?Thank you @喬傑克 for an awesome translation!)
I"ve been programming since I was 12, and every day, I"m learning as much as I can to improve my programming technique. Here"s some general guidelines I try to keep in mind.
Work with A Better Coder
The fastest way is to learn from a much better coder. Ask questions about design decisions and the libraries that she/he uses.Copy Good Code
Yes, when you see a piece of good code that does something better than how you could"ve designed it, use Ctrl-c and Ctrl-v. Copy it and modify it and learn why it"s better (usually because it"s simpler). Just remember to also give credit to the original author.Use Popular Libraries
Libraries will save you time because they prevent you from reinventing the wheel. Popular libraries are usually maintained by a community of experienced coders. Using the library will also likely enforce a better programming style. Just like how you would study vocabulary words to improve your language skills, learning new libraries improves your programming vocabulary. If you"re ambitious, you can even look in the source code and see how the logic is organized.Care About Style
In programming, form is function. For example, this is literally true in Python, where indentation indicates scope. But even for languages that are space agnostic, a clear style prevents errors. Good style also lends itself to documentation or, even better, is automatic documentation.If you read a piece of code that"s easy to understand, emulate its style and organization. And also read peoples" blogs about programming style.
Some basic style habits that I practice are,- Using long and descriptive function and variable names. Nowadays, your code will never burden a computer"s memory just because of the extra bytes in a long variable name. Plus, if you use a compiled language, the compiler will replace all the names anyways. If you"re lazy about typing long names, set up tab autocomplete.
- Using action verbs for my function names. Instead of "getCorrectedName()", I use "rename()". In this case, "rename" is a more precise verb than "get" and it simplifies the function name.
- Writing small functions that only do one task. If a function does more than one thing, I break it down into subfunctions that do only one thing each. This makes the code more modular and easier to debug. This also makes function names self-documenting.
Develop Iteratively
The most common reason that people give up on programming is that they don"t enjoy the results of their work.As you learn about more frameworks and libraries, it becomes tempting to write the whole project and then click run. It probably won"t work. It"s better to plan out the structure and then write tiny parts of the code. Test each part, experience the thrill of watching it work and then drop it into your overall project.
If you"re an experienced programmer, create classes and objects, even for your small scripts. Even if you think, "Oh this"ll be a script be for something small," if it works well, chances are you"ll want to expand it or use it for something else. Instead of duplicating code or writing complex if-else blocks, it feels amazing to just inherit from the original class, make some small changes and have a newly customized application.
Use StackOverflow
Almost every common task and bug is solved on StackOverflow. Plus, the solutions are elegant and the answers give great explanations.Write for Your Future Self
Imagine your future self either has amnesia or grows stupider (both usually feel true). It"s always a satisfying feeling to look over your old code and be greeted by user-friendly documentation.Use Version Control
Github hosts Git repositories (projects), Bitbucket hosts Git and Mercurial. If you aren"t comfortable with command line programming, then I recommend using SourceTree and Bitbucket.
Share Your CodeLet other people use it, give you suggestions and ask for improvements. You"re also more motivated to improve programs that other people are using.這裡是Brian回答的翻譯 (終於趕上一回了)。大部分都說的很對,我也有類似經驗;有些網站像Github大陸不一定能用。一些Brian提到的網站:www.github.com; http://www.bitbucket.org -- 版本控制;Bitbucket同時提供Git GUI客戶端SourceTreehttp://stackoverflow.com -- 關於代碼的問答網站,程序員版知乎
============= 譯文分割 ==============
我從12歲就開始編程而且每一天我都儘可能多學點以便提高我的編程水平。 下面是我牢記的一些原則。
- 和更好的程序員工作
從更好的程序員那裡學習是最快的方法。可以問他/她關於決策的問題,或者問他/她用什麼庫。
- 複製好代碼
是的,當你看到一條比你能設計的還要好的代碼的時候,Ctrl-c 然後 Ctrl-v。 複製然後修改它,學習它為什麼好(一般是因為它更簡潔)。而且記住聲明原作者。
- 使用流行的庫
庫會節省你很多時間,因為它們讓你不用重複發明輪子(重新寫已經存在的東西)。流行的庫通常都有一幫有經驗的程序員在維護。而且使用庫還可以幫你養成更好的代碼風格。學習新的庫會增長你的代碼「辭彙量」,就像你通過學習單詞來提高語言水平一樣。如果你有想法,你甚至可以打開源代碼看看裡面的邏輯是怎麼組織的。
- 注意風格
在程序里,型即功能。比如說在Python里這就很重要;在Python里縮進決定了作用域。但是即使是對不注意空格的語言,清晰的代碼風格依然可以阻止產生錯誤。好的風格還可以幫助你寫文檔,或者更好的是,它就是自動的文檔。
如果你看到條很易懂的代碼的話,模仿他的風格和結構。你也可以讀些關於程序風格的博客文章。
我經常實踐的一些基礎的風格如下:
* 使用長並且有描述性的函數名和變數名。現如今, 你的代碼絕不會因為長名字帶來的多餘的幾個位元組而給內存增加壓力。而且,如果你用的是編譯型語言,編譯器會把所有名字都替換掉。如果你覺得懶不想打那麼多字,把tab設置成自動補全。
* 用動詞命名函數名。比如我會用」rename()」而不是"getCorrectedName()」。在這個例子里,」rename」比"get」更具體,而且它讓這個函數名更簡單。
* 一個函數只做一個功能。如果一個函數包含很多功能,我會把它分解成小的函數,每個只做一件事。這樣代碼會更加模塊化而且debug也更簡單。而且函數名也更加自成文檔了。
- 迭代開發
人們放棄編程的最常見的原因就是他們無法享受自己工作的成果。當你學會更多架構和代碼庫的時候,一下寫出整個項目然後點「運行」會很吸引人。但是這一般不頂用。更好的方法應該是計劃好結構然後寫每一小部分。測試每一部分,感受成功的激動感,然後把這部分放進你整個的項目里。
如果你是個有經驗的程序員,你應該創建類和對象,即使你只有一個小腳本。 有時候即使你覺得「哦這只是很小一段代碼」,如果它運行正常,你很可能以後會擴展它或者把它用在別的地方。從原來的類繼承新類,做些小改動然後做出一個新的具體的應用,這種feel很爽;你該這樣而不是複製代碼或者重寫複雜的條件語句。
- 使用StackOverflow
基本上所有的普通功能和bug都在StackOverflow上被人解決了。那些解決方法都很優雅,並且有很棒的解釋。
- 給未來的自己寫代碼
想像未來的你得了失憶症或者變傻了(一般都會這樣的)。你會在看帶有友好文檔的舊的代碼的時候感到滿足的。
- 使用版本控制
Github上可以寄存Git repository,Bitbucket可以寄存Git或者Mercurial。如果你對命令行沒什麼信心,我推薦使用SourceTree和Bitbucket。
- 分享你的代碼
把代碼給別人用,讓他們給你建議,問他們有什麼可以改進的地方。如果別人用你的代碼你就會更有動力改進它們。
有一顆手藝人的匠心,對自己交出去的作品或者說產品負責嚴謹,不斷錘鍊自己的技藝。
用python寫詩。或者試試 BrainFuck 這個語言。
如此甚好!
1.看需求2.寫原型滿足80%的需求
3.把20%的需求添加進原型
如果加不進去 返回2君子遠庖廚,雅士不編程 ~~ 嘰咕 ~~~
別逗了。
Joel Spolsky 有一篇很有名的blog : The Joel Test: 12 Steps to Better Code裡面列了好的程序員的12條準則,如果符合這些準則,應該算是很優雅了吧。
The Joel Test
- Do you use source control?
- Can you make a build in one step?
- Do you make daily builds?
- Do you have a bug database?
- Do you fix bugs before writing new code?
- Do you have an up-to-date schedule?
- Do you have a spec?
- Do programmers have quiet working conditions?
- Do you use the best tools money can buy?
- Do you have testers?
- Do new candidates write code during their interview?
- Do you do hallway usability testing?
作為一個資深程序員,我認為所謂優雅的編程就是指結構合理,模式設計有預見性,能夠適應業務的變化而不需要大規模修改既有代碼結構的編程。這需要多年的經驗,吃過很多虧踩過很多坑才能做得到,新程序員無論你看多少設計模式和重構之類的書,也做不到這點,因為要做到這種水平,除了本身對於模式的了解,還要對所從事行業業務有較深刻的理解才行。
作為一個資深程序員,我認為所謂優雅的編程就是指改三遍代碼.不罵娘.敢把自己的電話號碼寫到注釋里.,最重要的是少寫代碼 .... 把自己的代碼刪掉的越多,那麼被瘋子追家裡的機會越少.
長篇大論都有了。說個個人感覺:編程優雅與否取決於你的知識儲備是否足夠。
《編寫可讀代碼的藝術》 我認為你是這個意思
如何優雅的回答此類問題呢!!!
假裝不是為了保持清醒而喝咖啡
使用 litcoffee 寫代碼喵/
前幾位說的跟話題有關?難道優雅的編程不是:
對著air(或者其他超級本),透亮乾淨的玻璃杯里,泡著菊,或者咖啡,或者茶,室溫恰好,然後,隨心情,放一支舒緩或者動感的音樂,手指在杯子和鍵盤間遊走,(沒滑鼠,關滑鼠什麼事?)嗯,就是這樣了。---另外提了一個問: 如何編優雅的程?推薦閱讀:
※你所見過最美的C語言代碼?
※近年NOIP(提高組)試題分析?
※為啥這句話輸入到谷歌翻譯,就會神奇的出現錯誤的結果?(見圖)不信大家試試?代碼錯誤嗎?
※所有的遞歸演算法空間複雜度都是O(n)嗎?? ?
TAG:編程 |