Oqish abstraktsiyalar qonuni
Qonunlar 0 06.08.2023 318

The Law of Leaky Abstractions on Joel on Software

Barcha noaniq abstraktsiyalar, ma'lum darajada, sizib chiqadi.

(Joel Spolsky)

Ushbu qonunda aytilishicha, murakkab tizimlar bilan ishlashni soddalashtirish uchun odatda hisoblashda qo'llaniladigan abstraktsiyalar ma'lum holatlarda asosiy tizimning elementlarini "oqish" qiladi, bu esa abstraktsiyaning kutilmagan tarzda harakat qilishiga olib keladi.

Masalan, faylni yuklash va uning mazmunini o'qish. Fayl tizimining API-lari quyi darajadagi yadro tizimlarining mavhumligi bo'lib, ular magnit plastinada (yoki SSD uchun flesh-xotirada) ma'lumotlarni o'zgartirish bilan bog'liq jismoniy jarayonlarning abstraktsiyasidir. Ko'pgina hollarda, faylni ikkilik ma'lumotlar oqimi kabi ko'rib chiqishning mavhumligi ishlaydi. Biroq, magnit disk uchun ma'lumotlarni ketma-ket o'qish tasodifiy kirishga qaraganda sezilarli darajada tezroq bo'ladi (sahifadagi nosozliklar ko'payganligi sababli), lekin SSD diskida bu qo'shimcha yuk bo'lmaydi. Ushbu ishni hal qilish uchun asosiy tafsilotlarni tushunish kerak bo'ladi (masalan, ma'lumotlar bazasi indeks fayllari tasodifiy kirishning qo'shimcha xarajatlarini kamaytirish uchun tuzilgan), ishlab chiquvchi bilishi kerak bo'lgan abstraktsiyaning "oqish" amalga oshirish tafsilotlari.

Yuqoridagi misol ko'proq abstraktsiyalar kiritilganda yanada murakkablashishi mumkin. Linux operatsion tizimi fayllarga tarmoq orqali kirish imkonini beradi, lekin mahalliy sifatida "oddiy" fayllar sifatida taqdim etiladi. Agar tarmoqdagi nosozliklar bo'lsa, bu abstraktsiya "oqib ketadi". Agar ishlab chiquvchi ushbu fayllarni "oddiy" fayllar deb hisoblasa, ular tarmoqning kechikishi va nosozliklar bo'lishi mumkinligini hisobga olmagan bo'lsa, echimlar xato bo'ladi.

Qonunni tavsiflovchi maqolada aytilishicha, abstraktsiyalarga haddan tashqari ishonish, asosiy jarayonlarni yomon tushunish bilan birgalikda, ba'zi hollarda muammoni hal qilishni yanada murakkablashtiradi.

Shuningdek qarang: Hyrum qonuni

Haqiqiy dunyo misollari:

  • Photoshop-ni sekin ishga tushirish - o'tmishda duch kelgan muammo. Photoshop sekin ishga tushadi, ba'zan bir necha daqiqa vaqt oladi. Muammo shundaki, u ishga tushirilganda joriy standart printer haqida ba'zi ma'lumotlarni o'qiydi. Biroq, agar bu printer aslida tarmoq printeri bo'lsa, bu juda uzoq vaqt talab qilishi mumkin. Mahalliy printerga o'xshash tizimga taqdim etilgan tarmoq printerining mavhumligi ulanishning yomon holatlarida foydalanuvchilar uchun muammo tug'dirdi.
The Law of Leaky Abstractions
Qonunlar 0 06.08.2023 318

The Law of Leaky Abstractions on Joel on Software

All non-trivial abstractions, to some degree, are leaky.

(Joel Spolsky)

This law states that abstractions, which are generally used in computing to simplify working with complicated systems, will in certain situations 'leak' elements of the underlying system, this making the abstraction behave in an unexpected way.

An example might be loading a file and reading its contents. The file system APIs are an abstraction of the lower level kernel systems, which are themselves an abstraction over the physical processes relating to changing data on a magnetic platter (or flash memory for an SSD). In most cases, the abstraction of treating a file like a stream of binary data will work. However, for a magnetic drive, reading data sequentially will be significantly faster than random access (due to increased overhead of page faults), but for an SSD drive, this overhead will not be present. Underlying details will need to be understood to deal with this case (for example, database index files are structured to reduce the overhead of random access), the abstraction 'leaks' implementation details the developer may need to be aware of.

The example above can become more complex when more abstractions are introduced. The Linux operating system allows files to be accessed over a network but represented locally as 'normal' files. This abstraction will 'leak' if there are network failures. If a developer treats these files as 'normal' files, without considering the fact that they may be subject to network latency and failures, the solutions will be buggy.

The article describing the law suggests that an over-reliance on abstractions, combined with a poor understanding of the underlying processes, actually makes dealing with the problem at hand more complex in some cases.

See also: Hyrum's Law

Real-world examples:

  • Photoshop Slow Startup - an issue I encountered in the past. Photoshop would be slow to startup, sometimes taking minutes. It seems the issue was that on startup it reads some information about the current default printer. However, if that printer is actually a network printer, this could take an extremely long time. The abstraction of a network printer being presented to the system similar to a local printer caused an issue for users in poor connectivity situations.