Sunday, June 12, 2016

WLC - Write Less Code

Given two solutions to a problem, the one with less code is usually better. 

Plite of the Programmer


Our job as programmers is to deliver value to customers. We deliver value by identifying needs, translating them into requirements, and writing code to meet those requirements. The difficulty of the task is compounded by the fact that requirements are constantly, unpredictably changing. Even after we finally ship, the story isn’t over. Code is a living thing that must be maintained, usually well beyond anyone's expectations.

Over the lifetime of a product, we will edit code 10x more than we write code, and we will read code 10x more than we edit it. Each token we add to our code may be read over 100 times. Every token we save could save up to 100 token-thoughts and 10 future edits. The key to our productivity is writing less code.

Why Count Tokens?


Tokens are the smallest meaningful element in code. Tokens are a more objective measure of code size since lines can have any number of tokens. Measuring tokens also ignores white-space and comments. Last, measuring tokens doesn't penalize using clear names. ‘Occupations’ is just as good a name under the token metrics as ‘occus’ or other hard to understand abbreviations.

Don't abbreviate words. Saving keystrokes only saves writing the code. It doesn't save edits or reading. Abriviations make reading harder. Since reading code dominates our time, abbreviations are a losing proposition. Only use shortened words if the shortened version is used at least as commonly in speech.

Measuring tokens is a simple, effective metric that lets us make decisions quickly and get on with solving problems and delivering value.

Why ‘Write Less Code’?


It's a simple concept with depth. Amateurs and masters both can apply and learn from it. A more accurate metric might be refactorability. We spend most of our time refactoring code, not writing it. Refactorability is the code-quality metric. It can be broken down into code size, clarity, and structure. Of the three, size is the only one that can be objectively measured, and while clarity and structure are important, both are usually improved by reducing code size. Refactorability is not something a novice will understand. ‘Write less code’ is an excellent guiding principle for all programmers.

How to Write Less Code


All other good coding practices essentially reduce code size. Two of the most important, and most often violated coding practices for reducing code-size are DRY and ZEN.

DRY


Don't. Repeat. Yourself. Don't you dare repeat yourself ;). The most problematic artifacts I've seen, both from novice and expert programmers, is code repetition. The big problem with repetition is it compounds the complexity of refactoring. Not only do we have to fix two or more things instead of one, but we have to understand how they all interact. Plus it bloats the code-base making it hard to understand.

DRY is a subtle art. The first, obvious level is ‘don't copy-paste your code,’ but as we level-up, we start to realize anything gzip might compress potentially violates DRY. The ultimate measure is, as always, refactorability. How many code-points need to change for a common refactor? Is there a way to reduce repetition to reduce the complexity of refactorability?

ZEN


Build it with Zero Extra Nuts (more commonly known as YAGNI). Because it is impossible to
predict future requirements, adding anything to our code that isn't strictly necessary to meet current requirements is not only a waste of time now, but it will haunt us for at least 10x future edits and 100x future reads.

It is fun to add cool features, but the master knows the only thing that matters is delivering value to the customer.

Write Less Code - Formalized


My hypothesis, and my experience, comes down to this:

  • Given two different functions, modules or programs
  • that both meet or exceed the problem's requirements, both correctness and performance
  • the one with less tokens is always better
  • as long as it doesn't sacrifice clarity.

The Practice of Writing Less Code


As with everything in life, writing less code is a practice. There is no point where we will master it. There is always another layer of deeper understanding. We are always learning how to make our code DRYer and more ZEN. We must constantly be looking for ways to meet requirements with less code.

