вторник, 27 сентября 2011 г.

How to design a good API and why it matters (collection of maxims)

  • Good programs are modular, and intermodular boundaries define APIs.
  • Good APIs are easy to use and hard to misuse.
  • Every API is a little language. The readable code is an indication of the good API.
  • The fundamental rule of API design is that when you in doubt about something, leave it out. You can always add things later, but you can't take them away.
  • Keep in mind the use-cases against your API while you design it.
  • Show your design to as many people as you can, and take their feedback seriously.
  • Documentation matters. No matter how good an API, it won't get used without good documentation. Document every exported API element: every class, method, field, and parameter.
  • Subclass only if you can say with a straight face that every instance of the subclass is an instance of the superclass. Exposed classes should never subclass just to reuse implementation code.
  • Design and document for inheritance or else prohibit it. This documentation takes the form of self-use patterns: how methods in a class use one another. Without it, safe subclassing is impossible.
  • Immutable objects are simple, thread-safe, and freely sharable.
  • Every method should do the least surprising thing it could, given its name. The same named methods should do the same things.
  • If something goes wrong, the API must inform about that. Compile-time is best.
  • Don't make the client do anything the API could do.

  • When designing an API, first gather requirements (with a healthy degree of skepticism). People often provide solutions; it's your job to ferret out the underlying problems and find the best solutions.

воскресенье, 25 сентября 2011 г.

Задача про монеты

Имеется 12 монет, среди которых ровно одна фальшивая (неизвестно какая). Все настоящие монеты одного веса, а фальшивая легче или тяжелее. На чашечных весах можно сравнивать по весу любые две группы монет. Нужно найти фальшивую монету и выяснить, легче она или тяжелее. Сколько взвешиваний для этого достаточно?

Придумал алгоритм для решение этой задачи для произвольного числа монет. Получились следующие цифры:

  • 3 монеты → 2 взвешивания
  • 4 монеты → 3 взвешивания
  • 13 монет → 4 взвешивания
  • 40 → 5
  • 121 → 6

Напрашивается естественная закономерность:

число взвешиваний = Ceiling[Log[3, 2×число монет + 2]].


А в прошлый раз мне эта задача не поддалась.

суббота, 10 сентября 2011 г.

Окончание чётной страницы

Человек, которого я любил, ушёл. И невозможно исправить то, что случилось.