tag:blogger.com,1999:blog-67145302024-03-13T02:11:55.406+00:00less code, more softwareHi. I'm <b>Jon Jagger</b>, director of software at <a href="https://kosli.com">Kosli</a>.
I built <a href="http://cyber-dojo.org">cyber-dojo</a>, the place teams <a href="http://jonjagger.blogspot.co.uk/2013/10/practice.html">practice</a> programming.
<br>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.comBlogger654125tag:blogger.com,1999:blog-6714530.post-60884573281842557542022-09-30T12:55:00.004+00:002022-09-30T16:53:43.650+00:00Is this shampoo?Mini-bottles in hotel bathrooms...<br/>
Am I the only one who can't read the writing on them?<br/>
I mean the font size is <i>tiny</i>. You can't read them.<br/>
There's just no way to tell which bottles are which.<br/>
Shampoo? Conditioner? Body lotion? Moisturiser? Shower gel?<br/>
It's unbelievable how many different bottles there are!<br/>
(<a href="https://t.co/r8DZHz3Pn3">But no toothpaste</a>).<br/>
Even if you have your reading glasses on you still can't read the labels.<br/>
Not that you have your reading glasses on when you're about to take a shower!<br/>
Not at first anyway.<br/>
You get in the bathroom and you see the 18 mini-bottles!<br/>
And you think - "figgins - which one is shampoo?"<br/>
You pick one hoping the bottles in this hotel will be different.<br/>
That you'll be able to read the label.<br/>
But no. You can't.<br/>
So you walk back into your room to try and find your glasses.<br/>
Now you're buck naked, wearing nothing but your glasses, about to take a shower!<br/>
You start reading the mini-bottle labels.<br/>
The colour is #3422DE and the background colour is #3422DD.<br/>
They're so similar you cannot read anything!<br/>
It's just not physically possible.<br/>
A small defeat in todays modern world!<br/>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-62926795497468964742020-11-17T11:45:00.002+00:002020-11-17T11:46:17.025+00:00Reality is not what is seems
<div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-uVKQo1lVv9A/X7O3HjeFtXI/AAAAAAAAGLI/BFeN63mfuQkREJhLC0dw0IuarqFXwgTNwCLcBGAsYHQ/s278/RealityIsNotWhatItSeems.jpg" style="display: block; padding: 1em 0; text-align: center; clear: right; float: right;"><img alt="" border="0" height="320" data-original-height="278" data-original-width="181" src="https://1.bp.blogspot.com/-uVKQo1lVv9A/X7O3HjeFtXI/AAAAAAAAGLI/BFeN63mfuQkREJhLC0dw0IuarqFXwgTNwCLcBGAsYHQ/s320/RealityIsNotWhatItSeems.jpg"/></a></div>
is an excellent book by Carlo Rovelli (isbn 978-0-141-98321-9)<br/>
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
It doesn't describe where there is a particle but how the particle shows itself to others.
</blockquote>
<blockquote>
It isn't things that enter into relations but, rather, relations that
ground the notion of 'thing'.
</blockquote>
<blockquote>
An object is a monotonous process. [Nelson Goodman]
</blockquote>
<blockquote>
The sun bends space around itself, and the Earth does not circle around it
drawn by a mysterious distant force but runs straight in a space that inclines.
</blockquote>
<blockquote>
What is conserved is the sum of mass and energy, not each separately.
Processes must exist that transform energy into mass, or mass into energy.
</blockquote>
<blockquote>
Take the equations of quantum mechanics that determines the form of the orbitals
of an electron. This equation has a certain number of solutions, and these
solutions correspond exactly to hydrogen, helium, oxygen ... and the other
elements!
</blockquote>
<blockquote>
On Mars, there are events that in this precise moment have already happened,
events that are yet to happen, but also a quarter of an hour during which
things occur that are neither in our past nor in our future.
</blockquote>
<blockquote>
<a href="http://jonjagger.blogspot.com/2013/12/time.html">Time</a> is not universal and fixed, it is something which expands and shrinks, according to the vicinity of masses.
</blockquote>
<blockquote>
Suppose we want to observe a very, very, very small region of space.
To do this, we need to place something in this area to mark the point
that we wish to consider. Say we place a particle there. Heisenberg had
understood that you can't locate a particle at a point in space for long.
It soon escapes. The smaller the region in which we try to locate a
particle, the greater the velocity at which it escapes. This is
Heisenberg's uncertainty principle.
</blockquote>
<blockquote>
It isn't true that we 'do not <a href="http://jonjagger.blogspot.com/2013/11/visibility.html">see</a>' Faraday lines. We <em>only</em>
see vibrating Faraday lines. "To see" is to perceive light,
and light is the movement of Faraday lines.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-21979216640089579272020-10-22T10:03:00.003+00:002020-10-22T10:03:32.958+00:00The Ascent of Man<div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-tR_sq0tJmU8/X5FYeTrr9qI/AAAAAAAAGJ0/vWOZRhVaVQEM0_IxppyDTHZFgOu7uKGiQCLcBGAsYHQ/s285/AscentOfMan.jpg" style="display: block; padding: 1em 0; text-align: center; clear: right; float: right;"><img alt="" border="0" height="320" data-original-height="285" data-original-width="177" src="https://1.bp.blogspot.com/-tR_sq0tJmU8/X5FYeTrr9qI/AAAAAAAAGJ0/vWOZRhVaVQEM0_IxppyDTHZFgOu7uKGiQCLcBGAsYHQ/s320/AscentOfMan.jpg"/></a></div>
is an excellent book by Jacob Bronowski (isbn 0-7088-2035-2)<br/>
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
<a href="http://jonjagger.blogspot.com/2013/10/adapting.html">Evolution</a> is the climbing of a ladder from the <a href="http://jonjagger.blogspot.com/2016/03/simplicity.html">simple</a> to the <a href="http://jonjagger.blogspot.com/2011/04/complexity.html">complex</a> by steps, each of which is stable in itself.
</blockquote>
<blockquote>
The turning point to the spread of agriculture in the Old World
was almost certainly the occurrence of two forms of what with
a large, full head of seeds. Before 800 BC wheat was not the
luxuriant plant it is today. It was merely one of many wild
grasses that spread throughout the Middle East. By some genetic
accident, the wild wheat crossed with a natural goat grass and
formed a fertile hybrid. ... Now we have a beautiful ear of wheat,
but one which will never spread in the wind because the ear
is too tight to break up.
</blockquote>
<blockquote>
The Principle of Uncertainty is a bad name. In science or outside it, we are not
uncertain, our knowledge is merely confined within a certain tolerance. We should
call it the Principle of Tolerance.
</blockquote>
<blockquote>
When to copper you add an even softer metal, tin, you make
an alloy which is harder and more durable than either - bronze. ...
Almost any pure material is weak, and many impurities will do
to make is stronger.
</blockquote>
<blockquote>
The making of the sword, like all ancient metallurgy, is surrounded
with ritual, and that is for a clear reason. When you have no written
language, when you have nothing that can be called a chemical formula,
then you must have a precise ceremonial which fixes the sequence of
operations so that they are exact and memorable.
</blockquote>
<blockquote>
To us gold is precious because it is scarce; but to the alchemists,
all over the world, gold was precious because it was incorruptible.
... every medicine to fight old age contained gold, metallic gold,
as an essential ingredient, and the alchemists urged their
patrons to drink from gold cups to prolong life.
</blockquote>
<blockquote>
We still use for the female the alchemical symbol for copper, that is,
what is soft: Venus. And we use for the male the alchemical sign
for iron, that is, what is hard: Mars.
</blockquote>
<blockquote>
When the Bible says three wise men followed a star to Bethlehem,
there sounds in the story the echo of an age when wise men were
stargazers.
</blockquote>
<blockquote>
Three thousand years after they were made, the village women
of Khuzistan still draw their water ration from the qanats.
</blockquote>
<blockquote>
The Greeks when they saw the Scythian riders believed the horse
and the rider to be one; that is how they invented the legend
of the centaur.
</blockquote>
<blockquote>
Galileo is the creator of the modern scientific method... he really did
for the first time what we think of as practical science: build the
apparatus, do the experiment, publish the results.
</blockquote>
<blockquote>
Relativity is the understanding of the world not as events but as relations.
</blockquote>
<blockquote>
He [Einstein] hated war, and cruelty, and hypocrisy, and above all he hated dogma.
</blockquote>
<blockquote>
It was because [James] Brindley could not spell the world 'navigator' that
workmen who dig trenches or canals are still called 'navvies'.
</blockquote>
<blockquote>
Always [Leo] Szilard wanted the [atom] bomb to be <a href="http://jonjagger.blogspot.com/2013/12/testing.html">tested</a> openly before the Japanese and an international audience, so that the Japanese should know
its power and should surrender before people died.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-7397394735730745282020-10-16T16:10:00.005+00:002020-10-16T16:15:24.960+00:00TDD - Romanes Eunt Domus!<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.blogger.com/video.g?token=AD6v5dzszBUSdgRxx9QUzgCXCduTvlIuLOBxfZ4Uz3X9vgsv-14RnK2U05biEJdhjLZZlecZSiFktIxVWeU' class='b-hbp-video b-uploaded' frameborder='0'></iframe>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-17778144966733838612020-10-13T16:21:00.000+00:002020-10-13T16:21:49.929+00:00What is Life?<div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-m4rS25Y9z0k/X4XTDuHrsWI/AAAAAAAAGI8/OkBzNIeuPH0sVn78raMNTuy7EGAUnq_HACLcBGAsYHQ/s292/WhatIsLife.jpg" style="display: block; padding: 1em 0; text-align: center; clear: left; float: right;"><img alt="" border="0" height="320" data-original-height="292" data-original-width="172" src="https://1.bp.blogspot.com/-m4rS25Y9z0k/X4XTDuHrsWI/AAAAAAAAGI8/OkBzNIeuPH0sVn78raMNTuy7EGAUnq_HACLcBGAsYHQ/s320/WhatIsLife.jpg"/></a></div>
is an excellent book by Paul Nurse (isbn 978-1-788451-40-6)<br/>
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
Cells repair these mutations, but they are not
completely successful. If they were, all individuals
of a species would be identical and evolution would stop.
This means the error rate itself is subject to natural selection.
If that error rate is too high the information stored by the genome
will degenerate and become meaningless, and if errors are too rare,
the possibility for evolutionary <a href="http://jonjagger.blogspot.com/2013/04/changing.html">change</a> is reduced.
Over the long term, the most successful species will be those that can maintain
the right balance between constancy and <a href="http://jonjagger.blogspot.com/2013/04/changing.html">change</a>.
</blockquote>
<blockquote>
Humanity should care about the entire biosphere; all the different
life forms that share our planet are our relatives.
</blockquote>
<blockquote>
Once microbes evolved the ability to photosynthesize, they
multiplied, over the millenia, to such an extent that the amount
of oxygen in the atmosphere spiked. What followed, between 2
and 2.4 billion years ago, is called the Great Oxygen
Catastrophe. All organisms that existed at that <a href="http://jonjagger.blogspot.com/2013/12/time.html">time</a> were
microbes, either bacteria or archaea, but some researchers
think <em>most</em> of them were wiped out by the appearance
of all that oxygen. It is ironic that life created conditions
that nearly ended life as a whole.
</blockquote>
<blockquote>
To fuel all of the chemical reactions needed to support your
body's trillions of cells, your mitochondria together produce,
amazingly, the equivalent of your entire bodyweight in ATP every day!
</blockquote>
<blockquote>
The life forms that survive natural selection persist because they
<em>work</em>, not necessarily because they do things in the most
efficient or straightforward way possible. ... This can disturb
some physicists who turn their attentions to biology. Physicists
tend to be attracted to elegant, <a href="http://jonjagger.blogspot.com/2016/03/simplicity.html">simple</a> solutions, and can be less comfortable with the messy and less-than-perfect reality of
living <a href="http://jonjagger.blogspot.com/2013/10/systems.html">systems</a>.
</blockquote>
<blockquote>
Even plants rely on bacteria found in or near their roots that
capture nitrogen from the atmosphere. ... In fact, this is something that,
as far as we know, no eukaryote can do for itself. That means there is
not a single known species of animal, plant or fungus that can generate
its own cellular chemistry entirely from scratch.
</blockquote>
<blockquote>
Life on Earth belongs to a single, vastly interconnected ecosystem,
which incorporates all living organisms.
</blockquote>
<blockquote>
Biology shows us that all the living organisms we know of are related
and closely interacting. We are bound by a deep connectedness
to all other life.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-52498438384321831762020-10-06T14:14:00.000+00:002020-10-06T14:14:00.258+00:00Sapiens. A Brief History of Human Kind<div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-5aojtTZRjPo/X3x6rD14rAI/AAAAAAAAGIM/1-Ercz0wBY8NY8fxNmrmhg9luDYRKVVBACLcBGAsYHQ/s274/Sapiens.jpg" style="display: block; padding: 1em 0; text-align: center; clear: left; float: right;"><img alt="" border="0" height="320" data-original-height="274" data-original-width="184" src="https://1.bp.blogspot.com/-5aojtTZRjPo/X3x6rD14rAI/AAAAAAAAGIM/1-Ercz0wBY8NY8fxNmrmhg9luDYRKVVBACLcBGAsYHQ/s320/Sapiens.jpg"/></a></div>
is an excellent book by Yuval Noah Harari (isbn 978-0-099-59008-8)<br/>
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
Whereas chimpanzees spend five hours a day chewing raw food, a single
hour suffices for people eating cooked food.
</blockquote>
<blockquote>
Since long intestines and large brains are both massive energy consumers,
it's hard to have both. By shortening the intestines and decreasing their
energy consumption, cooking inadvertently opened the way to the jumbo
brains of Neanderthals and Sapiens.
</blockquote>
<blockquote>
Large numbers of strangers can cooperate successfully by believing in
common myths.
</blockquote>
<blockquote>
There is some evidence that the size of the average Sapiens brain has actually
decreased since the age of foraging. Survival in that era required superb mental
abilities from everyone.
</blockquote>
<blockquote>
What characterises all these acts of <a href="http://jonjagger.blogspot.com/2013/10/communication.html">communication</a> is that the entities being addressed are local beings. They are not universal gods, but rather a particular
deer, a particular tree, a particular stream, a particular ghost.
</blockquote>
<blockquote>
The extra food did not translate into a better diet or more leisure.
Rather, it translated into population explosions and pampered elites.
</blockquote>
<blockquote>
The new agricultural tasks demanded so much <a href="http://jonjagger.blogspot.com/2013/12/time.html">time</a>
that <a href="http://jonjagger.blogspot.com/2013/12/people.html">people</a> were forced
to settle permanently next to their wheat fields.
</blockquote>
<blockquote>
This is the essence of the Agricultural Revolution: the ability to keep more
people alive under worse conditions.
</blockquote>
<blockquote>
One of history's few iron laws is that luxuries tend to become necessities and to
spawn new obligations.
</blockquote>
<blockquote>
Evolution is based on difference, not on equality.
</blockquote>
<blockquote>
There is not a single organ in the human body that only does the job its prototype
did when it first appeared hundreds of millions of years ago.
</blockquote>
<blockquote>
The mere fact that Mediterranean people believed in gold would cause Indians to
start believing in it as well. Even if Indians still had no real use for gold,
the fact that Mediterranean people wanted it would be enough to make the Indians
value it.
</blockquote>
<blockquote>
The first religious effect of the Agricultural Revolution was to turn plants
and animals from equal members of a spiritual round table into property.
</blockquote>
<blockquote>
The monotheist religions expelled the gods through the front door with a lot
of fanfare, only to take them back in through the side window. Christianity,
for example, developed its own pantheon of saints, whose cults differed
little from those of the polytheistic gods.
</blockquote>
<blockquote>
Level two chaos is chaos that reacts to predictions about it, and therefore
can never be predicted accurately.
</blockquote>
<blockquote>
In many societies, more people are in danger of dying from obesity than
from starvation.
</blockquote>
<blockquote>
Each year the US population spends more money on diets than the amount
needed to feed all the hungry people in the rest of the world.
</blockquote>
<blockquote>
Throughout history, the upper classes always claimed to be smarter, stronger
and generally better than the underclasses... With the help of new medical
capabilities, the pretensions of the upper classes might soon become an
objective reality.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-8624754678768966592020-10-05T18:26:00.003+00:002020-10-05T18:30:52.833+00:00The Culture Code<div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-GoPiQMd38JA/X3tlGd3lVGI/AAAAAAAAGH8/A0zjRyQkzhwpD-Fk6X5JhaE3YwsGdxMowCLcBGAsYHQ/s274/CultureCode.jpg" style="display: block; padding: 1em 0; text-align: center; clear: right; float: right;"><img alt="" border="0" height="200" data-original-height="274" data-original-width="184" src="https://1.bp.blogspot.com/-GoPiQMd38JA/X3tlGd3lVGI/AAAAAAAAGH8/A0zjRyQkzhwpD-Fk6X5JhaE3YwsGdxMowCLcBGAsYHQ/s200/CultureCode.jpg"/></a></div>
is an excellent book by Daniel Coyle (isbn 978-1-847-94127-5)<br/>
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
Much of the connection happens around the dinner table,
as Popovich is obsessed with food and wine.
</blockquote>
<blockquote>
One misconception about highly successful <a href="http://jonjagger.blogspot.com/2011/11/culture-quotes.html">cultures</a>
is that they are happy, lighthearted places. This is mostly not
the case. They are energized and engaged, but at their core
their members are oriented less around achieving happiness
than around solving hard problems together.
</blockquote>
<blockquote>
Allen could find none that played a meaningful role in
cohesion. Except for one. The distance between their desks.
</blockquote>
<blockquote>
For the vast majority of human history, sustained proximity
has been an indicator of belonging.
</blockquote>
<blockquote>
It's important to avoid interruptions. The smoothness of turn
taking, as we've seen, is a powerful indicator of cohesive
group performance.
</blockquote>
<blockquote>
The groups I studied had extremely low tolerance for bad apple behaviour.
</blockquote>
<blockquote>
The groups I visited were uniformly obsessed with design as a lever
for cohesion and interaction.
</blockquote>
<blockquote>
He also had the company buy nicer coffee machines and install them
in more convenient gathering places.
</blockquote>
<blockquote>
Merely replacing four-person tables with ten-person tables has
boosted productivity by 10 percent.
</blockquote>
<blockquote>
Kauffman decreed that every aspect of training be team-based.
</blockquote>
<blockquote>
It's very hard to be empathic when you're talking.
</blockquote>
<blockquote>
The road to success is paved with mistakes well handled.
</blockquote>
<blockquote>
We should have made the hallways wider. We should have made the cafe bigger.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-18971599221447734522020-04-06T19:01:00.002+00:002020-10-05T18:18:11.390+00:00Life, the Universe and Everything
<div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-6wznay_24pc/Xot8PcAMu4I/AAAAAAAAF88/414ozr7Elj8mCqoRUFGlXDhW0QHL0NLPwCLcBGAsYHQ/s1600/LifeTheUniverseAndEverything.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://4.bp.blogspot.com/-6wznay_24pc/Xot8PcAMu4I/AAAAAAAAF88/414ozr7Elj8mCqoRUFGlXDhW0QHL0NLPwCLcBGAsYHQ/s320/LifeTheUniverseAndEverything.jpg" width="180" height="320" data-original-width="900" data-original-height="1600" /></a></div>
is an excellent book by Douglas Adams (isbn 978-0-330-49120-4).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
He would insult the universe. That is, he would insult
everybody in it. Individually, personally, one by one,
and (this was the thing that he really decided to grit his teeth over)
in alphabetical order.
</blockquote>
<blockquote>
'Eddies,' said Ford, 'in the space-time continuum.'<br/>
'Ah, nodded Arthur, 'is he? Is he?'
</blockquote>
<blockquote>
It was a charming and delightful day at Lord's as
Ford and Arthur tumbled haphazardly out of a space-time
anomaly and the immaculate turf rather hard.
</blockquote>
<blockquote>
'A what?' he said.<br/>
'An SEP.'<br/>
'An S...?'<br/>
'...EP.'<br/>
'And what's that?'<br/>
'Somebody Else's Problem,' said Ford.
</blockquote>
<blockquote>
The second non-absolute number is the given time of arrival,
which is now known to be one those most bizarre of concepts,
a recipriversexcluson, a number whose existence can only
be defined as being anything other than itself. In other words,
the given time of arrival is the one moment of time at which
it is impossible that any member of the party will arrive.
</blockquote>
<blockquote>
'Tell me it was a coincidence, Dent' it said. 'I dare you
to tell me it was a coincidence!'
'It was a coincidence,' said Arthur quickly.
</blockquote>
<blockquote>
It was the Cathedral of Hate.<br/>
It was the product of a min that was not merely twisted,
but actually sprained.<br/>
It was huge. It was horrific.<br/>
It had a Statue in it.<br/>
We will come to the Statue in a moment.
</blockquote>
<blockquote>
'I got yanked involuntarily back into the physical word,'
pursued Agrajag, 'as a bunch of petunias. In, I might add, a bowl.'
</blockquote>
<blockquote>
Strictly speaking, all editors since Lig Lury Jr have therefore
been designated Acting Editors, and Lig's desk is still
preserved the way he left it, with the addition of a small sign
which says 'Lig Lury Jr, Editor, Missing, presumed Fed'.
</blockquote>
<blockquote>
The longest and most destructive party ever held is now into its
fourth generation and still no one shows any signs of leaving.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-56418228004946894212020-03-29T16:11:00.000+00:002020-03-29T16:29:26.317+00:00The Restaurant at the End of the Universe<div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-FaRqmoezJcY/XoDITyep_2I/AAAAAAAAF7E/3TCsPlQeZn4I0jqM2iSHwuHmglX9o4Y3wCLcBGAsYHQ/s1600/TheRestaurantAtTheEndOfTheUniverse.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://2.bp.blogspot.com/-FaRqmoezJcY/XoDITyep_2I/AAAAAAAAF7E/3TCsPlQeZn4I0jqM2iSHwuHmglX9o4Y3wCLcBGAsYHQ/s320/TheRestaurantAtTheEndOfTheUniverse.jpg" width="180" height="320" data-original-width="900" data-original-height="1600" /></a></div>
is an excellent book by Douglas Adams (isbn 978-0-330-49121-1).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
There is another theory which states that this has already happened.
</blockquote>
<blockquote>
The story so far: In the beginning the Universe was created.
This had made a lot of people very angry and been widely regarded
as a bad move.
</blockquote>
<blockquote>
The motto stands - or rather stood - in three-mile high illuminated
letters near the Complaints Department spaceport on Eadraz.
Unfortunately its weight was such that shortly after it was erected,
the ground beneath the letters caved in and they dropped for nearly
half their length through the offices of many talented young
complaints executives - now deceased.
</blockquote>
<blockquote>
Quite how Zaphod Bebblebrox arrived at the idea of holding a
seance at this point is something he was never quite clear on.
</blockquote>
<blockquote>
'Listen, three eyes,' he said', 'don't you try to outweird me,
I get stranger things than you free with my breakfast cereal.'
</blockquote>
<blockquote>
Marvin was forced to say something which came very hard to him.
'I don't know,' he said.
</blockquote>
<blockquote>
'I go up,' said the elevator, 'or down.'<br/>
'Good,' said Zaphod. 'We're going up.'<br/>
'Or down,' the elevator reminded him.
</blockquote>
<blockquote>
And the worse they were to wear, the more people had to buy to
keep themselves shod, and the more the shops poliferated,
until the whole economy of the place passed what I believe is
termed the Shoe Event Horizon, and it became no longer economically
possible to build anything other than shoe shops.
</blockquote>
<blockquote>
'Transtellar Cruise Lines would like to apologize to passengers
for the continuing <a href="http://jonjagger.blogspot.com/2013/12/time.html">delay</a> to this flight. We are currently
awaiting the loading of our complement of small lemon-soaked
napkins for your comfort, refreshment and hygiene during the
journey. Meanwhile we thank you for your patience.'
</blockquote>
<blockquote>
In it, guests take (willan on-take) their places at table
and eat (willan on-eat) sumptuous meals whilst watching
(willing watchen) the whole of creation explode around them.
</blockquote>
<blockquote>
'You've never heard of Disaster Area?'
</blockquote>
<blockquote>
'It says "Golgafrinchan Ark Fleet, Ship B, Hold Seven,
Telephone Sanitizer Second Class" - and a serial number.'
</blockquote>
<blockquote>
To summarize the summary: anyone who is capable of getting
themselves made President should on no account be allowed
to do the job.
</blockquote>
<blockquote>
Their track suits were now all dirty and even torn, but they
all had immaculately styled hair.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-60521943716223915672020-03-23T09:26:00.000+00:002020-03-24T06:18:34.338+00:00The Hitch Hiker's Guide to the Galaxy
<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-SOlSh7CenJ4/XniAiYpmQHI/AAAAAAAAF6c/K-fetIWt6o4Uyqly6SYWw10XbXGzQ3xjgCLcBGAsYHQ/s1600/HitchHikersGuide.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-SOlSh7CenJ4/XniAiYpmQHI/AAAAAAAAF6c/K-fetIWt6o4Uyqly6SYWw10XbXGzQ3xjgCLcBGAsYHQ/s320/HitchHikersGuide.jpg" width="180" height="320" data-original-width="900" data-original-height="1600" /></a></div>
is an excellent book by Douglas Adams (isbn 978-0-330-49119-8).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a>
I'm going to quote from a few pages. <br />
<br />
<blockquote>
Far out in the uncharted backwaters of the unfashionable
end of the Western Spiral Arm of the Galaxy lies a small
unregarded yellow sun.
</blockquote>
<blockquote>
The Guide also tells you on which planets the best Pan
Galactic Gargle Blasters are mixed, how much you can
expect to pay for one and what voluntary <a href="http://jonjagger.blogspot.com/2013/12/organization.html">organizations</a>
exist to help you rehabilitate afterwards.
</blockquote>
<blockquote>
People of Earth, your attention please.
This is Prostetnic Vogon Jeltz of the Galactic Hyperspace
Planning Council...
</blockquote>
<blockquote>
The practical upshot of all this is that if you stick a Babel fish in
your ear you can instantly understand anything said to you in any
form of language.
</blockquote>
<blockquote>
The prisoners sat in the Poetry Appreciation chars - strapped in.
</blockquote>
<blockquote>
'Space', it says, 'is big. Really big. You just won't believe how
vastly hugely mindbogglingly big it is. I mean you may think it's
a long way down the road to the chemist, but that's just peanuts to
space. Listen...' and so on.
</blockquote>
<blockquote>
The principle of generating small amounts of finite improbability
by simply hooking the logic circuits of a Bambleweeny 57
Sub-Meson Brain to an atomic vector plotter suspended in a strong
Brownian Motion producer (say a nice hot cup of tea) were of
course well understood.
</blockquote>
<blockquote>
Here I am, brain the size of a planet and they ask me to take you
down to the bridge. Call that job satisfaction? Cos I don't.
</blockquote>
<blockquote>
For years radios had been operated by means of pressing buttons
and turning dials; then as technology became more sophisticated
the controls were made touch sensitive - you merely had to brush
the panels with your fingers; now all you had to do was wave your
hand in the general direction of the components and hope. It saved
a lot muscular expenditure of course, but meant that you had to sit
infuriatingly still if you wanted to keep listening to the same
programme.
</blockquote>
<blockquote>
'Oh God,' said Zaphod. He hadn't worked with this computer for
long but had already learned to loathe it.
</blockquote>
<blockquote>
'Computer!' shouted Zaphod. 'Rotate angle of vision through
one-eighty degrees and don't talk about it!'
</blockquote>
<blockquote>
Many men of course became extremely rich, but this was perfectly
natural and nothing to be ashamed of because no one was
really poor - at least no one worth speaking of.
</blockquote>
<blockquote>
'Hi there! This is Eddie your shipboard computer, and I'm feeling
just great, guys, and I know I'm just going to get a bundle of kicks
out of any program you care to run through me.'
</blockquote>
<blockquote>
'You just let the machines get on with the adding up,' warned
Majikthise, 'and we'll take care of the eternal verities, thank you very much.
You want to check your legal position you do, mate. Under the law the
Quest for the Ultimate Truth is quite clearly the inalienable prerogative
of your working thinkers.'
</blockquote>
<blockquote>
'I think the problem, to be quite honest with you, is that you've
never actually known what the question is.'
</blockquote>
<blockquote>
R is a velocity measure, defined as a reasonable speed of travel
that is consistent with health, mental wellbeing and not being more than
say five minutes late.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-13049938105260060062017-10-13T20:13:00.000+00:002017-10-18T08:17:09.009+00:00How to read water
<div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-MWF3gQgW0gw/WeEdHvYrrGI/AAAAAAAAFUk/JJg0VQa0oJgnaZ37YFW-5uq83vHwLtl1QCLcBGAs/s1600/HowToReadWater.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://3.bp.blogspot.com/-MWF3gQgW0gw/WeEdHvYrrGI/AAAAAAAAFUk/JJg0VQa0oJgnaZ37YFW-5uq83vHwLtl1QCLcBGAs/s400/HowToReadWater.jpg" width="225" height="400" data-original-width="900" data-original-height="1600" /></a></div>
is an excellent book by Tristan Gooley (isbn 978-1-473-61522-9).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to quote from a few pages:
<blockquote>
One of the universal truths of human observation is that we
<a href="http://jonjagger.blogspot.co.uk/2013/11/observing.html">see</a>
more of what we expect to see and less of what we don't expect to see.<br/>
Much of my work is not about teaching people to see things that are hard to see,
but in showing them how to notice the things that hide in plain sight.
</blockquote>
<blockquote>
It did not take sailors long to work out that a ship that carries too much may
be vulnerable in heavy seas, but sailors were rarely the ones to make the
decision about how much cargo a ship could safely carry. The merchants making
the profit would have had a different view to the deckhand, especially if the
trader never set foot on the vessel. This led to a wrestle between greedy traders
and cautious captains that lasted centuries. The first attempts to regulate how
much a ship could carry go back over four thousand years to ancient Crete.
<br/>
Samuel Plimsoll, a nineteenth-century English politician, realized that a low
freeboard height can present a problem, but he also appreciated that it becomes
the solution if we take a very keen interest in it. In other words,
we can tell if there is too much cargo in the boat by looking much more carefully
at how high the water rises up the side of the hull. And the easiest way to do
this is by drawing a ruler on the side of the ship, calibrated according to an
architect's or engineer's understanding of the boat. These lines, which became
known as Plimsoll Lines, were such a simple and brilliant success that they
became law and proliferated around the world.
</blockquote>
<blockquote>
From 1833, when the first tide tables were produced by the Admiralty, the
emphasis shifted from looking, thinking and understanding, to depending
on tables of others' measurements.
</blockquote>
<blockquote>
There is a stange truth in the profile of beaches: they have <a href="http://jonjagger.blogspot.co.uk/2013/10/adapting.html">evolved</a> in a physical
sense to be a near ideal shape to defend themselves against the onslaught of the sea.
This means that almost any attempt to engineer a 'solution' to what nature is trying
to achieve has as much chance of backfiring as working.
</blockquote>
<blockquote>
Many sailors use little pieces of fabric, nicknamed 'tell-tails', that are
tied to the sails and stays (the wires that give the mast stability), to offer
a constant <a href="http://jonjagger.blogspot.co.uk/2013/11/visibility.html">visual</a>
reminder of what the wind is doing.
</blockquote>
<blockquote>
Once the depth of the water is half the wavelength of the waves, it effectively
cramps the motion of the waves and it is this that slows them down.
</blockquote>
<blockquote>
Sailors dislike precision almost as much as they like
<a href="http://jonjagger.blogspot.co.uk/2010/05/bureaucracy.html">bureaucracy</a>.
</blockquote>
<blockquote>
Rivers do not run straight for more than ten times their own width.
</blockquote>
<blockquote>
There will be an alternating combination of quick water and much slower
water and this always happens in a certain way. The quick patches are knowns,
perhaps onomatopoeically, as 'riffles' and the slower areas are known as pools.
If there is no human tinkering with a river's flow, then there will be a
riffle-pool sequence for every stretch of river that is fives times its width.
</blockquote>
<blockquote>
It is typical for the water at the sides of a river to be
travelling at only a quarter of the speed of the water in
the centre. The river is being slowed by two things at its sides;
when it comes into contact with banks it is slowed by friction
and it is also slowed by the shallowing at the sides.
</blockquote>
<blockquote>
A stream is just a river you can step over.
</blockquote>
<blockquote>
Swell is the name of the waves that have enough energy to
travel beyond the area of wind.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com4tag:blogger.com,1999:blog-6714530.post-75486911792036091612017-10-10T16:42:00.002+00:002017-10-11T16:57:26.074+00:00Organize for Complexity
<div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-Yl3BK86qV8Q/Wdz3lhst9sI/AAAAAAAAFUI/jXtsbHb_DQ8PfEFyXfVfovnCepi7XTxkwCLcBGAs/s1600/OrganizeForComplexity.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://3.bp.blogspot.com/-Yl3BK86qV8Q/Wdz3lhst9sI/AAAAAAAAFUI/jXtsbHb_DQ8PfEFyXfVfovnCepi7XTxkwCLcBGAs/s400/OrganizeForComplexity.jpg" width="259" height="400" data-original-width="1036" data-original-height="1600" /></a></div>
is an excellent book by Niels Pflaeging (isbn 978-0-9915376-0-0).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to quote from a few pages:
<blockquote>
What Taylor pioneered was the idea of consistently dividing an
<a href="http://jonjagger.blogspot.co.uk/2013/12/organization.html">organization</a>
between thinking people (managers) and executing
<a href="http://jonjagger.blogspot.co.uk/2013/12/people.html">people</a> (workers).
</blockquote>
<blockquote>
Problem-solving in a life-less <a href="http://jonjagger.blogspot.co.uk/2013/10/systems.html">system</a> is about instruction.<br/>
Problem-solving in a living <a href="http://jonjagger.blogspot.co.uk/2013/10/systems.html">system</a> is about
<a href="http://jonjagger.blogspot.co.uk/2013/10/communication.html">communication</a>.
</blockquote>
<blockquote>
Any attempt to motivate can only lead to de-motivation.
</blockquote>
<blockquote>
Ultimately, organizing for <a href="http://jonjagger.blogspot.co.uk/2011/04/complexity.html">complexity</a>
and self-organization are always about empowering
<a href="http://jonjagger.blogspot.co.uk/2013/10/teams.html">teams</a>
... not about empowering individuals.
</blockquote>
<blockquote>
Actual <a href="http://jonjagger.blogspot.co.uk/2013/10/teams.html">teams</a>
of people working for and with each other.
</blockquote>
<blockquote>
Nobody is in control. Everybody is in charge.
</blockquote>
<blockquote>
To be intensively involved in selection [recruiting] is a matter of honor.
</blockquote>
<blockquote>
A hallmark of great selection is that it is highly
<a href="http://jonjagger.blogspot.co.uk/2013/12/time.html">time</a>-consuming.
</blockquote>
<blockquote>
Management is a mindset that will not just go away all by itself.
</blockquote>
<blockquote>
When employees think for themselves and make entrepreneurial decisions
automonomously, you must at all times bear joint reponsibility for
those decisions, even if you or other members of the organization
might have decided differently.
</blockquote>
<blockquote>
A "beta" kind of organization produces many such stories: Peculiarities,
unusual practices, by which they can be instantly recognized among so
many over-managed and under-led organizations.
</blockquote>
<blockquote>
People do not need to be forced to work. However, this deeply-seated
prejudice about people and their relationship to work is what keeps
management alive.
</blockquote>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-3526602057492423762017-09-07T19:29:00.002+00:002017-09-07T19:29:03.871+00:00The book of five rings<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_u9orsdHAiCg/TTYVzX-eVlI/AAAAAAAAArg/R5k2G0gllKw/s1600/TheBookOfFiveRings.JPG"><img style="float:right; margin:0em;cursor:pointer; cursor:hand;width: 150px; height: 200px;" src="https://4.bp.blogspot.com/_u9orsdHAiCg/TTYVzX-eVlI/AAAAAAAAArg/R5k2G0gllKw/s200/TheBookOfFiveRings.JPG" border="0" alt="" id="BLOGGER_PHOTO_ID_5563658362016388690"></a>
is an excellent book by Miyamoto Musashi, translated by Thomas Cleary (isbn 1-57062-748-7).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to quote from a few pages:
<blockquote>
Preface: In common parlance, to do something with a real sword means to do it with utmost earnestness… The Book of Five Rings… explicitly intended to symbolise processes of struggle and mastery in all concerns and walks of life.
</blockquote>
<blockquote>
The martial way of life practiced by warriors is based on excelling others in anything and everything.
</blockquote>
<blockquote>
Fourth is the way of the artisan. In terms of the way of the carpenter, this involves skilful construction of all sorts of tools, knowing how to use each tool skilfully, drawing up plans correctly by means of the square and the ruler, making a living by diligent <a href="http://jonjagger.blogspot.co.uk/2013/10/practice.html">practice</a> of the craft… <a href="http://jonjagger.blogspot.co.uk/2013/10/practice.html">practice</a> unremittingly.
</blockquote>
<blockquote>
You should <a href="http://jonjagger.blogspot.co.uk/2013/11/observing.html">observe</a> reflectively, with overall awareness of the large picture a well as precise attention to small details.
</blockquote>
<blockquote>
Having attained a principle, one detaches from the principles; thus one has spontaneous independence in the science of martial arts and naturally attains marvels.
</blockquote>
<blockquote>
As human beings, it is essential for each of us to cultivate and polish our individual path.
</blockquote>
<blockquote>
<a href="http://jonjagger.blogspot.co.uk/2013/11/observing.html">Observation</a> and perception are two separate things.
</blockquote>
<blockquote>
It is essential to be relaxed in body and mind.
</blockquote>
<blockquote>
If you get to feeling snarled up and are making no progress, you toss your mood away and think in your heart that you are starting everything anew.
</blockquote>
<blockquote>
In my military science, it is essential that the physical aspect and the mental state both be simple and direct.
</blockquote>
<blockquote>
Whether in large- or small-scale military science, there is no narrow focus of the vision. As I have already written, by finicky narrowness of focus, you forget about bigger things and get confused, thus letting certain victory escape you.
</blockquote>
<blockquote>
Things stick in your mind because of being in doubt.
</blockquote>
<blockquote>
The <a href="http://jonjagger.blogspot.co.uk/2013/10/practice.html">practice</a> of all the arts is for the purpose of clearing away what is on your mind. In the beginning, you do not know anything, so <a href="http://jonjagger.blogspot.co.uk/2014/01/paradox.html">paradoxically</a> you do not have any questions on your mind. Then, when you get into studies, there is something on your mind and you are obstructed by that. This makes everything difficult to do.
</blockquote>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-77553596087979648292017-08-06T13:27:00.000+00:002017-08-07T00:07:38.543+00:00practising jQuery in cyber-dojo<a href="http://cyber-dojo.org">cyber-dojo</a> now supports practising <a href="http://jquery.com/">jQuery</a>.<br>
Enjoy :-)<br>
<br>
<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-nvdlJsioQNo/WYcX2lgpFDI/AAAAAAAAFPo/A9QTroL0mskVcHG-a3JMEKeGkSukVDdzACLcBGAs/s1600/setupPage.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img style="border:2px solid gray;" src="https://1.bp.blogspot.com/-nvdlJsioQNo/WYcX2lgpFDI/AAAAAAAAFPo/A9QTroL0mskVcHG-a3JMEKeGkSukVDdzACLcBGAs/s640/setupPage.png" width="640" height="537" data-original-width="1510" data-original-height="1268" /></a></div>
<br>
<div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-PMD5lIZgGCw/WYcX2-9xWmI/AAAAAAAAFPk/vBiBoxbgRlMQ08M41qDNfA9NL1STynrDQCLcBGAs/s1600/jQueryAdaptor.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img style="border:2px solid gray;" src="https://3.bp.blogspot.com/-PMD5lIZgGCw/WYcX2-9xWmI/AAAAAAAAFPk/vBiBoxbgRlMQ08M41qDNfA9NL1STynrDQCLcBGAs/s640/jQueryAdaptor.png" width="640" height="292" data-original-width="1600" data-original-height="730" /></a></div>
<br>
<div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-QWIGj-4bCZE/WYeuqI61eqI/AAAAAAAAFP4/lIY4OfPPf2kMZbXP9woFyqCYhGR1EKZzACLcBGAs/s1600/jQueryTest.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img style="border:2px solid gray;" src="https://4.bp.blogspot.com/-QWIGj-4bCZE/WYeuqI61eqI/AAAAAAAAFP4/lIY4OfPPf2kMZbXP9woFyqCYhGR1EKZzACLcBGAs/s640/jQueryTest.png" width="640" height="412" data-original-width="1600" data-original-height="1031" /></a></div>
<br>
<br>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-65975223574433971202017-07-06T09:33:00.000+00:002017-07-06T09:33:35.422+00:00compile time assertions in C
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="author" content="Jon Jagger" />
<style media="screen" type="text/css">
body.hook
{
font-family: "Times", serif;
background: white;
margin-left: 3.0em;
margin-right: 0.5em;
}
h1
{
color: #993222;
font-style: normal;
font-weight: normal;
font-family: Georgia, Utopia, 'Palatino Linotype', Palatino, serif;
}
pre.fragment
{
font-size: 0.8em;
font-family: "Courier New", Courier, monospace;
font-weight: normal;
background-color: black;
color: white;
padding: 1em 1em 1em 1em;
padding: 0.7em 0.7em 0.7em 0.7em;
margin: 1em 1em 1em 2em;
}
</style>
</head>
<body class="hook">
<P>
C has a facility for checking dynamic assertions at run-time. It's inside
<SPAN CLASS="CODE"><assert.h></SPAN> and its called
<SPAN CLASS="CODE">assert</SPAN>. Now
<SPAN CLASS="CODE">assert</SPAN> is a macro, so why isn't it called
<SPAN CLASS="CODE">ASSERT</SPAN>? I don't know. Prior art no doubt. Anyway,
<SPAN CLASS="CODE">assert</SPAN> is a
dynamic runtime feature, you can only use it inside functions.
</P>
<PRE class="fragment">
<SPAN CLASS="COMMENT">/* not inside a function, won't compile :-( */</SPAN>
assert(sizeof(int) * CHAR_BIT >= 32);
</PRE>
<P>
That's a pity because it would be nice if I could get the compiler to
check things like this automatically at compile time. I've occasionally seen
an attempt at a compile-time check like this...
</P>
<PRE class="fragment">
#if sizeof(int) * CHAR_BIT < 32
#error People of Earth. Your attention please...
#endif
</PRE>
<P>
But this doesn't work. The C preprocessor is a glorified text reformatter:
it knows practically nothing about C. However, there <I>is</I> a way to write this
as a compile time assertion (and moving any error trap to an earlier phase
is a Good Thing)
</P>
<H1>{ yourself }</H1>
<PRE class="fragment">
#define COMPILE_TIME_ASSERT(expr) \
char constraint[expr]
COMPILE_TIME_ASSERT(sizeof(int) * CHAR_BIT >= 32);
</PRE>
<P>
What's is going on here? Well, the example preprocesses to...
</P>
<PRE class="fragment">
char constraint[sizeof(int) * CHAR_BIT >= 32];
</PRE>
<P>
If the expression is true, (an <SPAN CLASS="CODE">int</SPAN> is at least 32 bits),
the expression
will have a value of one, and <SPAN CLASS="CODE">constraint</SPAN> will be an array
of one char.
If the assertion is false, (an <SPAN CLASS="CODE">int</SPAN> is less than 32 bits),
the expression
will have a value of zero, and <SPAN CLASS="CODE">constraint</SPAN> will be an
empty array. That's
illegal, and you'll get a compile time error. Viola, a compile time assertion :-)
You can use it inside and outside a function but you can't use it twice in
the same function, as you end up with a duplicate definition.
To solve that problem you could resort to some convoluted macro trickery:
</P>
<PRE class="fragment">
#define COMPILE_TIME_ASSERT(expr) char UNIQUE_NAME[expr]
#define UNIQUE_NAME MAKE_NAME(__LINE__)
#define MAKE_NAME(line) MAKE_NAME2(line)
#define MAKE_NAME2(line) constraint_ ## line
</PRE>
<P>
But this is pretty horrible. Also, you will probably get warnings about
unused variables. Take a step back for a moment and think about why
it works at all. It's because you have to specify the size of an array as
a compile time constant. The formal grammar of a direct-declarator
tells you this. Let's look at some bits of grammar more closely:
<P>
<H1>Constrained arrays</H1>
<PRE> direct-declarator:
identifier
( declarator )
direct-declarator [ constant-expression opt ]
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-list opt )
</PRE>
<P>
I just piggy backed on this, using the constraint that the value of the
constant expression cannot (in this context) be zero. A natural question
(to the curious) is are there other parts of the formal grammar that
require a constant expression. The answer, of course, is yes.
<H1>Constrained enums</H1>
<PRE> enumerator:
enumeration-constant
enumeration-constant = constant-expression
</PRE>
<P>
However, I can't use this because there are no useful constraints in this context.
</P>
<H1>Constrained bit-fields</H1>
<PRE> struct-declarator:
declarator
declarator opt : constant-expression
</PRE>
<P>
Reading the constraints of a bit field I see that if the width of a bit-field
is zero the declaration cannot have a declarator. In other words this is legal...
</P>
<PRE>
struct x { unsigned int : 0; };
</PRE>
<P>
but this is not...
</P>
<PRE>
struct x { unsigned int bf : 0; };
</PRE>
<P>
This suggests another way to create a compile time assertion
</P>
<PRE class="fragment">
#define COMPILE_TIME_ASSERT(expr) \
struct x { unsigned int bf : expr; }
COMPILE_TIME_ASSERT(sizeof(int) * CHAR_BIT >= 32);
</PRE>
<P>
Trying this we again get duplicate definitions, not of a variable
this time, but of the type <SPAN CLASS="CODE">struct x</SPAN>. However we can fix
this by creating an anonymous struct:
</P>
<PRE class="fragment">
#define COMPILE_TIME_ASSERT(expr) \
struct { unsigned int bf : expr; }
</PRE>
<P>
This works. However, now you'll probably get warnings about the
unused untagged struct.
</P>
There is one last bit of grammar that
uses a constant-expression.
<P>
<H1>Constrained switch</H1>
<PRE> labelled-statement:
identifier : statement
case constant-expression : statement
default : statement
</PRE>
<P>
It's well known that you can't have two <SPAN CLASS="CODE">case</SPAN> labels with the
same constant. The following will not compile...
</P>
<PRE class="fragment">
switch (0)
{
case 0:
case 0:;
}
</PRE>
So, here's yet another way to create a compile time assertion. This time we
don't create a dummy variable, or a dummy type, but a dummy statement. A
dummy <SPAN CLASS="CODE">switch</SPAN> statement:
<PRE class="fragment">
#define COMPILE_TIME_ASSERT(pred) \
switch(0){case 0:case pred:;}
COMPILE_TIME_ASSERT(sizeof(int) * CHAR_BIT >= 32);
</PRE>
<P>
If pred evaluates to true (i.e., 1) then the <SPAN CLASS="CODE">case</SPAN>
labels will be 0 and 1.
Different; Ok. If pred evaluates to false (i.e., 0) then the
<SPAN CLASS="CODE">case</SPAN> labels
will be 0 and 0. The same; Compile time error. Viola. However, a
<SPAN CLASS="CODE">switch</SPAN>
statement cannot exist in the global scope. So the last piece of the puzzle
is to put the compile time assertions inside a function.
</P>
<PRE class="fragment">
#include <limits.h>
#define COMPILE_TIME_ASSERT(pred) \
switch(0){case 0:case pred:;}
#define ASSERT_MIN_BITSIZE(type, size) \
COMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size)
#define ASSERT_EXACT_BITSIZE(type, size) \
COMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT == size)
void compile_time_assertions(void)
{
ASSERT_MIN_BITSIZE(char, 8);
ASSERT_MIN_BITSIZE(int, 16);
ASSERT_EXACT_BITSIZE(long, 32);
}
</PRE>
</body>
</html>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-45164466498347227762017-07-05T06:00:00.003+00:002020-04-04T15:09:24.191+00:00C# struct/class differences
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="author" content="Jon Jagger" />
<link rel="stylesheet" href="csharpArticle.css"></link>
<style media="screen" type="text/css">
body.hook
{
font-family: "Times", serif;
background: white;
margin-left: 3.0em;
margin-right: 0.5em;
}
h1
{
/*border-top: thick solid gray;
padding: 0.5em 0em 0em 0em; */
color: #993222;
font-style: normal;
font-weight: normal;
font-family: Georgia, Utopia, 'Palatino Linotype', Palatino, serif;
}
p
{
font-size: 1.0em;
font-family: "Times", serif;
font-weight: 100;
}
pre.fragment
{
font-size: 0.8em;
font-family: "Courier New", Courier, monospace;
font-weight: normal;
background-color: black;
padding: 1em 1em 1em 1em;
/* border-right: 1 solid gray; */
padding: 0.7em 0.7em 0.7em 0.7em;
margin: 1em 1em 1em 2em;
/* border-left: 3 solid gray;
border-right: 3 solid gray;
border-top: 3 solid gray;
border-bottom: 3 solid gray; */
}
span.emphasis
{
font-style: italic;
}
span.identifier
{
color: #cc9900;
}
span.keyword
{
color: khaki;
}
span.operator
{
color: white;
}
span.literal
{
color: green;
}
span.punctuator
{
color: white;
}
span.comment
{
color: lightgray;
}
</style>
</head>
<body class="hook">
<ul xmlns="">
<li><a href="#events are locked">Events are locked?</a></li>
<li><a href="#exist on stack or heap">Exist on stack or heap?</a></li>
<li><a href="#can cause garbage collection">Can cause garbage collection?</a></li>
<li><a href="#meaning of this">Meaning of this?</a></li>
<li><a href="#always has a default constructor">Always has a default constructor?</a></li>
<li><a href="#default construction triggers static constructor">Default construction triggers static construction?</a></li>
<li><a href="#can be null">Can be null?</a></li>
<li><a href="#use with the as operator">Use with the as operator?</a></li>
<li><a href="#can be locked">Can be locked?</a></li>
<li><a href="#can have a destructor">Can have a destructor?</a></li>
<li><a href="#default field layout">Default field layout?</a></li>
<li><a href="#can be a volatile field">Can be a volatile field?</a></li>
<li><a href="#can have synchronized methods">Can have synchronized methods?</a></li>
<li><a href="#can be pointed to">Can be pointed to?</a></li>
<li><a href="#can be stackallocd">Can be stackalloc'd?</a></li>
<li><a href="#can be sizeofd">Can be sizeof'd?</a></li>
<li><a href="#how to initialize fields">How to initialize fields?</a></li>
<li><a href="#inheritance differences">Inheritance differences?</a></li>
<li><a href="#equals behavior">Equals behavior</a></li>
</ul>
<a id="events are locked" name="events are locked" xmlns=""></a><h1 xmlns="">Events are locked?</h1>
<p xmlns="">
Events declared in a class have their += and -= access automatically locked via a lock(this) to make them
thread safe (static events are locked on the typeof the class). Events declared in a struct do <em>not</em>
have their += and -= access automatically locked. A lock(this) for a struct would not work since you can
only lock on a reference type expression.
</p>
<a id="exist on stack or heap" name="exist on stack or heap" xmlns=""></a><h1 xmlns="">Exist on stack or heap?</h1>
<p xmlns="">
Value type local instances are allocated on the stack.
Reference type local instances are allocated on the heap.
</p>
<a id="can cause garbage collection" name="can cause garbage collection" xmlns=""></a><h1 xmlns="">Can cause garbage collection?</h1>
<p xmlns="">
Creating a struct instance cannot cause a garbage collection
(unless the constructor directly or indirectly creates
a reference type instance) whereas creating a reference type
instance can cause garbage collection.
</p>
<a id="meaning of this" name="meaning of this" xmlns=""></a><h1 xmlns="">Meaning of this?</h1>
<p xmlns="">
In a class, <span class="emphasis" title="emphasis">this</span> is classified as a value, and thus cannot appear on the
left hand side of an assignment, or be used as a ref/out parameter. For example:
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="keyword">public</span> <span class="keyword">void</span> <span class="identifier">Method</span><span class="punctuator">(</span><span class="identifier">Indirect</span> <span class="identifier">that</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">RefParameter</span><span class="punctuator">(</span><span class="keyword">ref</span> <span class="keyword">this</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="identifier">OutParameter</span><span class="punctuator">(</span><span class="keyword">out</span> <span class="keyword">this</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="keyword">this</span> <span class="operator">=</span> <span class="identifier">that</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="punctuator">}</span>
<span class="comment">//...</span>
<span class="punctuator">}</span></pre>
In a struct, <span class="emphasis" title="emphasis">this</span> is classified as an out parameter in a constructor and as a ref parameter
in all other function members. Thus it is possible to modify the entire structure by
assigning to this or passing this as a ref/out parameter. For example:
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="keyword">public</span> <span class="keyword">void</span> <span class="identifier">Reassign</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">that</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">RefParameter</span><span class="punctuator">(</span><span class="keyword">ref</span> <span class="keyword">this</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="identifier">OutParameter</span><span class="punctuator">(</span><span class="keyword">out</span> <span class="keyword">this</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="keyword">this</span> <span class="operator">=</span> <span class="identifier">that</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span>
<span class="comment">//...</span>
<span class="punctuator">}</span></pre>
Furthermore, you can reassign a whole struct even when the
struct contains readonly fields!
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="keyword">int</span> <span class="identifier">value</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Field</span> <span class="operator">=</span> <span class="identifier">value</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">public</span> <span class="keyword">void</span> <span class="identifier">Reassign</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">that</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">RefParameter</span><span class="punctuator">(</span><span class="keyword">ref</span> <span class="keyword">this</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="identifier">OutParameter</span><span class="punctuator">(</span><span class="keyword">out</span> <span class="keyword">this</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="keyword">this</span> <span class="operator">=</span> <span class="identifier">that</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span>
<span class="keyword">public</span> <span class="keyword">readonly</span> <span class="keyword">int</span> <span class="identifier">Field</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Show</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="literal">42</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">s</span><span class="operator">.</span><span class="identifier">Field</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// writes 42</span>
<span class="identifier">s</span><span class="operator">.</span><span class="identifier">Reassign</span><span class="punctuator">(</span><span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="literal">24</span><span class="punctuator">)</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">s</span><span class="operator">.</span><span class="identifier">Field</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// writes 24</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
Note however that when you call a method on a readonly value-type field, the method
call is made on a <span class="emphasis" title="emphasis">copy</span> of the field.
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">// as above</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Caller</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="keyword">void</span> <span class="identifier">Method</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">d</span><span class="operator">.</span><span class="identifier">Field</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// writes 42</span>
<span class="identifier">d</span><span class="operator">.</span><span class="identifier">Reassign</span><span class="punctuator">(</span><span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="literal">24</span><span class="punctuator">)</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">d</span><span class="operator">.</span><span class="identifier">Field</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// writes 42!</span>
<span class="punctuator">}</span>
<span class="keyword">private</span> <span class="keyword">readonly</span> <span class="identifier">Direct</span> <span class="identifier">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="literal">42</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Show</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Caller</span> <span class="identifier">c</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Caller</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">c</span><span class="operator">.</span><span class="identifier">Method</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
</p>
<a id="always has a default constructor" name="always has a default constructor" xmlns=""></a><h1 xmlns="">Always have a default constructor?</h1>
<p xmlns="">
A struct <span class="emphasis" title="emphasis">always</span> has a built-in public default constructor.
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">DefaultConstructor</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Eg</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">yes</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// always compiles ok</span>
<span class="identifier">InDirect</span> <span class="identifier">maybe</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">InDirect</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles if c'tor exists and is accessible</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
This means that a struct is <span class="emphasis" title="emphasis">always</span>
instantiable whereas a class might not be since all its
constructors could be private.
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">NonInstantiable</span>
<span class="punctuator">{</span>
<span class="keyword">private</span> <span class="identifier">NonInstantiable</span><span class="punctuator">(</span><span class="punctuator">)</span> <span class="comment">// ok</span>
<span class="punctuator">{</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">private</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="punctuator">)</span> <span class="comment">// compile-time error</span>
<span class="punctuator">{</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
</p>
<a id="default construction triggers static constructor" name="default construction triggers static constructor" xmlns=""></a><h1 xmlns="">Default construction triggers static constructor?</h1>
<p xmlns="">
A structs static constructor is <span class="emphasis" title="emphasis">not</span> triggered by calling
the structs default constructor. It is for a class.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="literal">"This is not written"</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">NotTriggered</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">local</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
<a id="can be null" name="can be null" xmlns=""></a><h1 xmlns="">Can be null?</h1>
<p xmlns="">
A struct instance cannot be null.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">class</span> <span class="identifier">Nullness</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Eg</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">s</span><span class="punctuator">,</span> <span class="identifier">Indirect</span> <span class="identifier">c</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">if</span> <span class="punctuator">(</span><span class="identifier">s</span> <span class="operator">==</span> <span class="keyword">null</span><span class="punctuator">)</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="comment">// compile-time error</span>
<span class="keyword">if</span> <span class="punctuator">(</span><span class="identifier">c</span> <span class="operator">==</span> <span class="keyword">null</span><span class="punctuator">)</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
<a id="use with the as operator" name="use with the as operator" xmlns=""></a><h1 xmlns="">Use with the as operator?</h1>
<p xmlns="">
A struct type cannot be the right hand side operand of the as operator.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">class</span> <span class="identifier">Fragment</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Eg</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">s</span><span class="punctuator">,</span> <span class="identifier">Indirect</span> <span class="identifier">c</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">no</span> <span class="operator">=</span> <span class="identifier">s</span> <span class="keyword">as</span> <span class="identifier">Direct</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="identifier">InDirect</span> <span class="identifier">yes</span> <span class="operator">=</span> <span class="identifier">c</span> <span class="keyword">as</span> <span class="identifier">InDirect</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
<a id="can be locked" name="can be locked" xmlns=""></a><h1 xmlns="">Can be locked?</h1>
<p xmlns="">
A struct type expression cannot be the operand of a lock statement.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">class</span> <span class="identifier">LockStatement</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Eg</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">s</span><span class="punctuator">,</span> <span class="identifier">InDirect</span> <span class="identifier">c</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">lock</span><span class="punctuator">(</span><span class="identifier">s</span><span class="punctuator">)</span> <span class="punctuator">{</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="punctuator">}</span> <span class="comment">// compile-time error</span>
<span class="keyword">lock</span><span class="punctuator">(</span><span class="identifier">c</span><span class="punctuator">)</span> <span class="punctuator">{</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="punctuator">}</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
<a id="can have a destructor" name="can have a destructor" xmlns=""></a><h1 xmlns="">Can have a destructor?</h1>
<p xmlns="">
A struct cannot have a destructor. A destructor is just
an override of object.Finalize in disguise, and structs, being value types,
are not subject to garbage collection.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="operator">~</span><span class="identifier">Direct</span><span class="punctuator">(</span><span class="punctuator">)</span> <span class="punctuator">{</span><span class="punctuator">}</span> <span class="comment">// compile-time error</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">InDirect</span>
<span class="punctuator">{</span>
<span class="operator">~</span><span class="identifier">InDirect</span><span class="punctuator">(</span><span class="punctuator">)</span> <span class="punctuator">{</span><span class="punctuator">}</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span></pre>
<p xmlns="">
And the CIL for ~Indirect() looks like this:
</p>
<pre class="fragment" xmlns="">
<span class="operator">.</span><span class="identifier">method</span> <span class="identifier">family</span> <span class="identifier">hidebysig</span> <span class="keyword">virtual</span> <span class="identifier">instance</span> <span class="keyword">void</span>
<span class="identifier">Finalize</span><span class="punctuator">(</span><span class="punctuator">)</span> <span class="identifier">cil</span> <span class="identifier">managed</span>
<span class="punctuator">{</span>
<span class="comment">// ...</span>
<span class="punctuator">}</span> <span class="comment">// end of method Indirect::Finalize</span></pre>
<a id="default field layout" name="default field layout" xmlns=""></a><h1 xmlns="">Default field layout?</h1>
<p xmlns="">
The default [StructLayout] attribute (which lives in the
System.Runtime.InteropServices namespace) for a struct is LayoutKind.Sequential
whereas the default StructLayout for a class is LayoutKind.Auto. (And yes,
despite its name you can tag a class with the StructLayout attribute.)
In other words the CIL for this:
</p>
<pre class="fragment" xmlns="">
<span class="keyword">public</span> <span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span></pre>
<p xmlns="">
looks like this:
</p>
<pre class="fragment" xmlns="">
<span class="operator">.</span><span class="keyword">class</span> <span class="keyword">public</span> <span class="identifier">sequential</span> <span class="identifier">ansi</span> <span class="keyword">sealed</span> <span class="identifier">beforefieldinit</span> <span class="identifier">Direct</span>
<span class="identifier">extends</span> <span class="punctuator">[</span><span class="identifier">mscorlib</span><span class="punctuator">]</span><span class="identifier">System</span><span class="operator">.</span><span class="identifier">ValueType</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span></pre>
<p xmlns="">
whereas the CIL for this:
</p>
<pre class="fragment" xmlns="">
<span class="keyword">public</span> <span class="keyword">sealed</span> <span class="keyword">class</span> <span class="identifier">InDirect</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span></pre>
<p xmlns="">
looks like this:
</p>
<pre class="fragment" xmlns="">
<span class="operator">.</span><span class="keyword">class</span> <span class="keyword">public</span> <span class="identifier">auto</span> <span class="identifier">ansi</span> <span class="keyword">sealed</span> <span class="identifier">beforefieldinit</span> <span class="identifier">Indirect</span>
<span class="identifier">extends</span> <span class="punctuator">[</span><span class="identifier">mscorlib</span><span class="punctuator">]</span><span class="identifier">System</span><span class="operator">.</span><span class="identifier">Object</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span></pre>
<a id="can be a volatile field" name="can be a volatile field" xmlns=""></a><h1 xmlns="">Can be a volatile field?</h1>
<p xmlns="">
You can't declare a user-defined struct type as a volatile field
but you can declare a user-defined class type as a volatile field.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">class</span> <span class="identifier">Bad</span>
<span class="punctuator">{</span>
<span class="keyword">private</span> <span class="identifier">volatile</span> <span class="identifier">Direct</span> <span class="identifier">field</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Good</span>
<span class="punctuator">{</span>
<span class="keyword">private</span> <span class="identifier">volatile</span> <span class="identifier">Indirect</span> <span class="identifier">field</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span></pre>
<a id="can have synchronized methods" name="can have synchronized methods" xmlns=""></a><h1 xmlns="">Can have synchronized methods?</h1>
<p xmlns="">
You can't use the [MethodImpl(MethodImplOptions.Synchronized)] attribute
on methods of a struct type (if you call the method you get a runtime TypeLoadException) whereas you can use
the [MethodImpl(MethodImplOptions.Synchronized)] attribute on methods of a class type.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">using</span> <span class="identifier">System</span><span class="operator">.</span><span class="identifier">Runtime</span><span class="operator">.</span><span class="identifier">CompilerServices</span><span class="punctuator">;</span>
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="punctuator">[</span><span class="identifier">MethodImpl</span><span class="punctuator">(</span><span class="identifier">MethodImplOptions</span><span class="operator">.</span><span class="identifier">Synchronized</span><span class="punctuator">)</span><span class="punctuator">]</span> <span class="comment">// compiles and runs ok</span>
<span class="keyword">public</span> <span class="keyword">void</span> <span class="identifier">Method</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="punctuator">[</span><span class="identifier">MethodImpl</span><span class="punctuator">(</span><span class="identifier">MethodImplOptions</span><span class="operator">.</span><span class="identifier">Synchronized</span><span class="punctuator">)</span><span class="punctuator">]</span> <span class="comment">// compiles ok, runtime TypeLoadException</span>
<span class="keyword">public</span> <span class="keyword">void</span> <span class="identifier">Method</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
<a id="can be pointed to" name="can be pointed to" xmlns=""></a><h1 xmlns="">Can be pointed to?</h1>
<p xmlns="">
Clause 25.2 of the C# standard defines an unmanaged type as any type that isn't
a reference type and doesn't contain reference-type fields at any level of
nesting. That is, one of the following:
<ul>
<li>Any simple value type (11.1.3, eg byte, int, long, double, bool, etc).</li>
<li>Any enum type.</li>
<li>Any pointer type.</li>
<li>Any user-defined struct-type that contains fields of unmanaged types only.</li>
</ul>
You can <span class="emphasis" title="emphasis">never</span> take the address of a instance of a type that is
not unmanaged (a fixed variable 25.3).
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">Bad</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Indirect</span> <span class="identifier">variable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="keyword">fixed</span><span class="punctuator">(</span><span class="identifier">Indirect</span> <span class="operator">*</span> <span class="identifier">ptr</span> <span class="operator">=</span> <span class="operator">&</span><span class="identifier">variable</span><span class="punctuator">)</span> <span class="comment">// compile-time error</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
If you want to fix an unmanaged instance you have to do so by fixing it through an
unmanaged field. For example:
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="keyword">int</span> <span class="identifier">fixHandle</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Bad</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Indirect</span> <span class="identifier">variable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="keyword">fixed</span><span class="punctuator">(</span><span class="keyword">int</span> <span class="operator">*</span> <span class="identifier">ptr</span> <span class="operator">=</span> <span class="operator">&</span><span class="identifier">variable</span><span class="operator">.</span><span class="identifier">fixHandle</span><span class="punctuator">)</span> <span class="comment">// compiles ok</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
In contrast, you can (nearly) always take the address of an unmanaged instance.
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">// no reference fields at any level of nesting</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">SimpleCase</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">variable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="operator">*</span> <span class="identifier">ptr</span> <span class="operator">=</span> <span class="operator">&</span><span class="identifier">variable</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="comment">//... </span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
However, you have to take the address inside a fixed statement if the variable is moveable
(subject to relocation by the garbage collector, see 25.3 and example above).
Also, you can never take the address of a volatile field.
</p>
<p xmlns="">
So, in summary, you can <span class="emphasis" title="emphasis">never</span> create a pointer to a class type but you
sometimes create a pointer to a struct type.
</p>
<a id="can be stackallocd" name="can be stackallocd" xmlns=""></a><h1 xmlns="">Can be stackalloc'd?</h1>
<p xmlns="">
You can only use stackalloc on unmanaged types. Hence you can never use stackalloc on
class types. For example:
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Bad</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="identifier">Indirect</span> <span class="operator">*</span> <span class="identifier">array</span> <span class="operator">=</span> <span class="keyword">stackalloc</span> <span class="identifier">Indirect</span><span class="punctuator">[</span><span class="literal">42</span><span class="punctuator">]</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
Where as you can use stackalloc on struct types that are unmanaged. For example:
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">// no reference fields at any level of nesting</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Good</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="operator">*</span> <span class="identifier">array</span> <span class="operator">=</span> <span class="keyword">stackalloc</span> <span class="identifier">Direct</span><span class="punctuator">[</span><span class="literal">42</span><span class="punctuator">]</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
</p>
<a id="can be sizeofd" name="can be sizeofd" xmlns=""></a><h1 xmlns="">Can be sizeof'd?</h1>
<p xmlns="">
You can only use sizeof on unmanaged types. Hence you can never use sizeof on
class types. For example:
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Bad</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="keyword">int</span> <span class="identifier">size</span> <span class="operator">=</span> <span class="keyword">sizeof</span><span class="punctuator">(</span><span class="identifier">Indirect</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
Where as you can use sizeof on struct types that are unmanaged. For example:
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">// no reference fields at any level of nesting</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Good</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">unsafe</span>
<span class="punctuator">{</span>
<span class="keyword">int</span> <span class="identifier">size</span> <span class="operator">=</span> <span class="keyword">sizeof</span><span class="punctuator">(</span><span class="identifier">Direct</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="comment">//...</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
</p>
<a id="how to initialize fields" name="how to initialize fields" xmlns=""></a><h1 xmlns="">How to initialize fields?</h1>
<p xmlns="">
The fields of a class have a default initialization to zero/false/null. The fields
of a struct have no default value.
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="keyword">int</span> <span class="identifier">Field</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="punctuator">}</span>
<span class="comment">//...</span>
<span class="keyword">public</span> <span class="keyword">int</span> <span class="identifier">Field</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Defaults</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">s</span><span class="punctuator">;</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">s</span><span class="operator">.</span><span class="identifier">Field</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="identifier">Indirect</span> <span class="identifier">c</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">c</span><span class="operator">.</span><span class="identifier">Field</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
</p>
<p xmlns="">
You can initialize fields in a class at their point of declaration. For example:
<pre class="fragment">
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="keyword">private</span> <span class="keyword">int</span> <span class="identifier">field</span> <span class="operator">=</span> <span class="literal">42</span><span class="punctuator">;</span>
<span class="punctuator">}</span></pre>
You can't do this for fields in a struct. For example:
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="comment">//...</span>
<span class="keyword">private</span> <span class="keyword">int</span> <span class="identifier">field</span> <span class="operator">=</span> <span class="literal">42</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="punctuator">}</span></pre>
Fields in a struct <span class="emphasis" title="emphasis">have</span> to be initialized in a constructor. For example:
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="keyword">int</span> <span class="identifier">value</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">field</span> <span class="operator">=</span> <span class="identifier">value</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="comment">//...</span>
<span class="keyword">private</span> <span class="keyword">int</span> <span class="identifier">field</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="punctuator">}</span></pre>
Also, the definite assignment rules of a struct are tracked on an individual
field basis. This means you can bypass initialization and "assign" the fields of
a struct one a time. For example:
<pre class="fragment">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="keyword">int</span> <span class="identifier">X</span><span class="punctuator">,</span> <span class="identifier">Y</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Example</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">d</span><span class="punctuator">;</span>
<span class="identifier">d</span><span class="operator">.</span><span class="identifier">X</span> <span class="operator">=</span> <span class="literal">42</span><span class="punctuator">;</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">d</span><span class="operator">.</span><span class="identifier">X</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compiles ok</span>
<span class="identifier">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span><span class="punctuator">(</span><span class="identifier">d</span><span class="operator">.</span><span class="identifier">Y</span><span class="punctuator">)</span><span class="punctuator">;</span> <span class="comment">// compile-time error</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
</p>
<a id="inheritance differences" name="inheritance differences" xmlns=""></a><h1 xmlns="">Inheritance differences?</h1>
<p xmlns="">
<ul>
<li>
a struct is implicitly sealed, a class isn't.
</li>
<li>
a struct can't be abstract, a class can.
</li>
<li>
a struct can't call : base() in its constructor whereas a class with no
explicit base class can.
</li>
<li>
a struct can't extend another class, a class can.
</li>
<li>
a struct can't declare protected members (eg fields, nested types) a class can.
</li>
<li>
a struct can't declare abstract function members, an abstract class can.
</li>
<li>
a struct can't declare virtual function members, a class can.
</li>
<li>
a struct can't declare sealed function members, a class can.
</li>
<li>
a struct can't declare override function members, a class can.
The one exception to this rule is that a struct can override the
virtual methods of System.Object, viz, Equals(), and GetHashCode(),
and ToString().
</li>
</ul>
</p>
<a id="equals behavior" name="equals behavior" xmlns=""></a><h1 xmlns="">Equals behavior?</h1>
<p xmlns="">
classes inherit Object.Equals which implements identity equality
whereas structs inherit ValueType.Equals which implements value equality.
</p>
<pre class="fragment" xmlns="">
<span class="keyword">using</span> <span class="identifier">System</span><span class="operator">.</span><span class="identifier">Diagnostics</span><span class="punctuator">;</span>
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="keyword">int</span> <span class="identifier">value</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">field</span> <span class="operator">=</span> <span class="identifier">value</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">private</span> <span class="keyword">int</span> <span class="identifier">field</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">Indirect</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="keyword">int</span> <span class="identifier">value</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">field</span> <span class="operator">=</span> <span class="identifier">value</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">private</span> <span class="keyword">int</span> <span class="identifier">field</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">class</span> <span class="identifier">EqualsBehavior</span>
<span class="punctuator">{</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">Main</span><span class="punctuator">(</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">Direct</span> <span class="identifier">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="literal">42</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Direct</span> <span class="identifier">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="literal">42</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Indirect</span> <span class="identifier">c1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="literal">42</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Indirect</span> <span class="identifier">c2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="identifier">Indirect</span><span class="punctuator">(</span><span class="literal">42</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="keyword">bool</span> <span class="identifier">structEquality</span> <span class="operator">=</span> <span class="identifier">s1</span><span class="operator">.</span><span class="identifier">Equals</span><span class="punctuator">(</span><span class="identifier">s2</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="keyword">bool</span> <span class="identifier">classIdentity</span> <span class="operator">=</span> <span class="operator">!</span><span class="identifier">c1</span><span class="operator">.</span><span class="identifier">Equals</span><span class="punctuator">(</span><span class="identifier">c2</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Debug</span><span class="operator">.</span><span class="identifier">Assert</span><span class="punctuator">(</span><span class="identifier">structEquality</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="identifier">Debug</span><span class="operator">.</span><span class="identifier">Assert</span><span class="punctuator">(</span><span class="identifier">classIdentity</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="punctuator">}</span></pre>
Overriding Equals for structs should be faster because it can avoid
reflection and boxing.
<pre class="fragment" xmlns="">
<span class="keyword">struct</span> <span class="identifier">Direct</span>
<span class="punctuator">{</span>
<span class="keyword">public</span> <span class="identifier">Direct</span><span class="punctuator">(</span><span class="keyword">int</span> <span class="identifier">value</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="identifier">field</span> <span class="operator">=</span> <span class="identifier">value</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">public</span> <span class="keyword">override</span> <span class="keyword">bool</span> <span class="identifier">Equals</span><span class="punctuator">(</span><span class="keyword">object</span> <span class="identifier">other</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">return</span> <span class="identifier">other</span> <span class="keyword">is</span> <span class="identifier">Direct</span> <span class="operator">&&</span> <span class="identifier">Equals</span><span class="punctuator">(</span><span class="punctuator">(</span><span class="identifier">Direct</span><span class="punctuator">)</span><span class="identifier">other</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="operator">==</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">lhs</span><span class="punctuator">,</span> <span class="identifier">Direct</span> <span class="identifier">rhs</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">return</span> <span class="identifier">lhs</span><span class="operator">.</span><span class="identifier">Equals</span><span class="punctuator">(</span><span class="identifier">rhs</span><span class="punctuator">)</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="comment">//... </span>
<span class="keyword">private</span> <span class="keyword">bool</span> <span class="identifier">Equals</span><span class="punctuator">(</span><span class="identifier">Direct</span> <span class="identifier">other</span><span class="punctuator">)</span>
<span class="punctuator">{</span>
<span class="keyword">return</span> <span class="identifier">field</span> <span class="operator">==</span> <span class="identifier">other</span><span class="operator">.</span><span class="identifier">field</span><span class="punctuator">;</span>
<span class="punctuator">}</span>
<span class="keyword">private</span> <span class="keyword">int</span> <span class="identifier">field</span><span class="punctuator">;</span>
<span class="punctuator">}</span></pre>
</body></html>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-12281617177231969732017-05-03T08:22:00.000+00:002017-05-03T08:22:29.503+00:00ACCU C++ Countdown Pub Quiz
<div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-625nx7tDPH4/WQcQUfWA84I/AAAAAAAAFGo/iJZ4hVvycyYB_q8jRldIk3ecJUvOTUVuwCLcB/s1600/Countdown1.png" imageanchor="1" style="clear: left; float: right; margin: 1em;"><img border="0" src="https://4.bp.blogspot.com/-625nx7tDPH4/WQcQUfWA84I/AAAAAAAAFGo/iJZ4hVvycyYB_q8jRldIk3ecJUvOTUVuwCLcB/s400/Countdown1.png" width="320" height="223" /></a></div>
The <a href="https://conference.accu.org/site/">ACCU conference</a> is one of the highlights of my year.
I ran a brand new session, a C++ Pub Quiz with an emphasis on fun and interaction,
based loosely on the popular UK TV game show
<a href="http://www.channel4.com/programmes/countdown">Countdown</a>.
<br/>
<br/>
In the TV version, contestants play individually and have 30 seconds to find the longest word
using only a small set of letters.
In this version, contestants play in teams, and have ~7 minutes to write the <em>smallest valid</em>
C++ program containing a small set of tokens.
<br/>
For example, if the tokens were:
<pre>catch -> [ ; -- foobar operator
</pre>
<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-YolT6sSODvY/WQcVa3fnnmI/AAAAAAAAFG4/QyIKsI8TJDE8eW7UAO_nA16I5eeP2fjtgCLcB/s1600/Countdown2.png" imageanchor="1" style="clear: left; float: right; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-YolT6sSODvY/WQcVa3fnnmI/AAAAAAAAFG4/QyIKsI8TJDE8eW7UAO_nA16I5eeP2fjtgCLcB/s320/Countdown2.png" width="320" height="223" /></a></div>
Then a winning program (53 character program) might be:
<pre>
class c {
c operator->(){
foobar: try{
}
catch(c x[]){
x--;
}
}
};
</pre>
<br/>
We used <a href="http://cyber-dojo.org">cyber-dojo</a> with some custom C++17 start-points
which automatically told you your program's size and score.
The rules were as follows:
<ul>
<li>The judges decision was final</li>
<li>Only non-whitespace characters were counted</li>
<li>Programs had to compile</li>
<li>Warnings were allowed</li>
<li>Extra tokens were allowed</li>
<li>Each token has to be a single whole token. For example the . token had to be the member access token; you could not use ... ellipsis or 4.2 floating point literal</li>
</ul>
<br/>
<br/>
The winners and the tokens were as follows (<a href="http://cyber-dojo.org/setup_custom_start_point/show">can you find smaller programs</a>?)
<hr/>
Round 1: snakes, 75 character program, <code>
dynamic_cast
snafu
+=
return
switch
final
</code>
<hr/>
Round 2: wolves,koalas tied, 54 character program, <code>
catch
;
foobar
operator
--
[
</code>
<hr/>
Round 3: frogs, 62 character program, <code>
else
~
default
->
using
foobar
0x4b
</code>
<hr/>
Round 4: tigers, 44 character program, <code>
string
include
for
auto
template
42
</code>
<hr/>
Round 5: pandas, tigers tied, 82 character program, <code>
virtual
typename
x
reinterpret_cast
static_cast
30ul
</code>
<hr/>
Round 6: wolves, 64 character program, <code>
constexpr
override
goto
wibble
.
this
</code>
<hr/>
The raccoons and lions won the conundrum rounds.<br/>
<br/>
The result was <em>very</em> close.<br/>
In 3rd place snakes with 481 points.<br/>
In 2nd place alligators with 488 points.<br/>
In 1st place tigers with 495 points.<br/>
A big thank you to my co-presenter Rob Chatley,
to all the contestants for being such good sports,
and to Bloomberg for sponsoring the Quiz.
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-70849617406866161492017-04-18T15:54:00.000+00:002017-04-18T15:54:40.437+00:00Docker in Action
<div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-NYdPhao9qJw/WPY2f1r70AI/AAAAAAAAFF4/BlB8uFf6yOAUjikYkcFrLqnRN8qYGhUSQCLcB/s1600/DockerInAction.jpeg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://2.bp.blogspot.com/-NYdPhao9qJw/WPY2f1r70AI/AAAAAAAAFF4/BlB8uFf6yOAUjikYkcFrLqnRN8qYGhUSQCLcB/s320/DockerInAction.jpeg" width="255" height="320" /></a></div>
is an excellent book by Jeff Nickoloff.
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to
quote from a few pages.
<br />
<br />
<blockquote>
The docker stop command tells the program with PID #1 in the container to halt.
</blockquote>
<blockquote>
Like most Docker isolation features, you can optionally create containers
<em>without</em> their own PID namespace.
</blockquote>
<blockquote>
If a situation arises where the name of a container needs to <a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">change</a>, you
can always rename the container with the [docker rename] command.
</blockquote>
<blockquote>
There are two types of volume... The first type of volume is a bind mount.
Bind mount volumes use any user-specified directory or file on the host
operating system. The second type is a managed volume. Managed volumes use
locations that are created by the Docker daemon in space controlled by
the daemon, called Docker managed space.
</blockquote>
<blockquote>
When you mount a volume on a container file system, it replaces the content
that the image provides at that location.
</blockquote>
<blockquote>
You can copy volumes directly or transitively.
</blockquote>
<blockquote>
The third situation where you can't use --volumes-from is if you need to <a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">change</a>
the write permission of a volume.
</blockquote>
<blockquote>
Remember that containers maintain IP address leases only when they're running.
So if a container is stopped or restarted, it will lose its IP lease and
any linked containers will have stale data.
</blockquote>
<blockquote>
It's not common to see minimum requirements published with open source software.
</blockquote>
<blockquote>
CPU shares differ from memory limits in that they're enforced only when there
is contention for time on the CPU.
</blockquote>
<blockquote>
The union file system on your computer may have a layer count limit. These limits
vary, but a limit of 42 layers is common on computers that use the AUFS system.
</blockquote>
<blockquote>
The most curious thing about this Dockerfile is that the ENTRYPOINT is set to a file
that doesn't exist.
</blockquote>
<blockquote>
You have no way way to specify a bind-mount volume or read-only volume at
image build time.
</blockquote>
<blockquote>
The examples in this chapter use the cURL command-line tool. Because this
is a book about Docker, you should use cURL from inside a container.
</blockquote>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-76265142301217709412017-03-19T11:31:00.000+00:002017-03-21T20:23:41.424+00:00Siddhartha
<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-vVzrawziWDI/WM5q0n0HMmI/AAAAAAAAFEg/KfJZrSJuwMo9za1mGX7VjE87F4p6hkmbgCLcB/s1600/Siddhartha.jpeg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://1.bp.blogspot.com/-vVzrawziWDI/WM5q0n0HMmI/AAAAAAAAFEg/KfJZrSJuwMo9za1mGX7VjE87F4p6hkmbgCLcB/s320/Siddhartha.jpeg" width="210" height="320" /></a></div>
is an excellent book by Herman Hesse.
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to
quote from a few pages.
<br />
<br />
<blockquote>
He learned more from the river than Vasudeva could teach him.
He learned from it continually. Above all, he learned from it
how to <a href="http://jonjagger.blogspot.co.uk/2014/01/listening.html">listen</a>,
to listen with a still heart, with a waiting,
open soul, without passion, without desire, without judgement,
without opinions.
</blockquote>
<blockquote>
It also happened that curious people came along, who had been told
that two wise men, magicians or holy men lived at the ferry.
The curious ones asked many questions but they received no replies,
and they found neither magicians nor wise men. They only found two
friendly old men, who appeared to be mute, rather odd and stupid.
And the curious ones laughed and said how foolish and credible
people were to spread such wild rumours.
</blockquote>
<blockquote>
Is it not perhaps a mistake on your part not to be strict with
him, not to punish him? Do you not chain him with your love?
Do you not shame him daily with your goodness and patience
and make it still more difficult for him?
</blockquote>
<blockquote>
Within Siddhartha there slowly grew and ripened the knowledge
of what wisdom really was and the goal of his long seeking.
It was nothing but a preparation of the soul, a secret art
of thinking, feeling and breathing thoughts of unity at every
moment of life.
</blockquote>
<blockquote>
From that hour Siddhartha ceased to fight against his destiny.
There shone in his face the serenity of knowledge, of one who
is no longer confronted with conflict of desires, who has found
salvation, who is in harmony with the streams of events, with
the stream of life, full of sympathy and compassion, surrendering
himself to the stream, belonging to the unity of all things.
</blockquote>
<blockquote>
In every truth the opposite is equally true. For example, a
truth can only be expressed and enveloped in words if it is
one-sided. Everything that is thought and expressed in words
is one-sided, only half the truth; it lacks totality,
completeness, unity.
</blockquote>
<blockquote>
The sinner is not on the way to a Buddha-like state; he is
not evolving, although our thinking cannot conceive things
otherwise. No, the potential Buddha already exists in the sinner;
his future is already there. The potential Buddha must be
recognized in him, in you, in everybody. The world, Govinda,
is not imperfect or slowly evolving along a path to perfection.
No, it is perfect at every moment; every sin already carries
grace within it, all small children are potential old men,
all sucklings have death within them, all dying people - eternal life.
</blockquote>
<blockquote>
In order to <a href="http://jonjagger.blogspot.co.uk/2013/10/learning.html">learn</a>
to love the world, and no longer compare it with
some kind of desired imaginary world, some imaginary vision of
perfection, but to leave it as it is, to love it and be glad
to belong to it.
</blockquote>
<blockquote>
It may be a thought, but I confess, my friend, that I do not
differentiate very much between thoughts and words. Quite
frankly, I do no attach great importance to thoughts either.
I attach more importance to things.
</blockquote>
<blockquote>
I think it is only important to love the world, not to despise
it, not for us to hate others, but to be able to regard the
world and ourselves and all beings with love, admiration
and respect.
</blockquote>
<blockquote>
The thing to me is of greater importance than the words;
his deeds and life and more important to me than his
opinions. Not in speech or thought do I regard him as a great
man, but in his deeds and life.
</blockquote>
<blockquote>
Uncontrollable tears trickled down his old face. He was
overwhelmed by a feeling of great love, of the most
humble veneration.
</blockquote>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-86735551393862061082017-03-11T08:51:00.001+00:002017-03-11T09:11:59.870+00:00The Hidden Life of Trees
<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-zaMEL9IzztM/WMO5Oo2fmtI/AAAAAAAAFEM/tul8VjSeiOAnxsuioqDuudKYIb4ls0QngCLcB/s1600/HiddenLifeOfTrees.jpeg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://1.bp.blogspot.com/-zaMEL9IzztM/WMO5Oo2fmtI/AAAAAAAAFEM/tul8VjSeiOAnxsuioqDuudKYIb4ls0QngCLcB/s320/HiddenLifeOfTrees.jpeg" width="175" /></a></div>
is an excellent book by Peter Wohlleben
(isbn 1771642483).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to
quote from a few pages. <br />
<br />
<blockquote>
In forked trees, at a certain point, two main shoots form, they continue
to grow alongside each other. Each side of the fork creates its own crown,
so in a heavy wind, both sides sway back and forth in different directions,
putting a great strain on the trunk where the two parted company. ...
The fork always breaks at its narrowest point, where the two sides diverge.
</blockquote>
<blockquote>
The process of <a href="http://jonjagger.blogspot.no/2013/10/learning.html">learning</a>
stability is triggered by painful micro-tears that
occur when the trees bend way over in the wind, first in one direction
and then in the other. Wherever it hurts, that's where the tree must
strengthen its support structure. ...
The thickness and stability of the trunk, therefore, builds up as the
tree responds to a series of aches and pains.
</blockquote>
<blockquote>
There is a honey fungus in Switzerland that covers almost 120 acres and
is about a thousand years old. Another in Oregon is estimated to be
2,400 years old, extends for 2,000 acres, and weighs 660 tons. That
makes fungi the largest known living organism in the world.
</blockquote>
<blockquote>
You find twice the amount of life-giving nitrogen and phosphorus
in plants that cooperate with fungal partners than in plants that
tap the soil with the roots alone.
</blockquote>
<blockquote>
Diversity provides security for ancient forests.
</blockquote>
<blockquote>
There are more life-forms in a handful of forest soil than there are
people on the planet.
</blockquote>
<blockquote>
As foresters like to say, the forest creates its own ideal habitat.
</blockquote>
<blockquote>
Commercial forest monocultures also encourage the mass reproduction of
butterflies and moths, such as nun moths and pine loopers.
What usually happens is that viral illnesses crop up towards the end of the
cycle and populations crash.
</blockquote>
<blockquote>
The storms pummel mature trunks with forces equivalent to a weight of
approximately 220 tons. Any tree unprepared for the onslaught can't
withstand the pressure and falls over. But deciduous trees are well
prepared. To be more aerodynamic they cast off all their solar panels.
And so a huge surface area of 1,200 square yards disappears and sinks
to the forest floor. This is the equivalent of a sailboat with a 130-foot
mast dropping a 100-by-130 foot mainsail.
</blockquote>
<blockquote>
Why do tree grow into pipes in the first place?...
What was attracting them was loose soil that had not been fully
compacted after construction. Here the roots found room to breathe
and grow. It was only incidentally that they penetrated the seals
between individual sections of pipe and eventually ran riot inside them.
</blockquote>
<blockquote>
Sometimes, especially in cold winters, the old wounds can act up again.
Then a crack like a rifle shot echoes through the forest and the trunk
splits open along the old injury. This is caused by differences in
tension in the frozen wood, because the wood in trees with a history of
injury varies greatly in density.
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-53838788630708931952017-01-24T13:31:00.001+00:002017-01-24T13:31:03.096+00:00the DevOps Handbook
<div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-TZV2JueWnQc/WIdUQdOwfjI/AAAAAAAAE3c/_JI1SL3MmA8KKdcIVExvUMdsYTl5qWq5ACLcB/s1600/TheDevOpsHandbook.jpeg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://4.bp.blogspot.com/-TZV2JueWnQc/WIdUQdOwfjI/AAAAAAAAE3c/_JI1SL3MmA8KKdcIVExvUMdsYTl5qWq5ACLcB/s400/TheDevOpsHandbook.jpeg" width="132" height="200" /></a></div>
is an excellent book by Gene Kim, Jez Humble, Patrick Debois, and John Willis
(isbn 978-1-942788-00-3).
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to
quote from a few pages. <br />
<br />
<blockquote>
Make infrastructure easier to rebuild than to repair.
</blockquote>
<blockquote>
The average age of a Netflix AWS instance is twenty-four days.
</blockquote>
<blockquote>
Interrupting technology workers is easy, because the consequences are <a href="http://jonjagger.blogspot.co.uk/2013/11/visibility.html">invisible</a>
to almost everyone.
</blockquote>
<blockquote>
In <a href="http://jonjagger.blogspot.co.uk/2011/04/complexity.html">complex</a>
<a href="http://jonjagger.blogspot.co.uk/2013/10/systems.html">systems</a>,
adding more inspection steps and approval processes
actually increases the likelihood of future failures.
</blockquote>
<blockquote>
Over the following year, they eliminated <a href="http://jonjagger.blogspot.co.uk/2013/12/testing.html">testing</a>
as a separate phase of work, instead integrating it into everyone's daily work. They doubled
the features being delivered per month and halved the number of defects.
</blockquote>
<blockquote>
<a href="http://jonjagger.blogspot.co.uk/2010/05/bureaucracy.html">Bureaucracies</a>
are incredibly resilient and are designed to survive adverse
conditions - one can remove half the bureaucrats, and the process will
still survive.
</blockquote>
<blockquote>
When we have a tightly coupled architecture, small
<a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">changes</a>
can result in large scale failure.
</blockquote>
<blockquote>
Our deployment pipeline infrastructure becomes as foundational for
our development processes as our version control infrastructure.
</blockquote>
<blockquote>
If we find that unit or acceptance
<a href="http://jonjagger.blogspot.co.uk/2013/12/testing.html">tests</a> are too difficult
and expensive to write and maintain, it's likely that we have an
architecture that is too tightly coupled.
</blockquote>
<blockquote>
Any successful product or
<a href="http://jonjagger.blogspot.co.uk/2013/12/organization.html">organization</a>
will necessarily evolve over its life cycle... eBay and Google are each on their fifth entire
rewrite of their architecture from top to bottom.
</blockquote>
<blockquote>
... which can lead to the unfortunate metric of mean time until declared innocent.
</blockquote>
<blockquote>
The principle of small batch sizes also applies to code
<a href="http://jonjagger.blogspot.co.uk/2013/11/observing.html">reviews</a>.
</blockquote>
<blockquote>
80% of MTTR (mean time to recovery) is spent trying to determine what
<a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">changed</a>.
</blockquote>
<blockquote>
High performing DevOps <a href="http://jonjagger.blogspot.co.uk/2013/12/organization.html">organizations</a>
will fail and make mistakes more often... If high performers are performing thirty times more frequently but
with only half the <a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">change</a> failure rate,
they're obviously having more failures. [Roy Rapoport, Netflix]
</blockquote>
<blockquote>
Spiders repair rips and tears in the web as they occur, not waiting for
the failures to accumulate. [Dr Steven Spear]
</blockquote>
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-91401110742940317142017-01-19T12:07:00.002+00:002017-01-24T14:54:03.868+00:00NDC Does C++ Countdown!
<div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-bYVE881mi1g/WIdqVIjlYbI/AAAAAAAAE3s/f3-n_Bzz_A8ELa1N3dp2G9HopsQGKGJrwCLcB/s1600/countdown.jpeg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://3.bp.blogspot.com/-bYVE881mi1g/WIdqVIjlYbI/AAAAAAAAE3s/f3-n_Bzz_A8ELa1N3dp2G9HopsQGKGJrwCLcB/s320/countdown.jpeg" width="320" height="175" /></a></div>
It was my pleasure to run a small workshop style session at the
excellent <a href="http://ndc-london.com/">NDC-London</a> conference.
I ran a fun C++ game which parodies the popular UK TV gameshow <a href="https://en.wikipedia.org/wiki/Countdown_(game_show)">Countdown</a>.
<ul>
<li>
In the TV version contestants take turns picking 9 random vowels/consonants
and finding the <em>longest</em> word in 30 seconds.
</li>
<li>
In my version contestants take turns picking 7 random tokens from 5 categories:
(keywords, identifiers, operators, punctuators, literals)
and writing the <em>shortest</em> C++ program using <em>all</em> 7 tokens in 8 minutes.
</li>
</ul>
Contestants write their code in customized <a href="http://cyber-dojo.org">cyber-dojo</a>
sessions which automatically:
<ul>
<li>checks which tokens have been used</li>
<li>tells you the size of the program</li>
<li>allows everyone to see all the submissions in the review</li>
</ul>
The rules:
<ul>
<li>tokens must be the correct type; eg you cannot write "." or ... for a dot operator</li>
<li>whitespace does <em>not</em> count towards the program's size</li>
<li>additional tokens are allowed</li>
<li>the program must compile</li>
<li>the program is <em>not</em> executed</li>
<li>warnings are allowed</li>
</ul>
In one round <a href="http://www.levelofindirection.com/about-me/">Phil Nash</a> selected these 7 tokens:
<pre class="code">
const vector<string> tokens =
{
".", // operator
"switch", // keyword
"snafu", // identifier
",", // punctuator
"\"salmo\"", // literal
"goto", // keyword
"!", // operator
};
</pre>
and the winning solution (54 characters long) was:
<pre class="code">
union X { X* x; };
X snafu() {
l: switch (X().x,!"salmo"); goto l;
}
</pre>
In another round Hulgar Frydrych selected these 7 tokens:
<pre class="code">
const vector<string> tokens =
{
"catch", // keyword
"->", // operator
"[", // punctuator
";", // punctuator
"--", // operator
"foobar", // identifier
"operator", // keyword
};
</pre>
and the winning solution (53 characters long) was:
<pre class="code">
class c {
c operator->(){
foobar:
try{
}
catch(c x[]){
x--;
}
}
};
</pre>
Can you create shorter versions?
<br/>
<br/>
Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-84862467644716344642016-12-06T20:12:00.001+00:002016-12-06T20:12:27.939+00:00the design and implementation of cyber-dojo
At the excellent <a href="http://agileonthebeach.com/2016-2/">Agile on the Beach conference</a> in Cornwall I did a presentation outlining some of the history, design and implementation of cyber-dojo.
The video has just gone live on youtube.
<br/>
<br/>
<iframe width="560" height="315" src="https://www.youtube.com/embed/IQdqcUBY6zw" frameborder="0" allowfullscreen></iframe>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-73667891824002780882016-10-21T10:00:00.000+00:002016-10-21T10:00:16.519+00:00Continuous Delivery
<div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-sdnzsCxLySU/WAmwHKYQjXI/AAAAAAAAEx8/z-8kpCmi42ELuYvb6pz1Q-O7WDUXrD2fwCLcB/s1600/ContinuousDelivery.jpeg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://2.bp.blogspot.com/-sdnzsCxLySU/WAmwHKYQjXI/AAAAAAAAEx8/z-8kpCmi42ELuYvb6pz1Q-O7WDUXrD2fwCLcB/s200/ContinuousDelivery.jpeg" width="153" height="200" /></a></div>
Is an excellent book by Jez Humble and Dave Farley.
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to quote from a few pages...
<blockquote>
Software delivers no value until it is in the hands of its users.
</blockquote>
<blockquote>
The <a href="http://jonjagger.blogspot.co.uk/2013/12/patterns.html">pattern</a>
that is central to this book is the deployment pipeline.
</blockquote>
<blockquote>
It should not be possible to make manual
<a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">changes</a> to
testing, staging, and production environments.
</blockquote>
<blockquote>
If releases are <a href="http://jonjagger.blogspot.co.uk/2013/12/time.html">frequent</a>,
the delta between releases will be small. This significantly reduces the risk associated
with releasing and makes it much easier to to roll back.
</blockquote>
<blockquote>
Branching should, in most circumstances, be avoided.
</blockquote>
<blockquote>
Dashboards should be ubiquitous, and certainly at least one should be
present in each team room.
</blockquote>
<blockquote>
One of the key principles of the deployment pipeline is that it is a pull
<a href="http://jonjagger.blogspot.co.uk/2013/10/systems.html">system</a>.
</blockquote>
<blockquote>
A corollary of having every version of every file in version control is that
it allows you to be aggressive about deleting things that you don't think
you need... The ability to weed out old ideas and implementations frees the
<a href="http://jonjagger.blogspot.co.uk/2013/10/teams.html">team</a>
to try new things and to
<a href="http://jonjagger.blogspot.co.uk/2014/02/improving.html">improve</a> the code.
</blockquote>
<blockquote>
It should always be cheaper to create a new environment than to repair an old one.
</blockquote>
<blockquote>
The goal of continuous integration is that the software is in a working state all the
<a href="http://jonjagger.blogspot.co.uk/2013/12/time.html">time</a>...
Continuous is a practice not a tool...
Continuously is more often than you think.
</blockquote>
<blockquote>
The most important <a href="http://jonjagger.blogspot.co.uk/2013/10/practice.html">practice</a>
for continuous integration to work properly is frequent check-ins to trunk or mainline.
</blockquote>
<blockquote>
Ideally, the compile and test process that you run prior to check-in and on
your CI server should take no more than a few minutes. We think that ten
minutes is about the limit, five minutes is better, and about 90 seconds is
ideal.
</blockquote>
<blockquote>
Enabling developers to run smoke tests against a working system on a developer
machine prior to each check-in can make a huge difference to the quality of
your application.
</blockquote>
<blockquote>
Build breakages are a normal and expected part of the process. Our aim is to find
errors and eliminate them as quickly as possible, without expecting perfection and
zero errors.
</blockquote>
<blockquote>
Having a comprehensive <a href="http://jonjagger.blogspot.co.uk/2013/12/testing.html">test</a>
suite is essential to continuous integration.
</blockquote>
<blockquote>
You should also consider refactoring as a cornerstone of effective software
development.
</blockquote>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0tag:blogger.com,1999:blog-6714530.post-50724399223997366712016-10-20T18:06:00.000+00:002016-10-20T18:06:56.380+00:00Building Microservices
<div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-cSN0kIa2yLU/WAkHZKc-ASI/AAAAAAAAExo/PcoWX1Fhp6Ys7g-XF0jg4R0kVm9xTg7ZwCLcB/s1600/BuildingMicroServices.jpeg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://4.bp.blogspot.com/-cSN0kIa2yLU/WAkHZKc-ASI/AAAAAAAAExo/PcoWX1Fhp6Ys7g-XF0jg4R0kVm9xTg7ZwCLcB/s200/BuildingMicroServices.jpeg" width="153" height="200" /></a></div>
Is an excellent book by Sam Newman.
<a href="http://jonjagger.blogspot.co.uk/p/book-snippets.html">As usual</a> I'm going to quote from a few pages...
<blockquote>
Because microservices are primarily modeled around business domains,
they avoid the problems of traditional <a href="http://jonjagger.blogspot.co.uk/2013/12/hierarchy.html">tiered</a> architectures.
</blockquote>
<blockquote>
Microservices should cleanly align to bounded contexts.
</blockquote>
<blockquote>
Another reason to prefer the nested approach could be to chunk up your
architecture to simplify
<a href="http://jonjagger.blogspot.co.uk/2016/03/code-and-test-oppose-each-other.html">testing</a>.
</blockquote>
<blockquote>
With an event-based <a href="http://jonjagger.blogspot.co.uk/2013/10/communication.html">collaboration</a>,
we invert things. Instead of a client
initiating requests asking for things to be done, it instead says
<i>this thing happened</i> and expects other parties to know what to do.
We never tell anyone else what to do.
</blockquote>
<blockquote>
We <a href="http://jonjagger.blogspot.co.uk/2009/06/bill-baileys-law.html">always</a>
want to maintain the ability to release microservices independenty of each other.
</blockquote>
<blockquote>
A red build means the last change possibly did not intergrate. You need to stop all
further check-ins that aren't involved in fixing the build to get it passing again.
</blockquote>
<blockquote>
The approach I prefer is to have a single CI build per microservice, to allow us to
quickly make and validate a change prior to deployment into production.
</blockquote>
<blockquote>
No <a href="http://jonjagger.blogspot.co.uk/2013/04/change.html">changes</a> are ever
made to a running server.
</blockquote>
<blockquote>
Rather than using a package manager like debs or RPMs, all software is
installed as independent Docker apps, each running in its own container.
</blockquote>
<blockquote>
Flaky tests are the enemy. When they fail, they don't tell us much...
A test suite with flaky tests can become a victim of what Diane Vaughan calls the
<i>normalization of deviance</i> - the idea that over time we can become so
accustomed to things being wrong that we start to accept them as being normal
and not a problem.
</blockquote>
<blockquote>
All too often, the approach of accepting multiple services being deployed together
<a href="http://jonjagger.blogspot.co.uk/2013/04/the-decomposition-fallacy.html">drifts</a>
into a situation where services become coupled.
</blockquote>
<blockquote>
Most <a href="http://jonjagger.blogspot.co.uk/2013/12/organization.html">organizations</a> that I see spending time creating functional
test suites often expend little or no effort at all on better monitoring or
recovering from failure.
</blockquote>
<br/>
<br/>Jon Jaggerhttp://www.blogger.com/profile/11560463167349216675noreply@blogger.com0