25 comments:

  1. After reading your more recent Jan 28th 2017 "Your SOUP is Leaking - Crafting Acronyms" article along with your thoughtful ZEN article and that principle's relationship to frameworks, I thought there might be a good alternative acronym to WLC that relates the two principles: ULC - Use Less Code.

    Through both thoughtful choices when sourcing 3rd party frameworks or modules and consideration in how to assemble or integrate them with your own code efforts, "Use" could include but also expand beyond just the code you create in a project.

    While it is actually more pronounceable, I'm not sure a 'hulk' without the H sounds all that elegant. :)

    ReplyDelete
    Replies
    1. I love that idea! WLC certainly isn't an elegant acronym by any stretch. "Hulk" means strength, not a terrible association with "ULC." I'll try it out.

      I do like that "use" is more general than "write".

      Delete
  2. This was such a thorough and insightful read. i am glad i got to read it. you have a way with words and on top of that you are quite knowledgable. keep posting more

    ReplyDelete
  3. Write My Essay Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.

    ReplyDelete
  4. It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. write my essay for me

    ReplyDelete
  5. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. smart essay writing service

    ReplyDelete
  6. I’m happy I located this blog! From time to time, students want to cognitive the keys of productive literary essays composing. Your first-class knowledge about this good post can become a proper basis for such people. nice one do my essay

    ReplyDelete
  7. I read this article, it is really informative one. Your way of writing and making things clear is very impressive. Thank you for sharing such an informative article. thesis proofreading

    ReplyDelete
  8. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. Mua Tài khoản Grammarly Premium

    ReplyDelete
  9. Hello everyone, We are Children's Furniture – Bao An Kids. Surely when you come to this page, you are looking for a professional children's interior design and construction unit to help you advise and choose for your family and baby to have a perfect and comfortable living space. the safest. Thank you very much for trusting and wanting to accompany Bao An Kids. nội thất trẻ em

    ReplyDelete

  10. Bài viết rất hay! Quà tặng 24k chuyên cung cấp:

    Đặt cúp đồng giá rẻ tại Hà Nội

    Quà tặng vinh danh ở đâu chất lượng và uy tín

    Hotline: 0866 866 711

    ReplyDelete
  11. Tan Binh valve has many years of experience in the field of industrial valves. Production meets DIN, BS, JIS standards… Tan Binh After more than 10 years of supplying materials for the water industry Our company is now a reliable partner for most companies providing water treatment systems in the local area. Hanoi as well as nationwide. van công nghiệp, Van cửa, van điện từ

    ReplyDelete

  12. Bài viết bạn rất hay! Công ty cổ phần nội thất KFA hoạt động trong lĩnh vực thiết kế kiến trúc, nội thất. gợi ý cho bạn:

    Bí quyết để có một không gian nội thất đẹp

    Khám phá chi phí thi công nội thất trọn gói bao nhiêu?

    Liên hệ để được tư vấn: ĐT: 0987.316.777

    ReplyDelete
  13. Today, the demand for modern sanitary products and equipment with preeminent features is increasing day by day. That's why many sanitary ware companies were born. To meet the essential needs of our customers. So what are the advantages of today's sanitary ware products? Let's find out detailed information with the Great World of Lights Showroom through the article below. More: https://bitly.com.vn/hfn52q

    ReplyDelete
  14. Nhân Sâm Trường Thọ chuyên cung cấp các sản phẩm chính hãng chất lượng:

    Trà hồng sâm Hàn Quốc

    Hồng sâm Hàn Quốc 6 năm tuổi

    Sản phẩm nhân sâm nhập khẩu trực tiếp từ Hàn Quốc có chất lượng đảm bảo, bao bì đóng gói đẹp mắt, sang trọng, vô cùng thích hợp để làm quà biếu, quà tặng.

    Địa chỉ: 853 - 855 Nguyễn Kiệm, P.3, Q.Gò Vấp, TP.HCM.

    Tel: +84 (28) 350.66666

    Hotline: 090.77.999.88

    ReplyDelete

  15. Công ty Cổ Phần Công Nghệ và Giải Pháp EMS cung cấp :

    Điều hòa chính xác

    hệ thống nguồn một chiều

    Điều hoà chính xác Liebert PEX

    Liên Lạc theo SĐT: +84 24 7306 6226; +84 24 6687 4507

    ReplyDelete
  16. Nội thất Kfa đã có trên 10 năm kinh nghiệm trong thiết kế và thi công nội thất, được hàng nghìn khách hàng tin tưởng - đem đến sự hài lòng cho cả những khách hàng khó tính nhất
    Phong cách tropical trong thiết kế nội thất
    Phong cách tropical là gì? Xem chi tiết ngay
    Vách ngăn phòng khách là gì
    Có Nên Sử Dụng Thảm Phòng Khách Hay Không

    ReplyDelete

  17. Công ty Cổ Phần Công Nghệ và Giải Pháp EMS cung cấp :

    Điều hòa chính xác

    hệ thống nguồn một chiều

    Điều hoà chính xác Liebert PEX

    Liên Lạc theo SĐT: +84 24 7306 6226; +84 24 6687 4507

    ReplyDelete
  18. Máy tính Hải long là địa chỉ bán máy tính cũ uy tín chất lượng, có đội ngũ tư vấn chuyên sâu, am hiểu tường tận về từng chi tiết của máy tính. Đặc biệt, thời gian bảo hành cũng như chính sách đổi trả, không kém việc bạn mua máy mới 100% tại các siêu thị lớn.
    Địa chỉ: Số 9 ngách 109 ngõ 69a Hoàng Văn Thái, Thanh Xuân, Hà Nội
    Điện thoại: 0972 105 943

    ReplyDelete
  19. Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng


    Giảo cổ lam giá rẻ tại Hà Nội

    Bao nhiêu tiền 1 kg giảo cổ lam

    ReplyDelete
  20. Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng

    Lều xông hơi mini giá rẻ tại Hà Nội

    Lều xông hơi gia đình chất lượng

    ReplyDelete
  21. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. nên mua điện thoại nào dưới 10 triệu

    ReplyDelete

  22. Công ty Tất Phú là nhà phân phối và trực tiếp nhập khẩu tấm xi măng CEMBOARD từ các hãng sản xuất uy tín, chất lượng từ nhiều nước khác nhau. Nắm trong tay gần 10 năm kinh nghiệm trong lĩnh vực cung cấp các dòng xi măng CEMBOARD chất lượng.

    Báo giá tấm cemboard

    Cung cấp tấm cemboard thái lan

    Địa chỉ: Tầng 3, tòa nhà Shodex, số 245 Tam Trinh, Hoàng Mai, Hà Nội

    Điện thoại: 024 66604646 – 0977 688 695

    ReplyDelete
  23. Xe nâng Đông Đô là địa chỉ chuyên cho thuê:
    - "Xe nâng komatsu 5 tấn".
    - "Xe nâng Mitsubishi".
    - "Xe nâng TCM cũ".
    - "Xe nâng Toyota cũ".
    Uy tín, giá rẻ nhất trên thị trường. Gọi ngay theo Hotline: 091.351.9810- 0912.018.299 để được hỗ trợ chi tiến nhất.

    ReplyDelete