Less code or less operation [closed]

Sometimes I hesitate between “More code to avoid unnecessary operations” and “less code but with redundant operations”. Let me just take an example (Win32 API):

I try to paint some controls manually when the cursor is over it. If the cursor is over this control and it is painted, then it doesn’t need to be painted again before the cursor goes out from it.

Of course, I can just paint this control whenever a WM_MOUSEMOVE appears. Or, I can use more variables to record the state of this control and paint it only when the state is changed.

Although it is not a question (in practice) in my example, I still want to have a feeling for which is the better choice, in general. And if this depends on cases, any way to develop a good sense to do the choice?

(I am not focus on “efficient”. It is more or less like a question as: If you are an idealist of programming, which one is better in your opinion.)

1

There is no better choice in general. Code readability and system performance are two valid goals, and if they are at odds, you must know about the strengths of those two forces in your particular situation before deciding. To say, “when readability and performance are at odds, always choose performance (or readibility)” would be a terrible oversimplification that leads to terrible decisions when followed blindly.

To make good decision, you would consider these forces in the context of your actual requirements. For example:

  • UI behaviour, particularly something to do with painting the mouse pointer, is rather sensitive to speed. It may not affect the actual throughput of an application much or at all, but the impression of speed is vital for a good user experience: milliseconds of feedback delay can contribute to subconscious dislike of a system. (Consult user experience professionals for more precise figures, but they are shockingly small.) So in this case, performance may actually be comparatively important rather than a “nice to have”.
  • At the same time, graphics rendering code is a rather fundamental part of a large system. You definitely want it to be correct and rock-solid – mis-drawing something in your user interface can leave an even worse impression than feedback lag. This puts a premium on maintainability, and that means readability is also important.
  • But rendering sprites, icons or whatever your low-level primitives are is (I hope) a relatively small, contained problem. Once you’ve implemented it, it may not require continuous improvement the way that business rules or interfaces do (how often does a new input modality come long that challenges the established keyboards and mice?), so maybe you can get away with optimizing the hell out off your code and then putting a lid on it. That would mean more opportunity to optimize and less danger of having to unravel your clever tricks again the next time you need to change low-level behavior.

This is not to say that you should optimize for speed, either in this case or in general, just because I named two pro-speed items and one pro-maintenance item. These are just examples of the kind of questions you need to ask to make the right decision depending on the circumstances. Also, you need to quantify the effects you are comparing, e.g. via usability testing and profiling metrics. One big minus can be enough to cancel out the advantages of a whole bunch of small plusses.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật