Data Oriented Programming 1st Edition Yehonathan Sharvit 2024 scribd download
Data Oriented Programming 1st Edition Yehonathan Sharvit 2024 scribd download
com
https://ebookname.com/product/data-oriented-programming-1st-
edition-yehonathan-sharvit/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/java-methods-a-ab-object-oriented-
programming-and-data-structures-maria-litvin/
ebookname.com
https://ebookname.com/product/web-programming-for-business-php-object-
oriented-programming-with-oracle-1st-edition-david-paper/
ebookname.com
https://ebookname.com/product/object-oriented-data-structures-using-
java-chip-weems/
ebookname.com
https://ebookname.com/product/stability-of-nonlinear-shells-on-the-
example-of-spherical-shells-1st-edition-dov-shilkrut-eds/
ebookname.com
Getting Started with Mortared Stonework Walls Patios
Fireplaces Columns More Macfie
https://ebookname.com/product/getting-started-with-mortared-stonework-
walls-patios-fireplaces-columns-more-macfie/
ebookname.com
https://ebookname.com/product/black-against-empire-the-history-and-
politics-of-the-black-panther-party-first-edition-joshua-bloom/
ebookname.com
https://ebookname.com/product/the-routledge-guide-to-music-technology-
thom-holmes/
ebookname.com
https://ebookname.com/product/brothers-and-sisters-in-india-a-study-
of-urban-adult-siblings-1st-edition-g-n-ramu/
ebookname.com
https://ebookname.com/product/the-shenandoah-1st-edition-julia-davis/
ebookname.com
Reduce software complexity
Yehonathan Sharvit
Forewords by Michael T. Nygard
and Ryan Singer
MANNING
Principles of data-oriented programming
Mutable
Representation
Specific Principle #3:
Data is
Data Schema
immutable.
Code Functional
programming
Principle #1:
Separate code
from data.
Object-oriented
programming
Data-Oriented
Programming
REDUCE SOFTWARE COMPLEXITY
YEHONATHAN SHARVIT
Forewords by MICHAEL T. NYGARD and RYAN SINGER
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning Publications
was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
The author and publisher have made every effort to ensure that the information in this book
was correct at press time. The author and publisher do not assume and hereby disclaim any
liability to any party for any loss, damage, or disruption caused by errors or omissions, whether
such errors or omissions result from negligence, accident, or any other cause, or from any usage
of the information herein.
ISBN: 9781617298578
Printed in the United States of America
To Karine, who supports my dysfunctionalities on a daily basis.
brief contents
PART 1 FLEXIBILITY ..................................................................1
1 ■ Complexity of object-oriented programming 3
2 ■ Separation between code and data 26
3 ■ Basic data manipulation 43
4 ■ State management 71
5 ■ Basic concurrency control 91
6 ■ Unit tests 110
vi
BRIEF CONTENTS vii
PART 3 MAINTAINABILITY......................................................245
12 ■ Advanced data validation 247
13 ■ Polymorphism 272
14 ■ Advanced data manipulation 295
15 ■ Debugging 311
contents
forewords xiii
preface xvii
acknowledgments xviii
about this book xx
about the author xxiv
about the cover illustration xxv
dramatis personae xxvi
viii
CONTENTS ix
4 State management
4.1
71
Multiple versions of the system data 72
4.2 Structural sharing 74
4.3 Implementing structural sharing 80
4.4 Data safety 82
4.5 The commit phase of a mutation 83
4.6 Ensuring system state integrity 85
4.7 Restoring previous states 86
6 Unit tests
6.1
110
The simplicity of data-oriented test cases 110
6.2 Unit tests for data manipulation code 112
The tree of function calls 113 Unit tests for functions down
■
the tree 115 Unit tests for nodes in the tree 119
■
PART 2 SCALABILITY....................................................137
10 Database operations
10.1
197
Fetching data from the database 198
10.2 Storing data in the database 204
10.3 Simple data manipulation 207
10.4 Advanced data manipulation 211
11 Web services
11.1
220
Another feature request 221
11.2 Building the insides like the outsides 222
11.3 Representing a client request as a map 225
CONTENTS xi
13 Polymorphism
13.1
272
The essence of polymorphism 273
13.2 Multimethods with single dispatch 277
13.3 Multimethods with multiple dispatch 281
13.4 Multimethods with dynamic dispatch 286
13.5 Integrating multimethods in a production system 289
15 Debugging
15.1
311
Determinism in programming 312
15.2 Reproducibility with numbers and strings 314
15.3 Reproducibility with any data 318
15.4 Unit tests 321
15.5 Dealing with external data sources 329
xii CONTENTS
xiii
xiv FOREWORDS
—MICHAEL T. NYGARD
author of Release It!: Design and
Deploy Production-Ready Software
FOREWORDS xv
This book hit me at just the right time. I had been building web apps for nearly 20
years in an object-oriented framework. I never considered myself an expert program-
mer, but I knew my tools well enough to look at a typical business problem, sketch out
a data model, and build an MVC-style app to get the job done.
Projects were thrilling at the start. I loved the feeling of plugging pieces together
and seeing the app come to life. But once I got it working, I ran into problems. I
couldn’t change one part without keeping all the other models in mind. I knew I
should write tests, but I had to set up so much state to test things that it didn’t feel
worth it—I didn’t want to write more code that would be hard to change. Even run-
ning bits of code in the console was tedious because I had to create database state to
call the method. I thought I was probably doing it wrong, but the solutions I knew
about, such as sophisticated testing frameworks, seemed to add to the complexity
instead of making things easier.
Then one day, I saw a talk on YouTube by Rich Hickey, the creator of Clojure. He
was explaining functional programming and contrasting it with OO, which he deri-
sively called “place-oriented programming.” I wasn’t sure if he was right, but I heard a
hidden message that intrigued me: “It’s not you, it’s your language.” I watched all the
videos I could find and started to think Clojure might be the answer.
Years went by. I kept watching Clojure videos and trying to apply functional princi-
ples when I could. But whenever it was time to start on a new project, I fell back on my
familiar framework. Changing to another language with a totally different ecosystem
of libraries was too big of a leap.
Then, just as I was about to start work on a new product, I found this book. The
words “Data-Oriented” in the title rang a bell. I heard programmers in those Clojure
videos use the words before, but I hadn’t really understood what they meant. Some-
thing about how it’s easier to build systems that manipulate data literals (like maps
and arrays) instead of custom objects. The languages I knew had good support for
data literals, so I thought I might learn something to hold me over until that magical
day when I might switch to Clojure.
My first a-ha moment came right in the introduction. In the first few pages, Yehona-
than explains that, though he’s written Clojure for 10 years, the book isn’t language-
specific, and the examples will be in JavaScript. Wait!—I thought. Could it really be
that I don’t have to change languages to deeply improve the way I write programs?
I was so excited by this prospect that I devoured the book in one sitting. My eyes
opened to something that had been right in front of me all along. Of course my code
was hard to test! Because of the ORM I used, all my functionality was written in objects
that assumed a bunch of database state! When I saw it spelled out with examples in the
book, I couldn’t unsee it. I didn’t need a new language, I just needed to approach pro-
gramming differently!
The designers I consider great all point to the same thing: good design is about
pulling things apart. It’s not just about getting the code to work, no matter how ugly.
Exploring the Variety of Random
Documents with Different Content
most valuable part of the effects, which in most families are
deposited under the imaginary security of locks, the plunder would
seldom recompense the difficulty and hazard of the enterprise; and
till some method of security be adopted by which such keys and
instruments may be rendered useless, no effectual check or
opposition can be given to the excessive and alarming practice of
house-breaking.
“Being confident that I have contrived a security which no
instrument but its proper key can reach; and which may be so
applied as not only to defy the art and ingenuity of the most skilful
workman, but to render the utmost force ineffectual, and thereby to
secure what is most valued as well from dishonest servants as from
the midnight ruffian, I think myself at liberty to declare (what
nothing but the discovery of an infallible remedy would justify my
disclosing), that all dependence on the inviolable security of locks,
even of those which are constructed on the best principle of any in
general use, is fallacious. To demonstrate this bold and alarming
proposition, I shall first state the common principles which are
applied in the art of lock-making; and by describing their operation
in instruments differently constructed, prove to my intelligent
readers that the best-constructed locks are liable to be secretly
opened with great facility; and that the locks in common use are
calculated only to induce a false confidence in their effect, and to
throw temptation to dishonesty in the way of those who are
acquainted with their imperfections, and know their inefficacy to the
purpose of security” (p. 5).
Tumblers had been so little thought of and used at the time
Bramah wrote, that his attention was almost exclusively directed to
warded locks. The mysterious clefts in a key, connected with some
kind of secret mechanism in the lock, had given the warded locks a
great hold on the public mind, as models of puzzlement and
security; and it was to shew that this confidence rested on a false
basis, that he to a great extent laboured. The following is his
exposition of the principle and the defects of the warded lock.
“Locks have been constructed, and are at present much used and
held in great esteem, from which the picklock is effectually excluded;
but the admission of false keys is an imperfection for which no
locksmith has ever found a corrective; nor can this imperfection be
remedied whilst the protection of the bolt is wholly confided to fixed
wards. For if a lock of any given size be furnished with wards in as
curious and complete a manner as it can be, those wards being
necessarily expressed on what is termed by locksmiths the bit or
web of the key, do not admit of a greater number of variations than
can be expressed on that bit or web; when, therefore, as many locks
have been completed of the given size as will include all the
variations which the surface of the bit will contain, every future lock
must be the counterpart of some former one, and the same key
which opens the one will of course unlock the other. It hence follows
that every lock which shall be fabricated on this given scale, beyond
the number at which the capability of variation ends, must be as
subject to the key of some other lock as to its own; and both
become less secure as their counterparts become more numerous.
This objection is confirmed by a reference to the locks commonly
fixed on drawers and bureaus, in which the variations are few, and
these so frequently repeated, from the infinite demand for such
locks, that, even if it were formed to resist the picklock, they would
be liable to be opened by ten thousand correspondent keys. And the
same observation applies in a greater or less degree to every lock in
which the variations are not endless.
“But if the variation of locks in which the bolt is guarded only by
fixed wards could be multiplied to infinity, they would afford no
security against the efforts of an ingenious locksmith; for though an
artful and judicious arrangement of the wards, or other
impediments, may render the passage to the bolt so intricate and
perplexed as to exclude every instrument but its proper key, a skilful
workman having access to the entrance will be at no loss to
fabricate a key which shall tally as perfectly with the wards as if the
lock had been open to his inspection. And this operation may not
only be performed to the highest degree of certainty and exactness,
but is conducted likewise with the utmost ease. For the block or bit,
which is intended to receive the impression of the wards, being fitted
to the keyhole, and the shank of the key bored to a sufficient depth
to receive the pipe, nothing remains but to cover the bit with a
preparation which, by a gentle pressure against the introductory
ward, may receive its impression, and thus furnish a certain direction
for the application of the file. The block or bit being thus prepared
with a tally to the first ward, gains admission to the second; and a
repetition of the means by which the first impression was obtained,
enables the workman to proceed, till by the dexterous use of his file
he has effected a free passage to the bolt. And in this operation he
is directed by an infallible guide; for, the pipe being a fixed centre on
which the key revolves without any variation, and the wards being
fixed likewise, their position must be accurately described on the
surface of the bit which is prepared to receive their impression. The
key therefore may be formed and perfectly fitted to the lock without
any extraordinary degree of genius or mechanical skill. It is from
hence evident that endless variations in the disposition of fixed
wards are not alone sufficient to the purpose of perfect security. I do
not mean to subtract from the merit of such inventions, nor to
dispute their utility or importance. Every approach towards
perfection in the art of lock-making may be productive of much
good, and is at least deserving of commendation; for if no higher
benefit were to result from it, than the rendering difficult or
impossible to many that which is still practicable and easy to a few,
it furnishes a material security against those from whom the greatest
mischiefs and dangers are to be apprehended.”
There can be little doubt, in the present day, that Bramah did not
over-rate the fallacies embodied in the system of wards for locks. He
was sufficiently a machinist to detect the weak points in the ordinary
locks; and, whatever may have been his over-estimate of his own
lock (presently to be described), he was certainly guilty of no
injustice to those who had preceded him; for their locks were
substantially as he has described them. To understand the true
bearings of his Dissertation too, we must remember that
housebreaking had risen to a most daring height in London at the
time he wrote (about the middle of the reign of George III.); and
men’s minds were more than usually absorbed by considerations
relating to their doors and locks.
Mr. Bramah, after doing due justice to the ingenuity of Barron’s
lock, in which, if the tumbler be either over lifted or under lifted the
lock cannot be opened, pointed out very clearly the defective
principle which still governed the lock. “Greatly as the art is indebted
to the ingenuity of Mr. Barron, he has not yet attained that point of
excellence in the construction of his lock which is essential to perfect
security. His improvement has greatly increased the difficulty but not
precluded the possibility of opening his lock by a key made and
obtained as above described (by a wax impression on a blank key);
for an impression of the tumblers may be taken by the same
method, and the key be made to act upon them as accurately as it
may be made to tally with the wards. Nor will the practicability of
obtaining such a key be prevented, however complicated the
principle or construction of the lock may be, whilst the disposition of
its parts may be ascertained and their impression correctly taken
from without. I apprehend the use of additional tumblers to have
been applied by Mr. Barron as a remedy for this imperfection.” Mr.
Bramah thought that Barron had a perception of a higher degree of
security, but had failed to realise it; because, by giving a uniform
motion to the tumblers, and presenting them with a face which
tallies exactly with the key, they still partake in a very great degree
of the nature of fixed wards, and the security of the lock is thereby
rendered in a proportionate degree defective and liable to doubt.
To shew how this insecurity arises, Mr. Bramah illustrates the
matter in the following way: “Suppose the key with which the
workman is making his way to the bolt to have passed the wards,
and to be in contact with the most prominent of the tumblers. The
impression, which the slightest touch will leave on the key, will direct
the application of the file till sufficient space is prepared to give it a
free passage. This being accomplished, the key will of course bear
upon the tumbler which is most remote; and being formed by this
process to tally with the face which the tumblers present, will
acquire as perfect a command of the lock as if it had been originally
made for the purpose. And the key, being thus brought to a bearing
on all the tumblers at once, the benefit arising from the increase of
their number, if multiplied by fifty, must inevitably be lost; for, having
but one motion, they act only with the effect of one instrument.”
It is worthy of notice, that even while thus shewing the weak
points of the Barron lock, Mr. Bramah seems to have had in his mind
some conception of infallibility or inviolability attainable by the lock
in question. After speaking of the defect arising from the bad
arrangement of the tumblers, he says: “But nothing is more easy
than to remove this objection, and to obtain perfect security from
the application of Mr. Barron’s principle. If the tumblers, which
project unequally and form a fixed tally to the key, were made to
present a plane surface, it would require a separate and unequal
motion to disengage them from the bolt; and consequently no
impression could be obtained from without that would give any idea
of their positions with respect to each other, or be of any use even
to the most skilful and experienced workman in the formation of a
false key. The correction of this defect would rescue the principle of
Mr. Barron’s lock, as far as I am capable of judging, from every
imputation of error or imperfection; and, as long as it could be kept
unimpaired, would be a perfect security. But the tumblers, on which
its security depends, being of slight substance, exposed to perpetual
friction—as well from the application of the key as from their own
proper motion—and their office being such as to render the most
trifling loss of metal fatal to their operation, they would need a
further exertion of Mr. Barron’s ingenuity to make them durable.”
It may perhaps be doubted whether the principle of Bramah’s lock
is not more clearly shewn in the original constructed by him than in
that of later date. In appearance it is totally different, but the same
pervading principle is observable in both; and the cylinder lock can
certainly be better understood when this original flat lock has been
studied. The annexed woodcut is taken from the first and very
scarce edition of Mr. Bramah’s Dissertation; the description is
somewhat more condensed, but perhaps sufficient for the purpose.
fig. 33. Bramah’s first model.
The first locks were made with a separate and independent spring
to each slide; but it is a very great improvement, the introduction of
one common spring to raise up the whole number; because if a
person attempts to pick the lock by depressing the slides separately
by means of any small pointed instruments, and by chance brings
two or more of them to the proper depth for turning round, should
he press any one too low, it is difficult to raise it again without
relieving the spring 6, which immediately throws the whole number
of slides up to the top, and destroys all that had been done towards
picking the lock. Another improvement of this lock, and one which
very much increased the difficulty of picking, and its consequent
security, was the introduction of false and deceptive notches cut in
the sliders, as seen at 3, 3. It was found that in the attempt to pick
this lock, an instrument was introduced by the keyhole to force the
cylinder round. At the same time that the slides were depressed by
separate instruments, those slides which were not at the proper level
for moving round were held fast by the notches 5, 5 in the plate f f
bearing against their sides; but when pressed down to the proper
level, or till the notch 2 came opposite f f, they were not held fast,
but were relieved. This furnished the depredator with the means of
ascertaining which slides were pressed low enough, or to the point
for unlocking. The notches 3, 3 in the slides are sometimes cut
above the true notch 2, sometimes below, and at other times one on
each side (one above and one below); they are not of sufficient
depth to allow the cylinder to turn round, but are intended to
mislead any one who attempts to pick, by his not knowing whether it
is the true notch or otherwise, or even whether the slider be higher
or lower than the true notch.
We have not yet sufficiently described the key of the Bramah lock.
One merit of the lock is the remarkable smallness of the key, which
renders it so conveniently portable. The key, as shewn in the upper
part of the figure, has six notches or clefts at the end of its pipe or
barrel; these clefts are cut to different depths, to accord with the
proper extent of movement in the slides. There is a small projection,
10, near the end of the pipe, fitted to enter the notch d in the
cylinder; this forces the cylinder round when the parts are all
properly adjusted. The bolt of the lock, when properly shot or
locked, is prevented from being forced back by the stud c on the
bottom, f, of the cylinder coming into a direct line with its centre of
motion, as shewn in fig. 39; in this position no force, applied to drive
the bolt back, would have any tendency to turn the cylinder round.
fig. 39. Section of the Bramah cylinder.
Fig. 43 represents the key in two different forms, or with the bits
differently arranged. Either form will lock the lock, but the other will
not then unlock it. The end of the key is represented in fig. 44,
shewing the screw which fixes the bits in their places. The bits for a
six-bitted key are shewn separately in fig. 45.
fig. 43. Key of the Parautoptic lock.