Download Complete Adaptive Code via C Agile coding with design patterns and SOLID principles 1st Edition Gary Mclean Hall PDF for All Chapters
Download Complete Adaptive Code via C Agile coding with design patterns and SOLID principles 1st Edition Gary Mclean Hall PDF for All Chapters
https://ebookgate.com
https://ebookgate.com/product/adaptive-code-via-c-
agile-coding-with-design-patterns-and-solid-
principles-1st-edition-gary-mclean-hall/
https://ebookgate.com/product/agile-software-development-principles-
patterns-and-practices-martin/
ebookgate.com
https://ebookgate.com/product/agile-software-development-principles-
patterns-and-practices-robert-martin/
ebookgate.com
https://ebookgate.com/product/c-class-design-handbook-coding-
effective-classes-1st-edition-richard-conway/
ebookgate.com
https://ebookgate.com/product/c-design-patterns-and-derivatives-
pricing-2nd-edition-m-s-joshi/
ebookgate.com
https://ebookgate.com/product/pro-objective-c-design-patterns-for-
ios-1st-edition-carlo-chung/
ebookgate.com
https://ebookgate.com/product/clr-via-c-third-edition-jeffrey-richter/
ebookgate.com
https://ebookgate.com/product/mobile-design-patterns-nudelman/
ebookgate.com
From the Library of Ida Schander
Adaptive Code via C#:
Agile coding with
design patterns and
SOLID principles
First Printing
Microsoft Press books are available through booksellers and distributors worldwide. If you need support related
to this book, email Microsoft Press Book Support at [email protected]. Please tell us what you think of
this book at http://aka.ms/tellpress.
This book is provided “as-is” and expresses the author’s views and opinions. The views, opinions and information
expressed in this book, including URL and other Internet website references, may change without notice.
Some examples depicted herein are provided for illustration only and are fictitious. No real association or
connection is intended or should be inferred.
Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks” webpage are trademarks
of the Microsoft group of companies. All other marks are property of their respective owners.
Introduction xv
Index 387
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Artifacts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Scrum board. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Charts and metrics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Backlogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The sprint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Release planning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Sprint planning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Daily Scrum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Sprint demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Sprint retrospective. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Scrum calendar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
microsoft.com/learning/booksurvey
vii
Managing dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Implementations versus interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
The new code smell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Alternatives to object construction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The Entourage anti-pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Stairway pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Resolving dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Dependency management with NuGet. . . . . . . . . . . . . . . . . . . . . . . . . . 77
Layering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Common patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Cross-cutting concerns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Asymmetric layering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
viii Contents
Refactoring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Changing existing code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
A new account type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Contents ix
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Preconditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Postconditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Data invariants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Liskov contract rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Code contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
x Contents
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Contents xi
“I want to view the messages that have been sent to a room.“ . . . . . . . . . 353
xii Contents
Index 387
microsoft.com/learning/booksurvey
Contents xiii
T he first words of the title of this book, Adaptive Code, provide a good description
of the outcome of applying the principles in the book: the ability of code to adapt
to any new requirement or unforeseen scenario while avoiding significant rework. The
aim of this book is to aggregate into one volume many of the current best practices in
the world of C# programming with the Microsoft .NET Framework. Although some of
the content is covered in other books, those books either focus heavily on theory or are
not specific to .NET development.
Programming can be a slow process. If your code is adaptive, you will be able to
make changes to it more quickly, more easily, and with fewer errors than you would
if you were working with a codebase that impedes changes. Requirements, as every
developer knows, are subject to change. How change is managed is a key differen
tiating factor between successful software projects and those that atrophy due to scope
creep. Developers can react in many ways to requirement changes, with two opposing
viewpoints highlighting the continuum that lies between.
First, developers can choose a rigid viewpoint. In this approach, from the develop-
ment process down to class design, the project is as inflexible as if it were implemented
50 years ago by using punch cards. Waterfall methodologies are conspicuous culprits
in ensuring that software cannot change freely. Their determination that the phases of
analysis, design, implementation, and testing be distinct and one-way make it difficult—
or at least expensive—for customers to change requirements after implementation has
begun. The code, then, does not need to be built for change: the process all but forbids
alterations.
The second approach, Agile methodology, is not just an alternative to such rigid
methodologies, but a reaction to them. The aim of Agile processes is to embrace change
as a necessary part of the contract between client and developer. If customers want to
change something in the product that they are paying for, the temporal and financial
cost should be correlated to the size of the change, not the phase of the process that is
currently in progress. Unlike physical engineering, software engineering works with a
malleable tool: source code. The bricks and mortar that form a house are literally fused
together as construction progresses. The expense involved in changing the design of
a house is necessarily linked to the completion of the building phase. If the project
has not been started—if it is still just in blueprints—change is relatively cheap. If the
windows are in, the electricity wired up, and the plumbing fitted, moving the upstairs
bathroom down next to the kitchen could be prohibitively expensive. With code, mov-
ing features around and reworking the navigation of a user interface should not be as
xv
This book demonstrates the second approach and explains, with real-world exam-
ples, the practicalities of implementing adaptive code.
Capable intermediate programmers who want to plug the gaps in their knowledge
or have doubts and questions about how some of the industry’s best practices fit
together will benefit most from this book, especially because the day-to-day reality
of programming rarely matches simple examples or theory. Much of SOLID is now
understood, but the intricacies of the open/closed principle (covered in Chapter 6)
and Liskov substitution (covered in Chapter 7) are not fully comprehended. Even
experienced programmers sometimes do not fully realize the benefits provided by
dependency injection (covered in Chapter 9). Similarly, the flexibility—adaptability—
that interfaces (covered in Chapter 3) lend to code is often overlooked.
This book can also help the more junior developer learn, from the ground up, which
aspects of common patterns and practices are benevolent and which are, in the long
term, malevolent. The code samples that I see from prospective employees have a lot
in common. The general theme is that the candidate is almost there with respect to
many skills but just needs a slight push in the right direction to become a significantly
better programmer. Specifically, the Entourage anti-pattern (covered in Chapter 2) and
the Service Locator anti-pattern (covered in Chapter 9) are very prevalent in sample
code. Practical alternatives, and their rationales, are provided in this book.
Assumptions
Ideally, you should have some practical experience of programming in a language that
is syntactically similar to C#, such as Java or C++. You should also have a strong founda-
tion in core procedural programming concepts such as conditional branching, loops,
and expressions. You should also have some experience of object-oriented program-
ming using classes, and at least a passing familiarity with interfaces.
xvi Introduction
■■ Chapter 1: Introduction to Scrum This chapter sets the scene for the book
by introducing Scrum, which is an Agile project management methodology. The
chapter gives an in-depth overview of the artifacts, roles, metrics, and phases
of a Scrum project. Finally, it shows how developers should organize themselves
and their code when operating in an Agile environment.
Introduction xvii
■■ Chapter 4: Unit testing and refactoring Two practices that are becoming
prerequisite skills are unit testing and refactoring. The two are closely related
and work in unison to produce adaptive code. Without the safety net of unit
tests, refactoring is prone to error; without refactoring, code becomes unwieldy,
rigid, and hard to comprehend. This chapter takes an example of unit testing
from humble beginnings and expands it to use more advanced—but practical
—patterns and practices such as fluent assertions, test-driven development,
and mocking. For refactoring, the chapter provides examples of real-world
refactors that improve the readability and maintainability of the source code.
■■ Chapter 7: The Liskov substitution principle This chapter shows the posi-
tive effects that result from applying the Liskov substitution principle on code,
particularly the fact that the guidelines help enforce the open/closed principle
and prevent the unintended consequences of change. Contracts—through
xviii Introduction
■■ Chapter 12: Adaptive sample: Sprint 2 The client, inevitably, makes some
changes to the requirements of the application, and the team accommodates
those changes through adaptive code.
Introduction xix
Volsung did not long remain childless, for ten stalwart sons and
one lovely daughter, Signy, came to brighten his home. As soon as
this maiden reached marriageable years, many suitors asked for her
hand, which was finally pledged to Siggeir, King of the Goths, whom,
however, she had never seen.
The wedding day came, and when the bride first beheld her
destined groom she shrank back in dismay, for his
The wedding
of Signy. puny form and lowering glances contrasted oddly with
her brothers’ strong frames and frank faces. But it was
too late to withdraw,—the family honor was at stake,—and Signy so
successfully concealed her dislike that none except her twin brother
Sigmund suspected how reluctantly she became Siggeir’s wife.
The wedding feast was held as usual, and when the
The sword in merrymakings had reached their height the guests
the Branstock. were startled by the sudden entrance of a tall, one-
eyed man, closely enveloped in a mantle of cloudy
blue. Without vouchsafing word or glance to any in the assembly, the
stranger strode up to the Branstock and thrust a glittering sword up
to the hilt in its great bole. Then, turning slowly around, he faced the
awe-struck assembly, and in the midst of the general silence
declared that the weapon would belong to the warrior who could pull
it out, and that it would assure him victory in every battle. These
words ended, he passed out and disappeared, leaving an intimate
conviction in the minds of all the guests that Odin, king of the gods,
had been in their midst.
“So sweet his speaking sounded, so wise his words did seem,
That moveless all men sat there, as in a happy dream
We stir not lest we waken; but there his speech had end,
And slowly down the hall-floor and outward did he wend;
And none would cast him a question or follow on his ways,
For they knew that the gift was Odin’s, a sword for the world to
praise.”
“Sons I have gotten and cherished, now stand ye forth and try;
Lest Odin tell in God-home how from the way he strayed,
And how to the man he would not he gave away his blade.”
“At last by the side of the Branstock Sigmund the Volsung stood,
And with right hand wise in battle the precious sword-hilt caught,
Yet in a careless fashion, as he deemed it all for naught;
When, lo, from floor to rafter went up a shattering shout,
For aloft in the hand of Sigmund the naked blade showed out
As high o’er his head he shook it: for the sword had come away
From the grip of the heart of the Branstock, as though all loose it
lay.”
THE BRANSTOCK.—Hoffmann.
“Then sweetly Volsung kissed her: ‘Woe am I for thy sake,
But Earth the word hath hearkened, that yet unborn I spake;
How I ne’er would turn me backward from the sword or fire of bale;—
—I have held that word till to-day, and to-day shall I change the tale?
And look on these thy brethren, how goodly and great are they,
Wouldst thou have the maidens mock them, when this pain hath
passed away
And they sit at the feast hereafter, that they feared the deadly
stroke?
Let us do our day’s work deftly for the praise and the glory of folk;
And if the Norns will have it that the Volsung kin shall fail,
Yet I know of the deed that dies not, and the name that shall ever
avail.’”
Marching towards the palace, the brave little troop soon fell into
Siggeir’s ambuscade, and, although they fought with heroic courage,
they were so overpowered by the superior number of their foes that
Volsung was soon slain and all his sons made captive. Led bound
into the presence of Siggeir, who had taken no part in the fight (for
he was an arrant coward), Sigmund was forced to relinquish his
precious sword, and he and his brothers were all condemned to die.
Signy, hearing this cruel sentence, vainly interceded for them,
but all she could obtain by her prayers and entreaties was that her
kinsmen should be chained to a fallen oak in the forest, there to
perish of hunger and thirst if the wild beasts spared them. Then,
fearing lest his wife should visit and succor her brothers, Siggeir
confined her in the palace, where she was closely guarded night and
day.
Early every morning Siggeir himself sent a messenger into the
forest to see whether the Volsungs were still living, and every
morning the man returned saying a monster had come during the
night and had devoured one of the princes, leaving nothing but his
bones. When none but Sigmund remained alive, Signy finally
prevailed upon one of her servants to carry some honey into the
forest and smear it over her brother’s face and mouth.
That very night the wild beast, attracted by the smell of the
honey, licked Sigmund’s face, and even thrust its tongue into his
mouth. Clinching his teeth upon it, Sigmund, weak and wounded as
he was, struggled until his bonds broke and he could slay the nightly
visitor who had caused the death of all his brothers. Then he
vanished into the forest, where he remained concealed until the daily
messenger had come and gone, and until Signy, released from
captivity, came speeding to the forest to weep over her kinsmen’s
remains.
Seeing her evident grief, and knowing she had no part in
Siggeir’s cruelty, Sigmund stole out of his place of concealment,
comforted her as best he could, helped her to bury the whitening
bones, and registered a solemn oath in her presence to avenge his
family’s wrongs. This vow was fully approved by Signy, who,
however, bade her brother abide a favorable time, promising to send
him a helper. Then the brother and sister sadly parted, she to return
to her distasteful palace home, and he to seek the most remote part
of the forest, where he built a tiny hut and plied the trade of a smith.
“And once in the dark she murmured: ‘Where then was the ancient
song
That the Gods were but twin-born once, and deemed it nothing
wrong
To mingle for the world’s sake, whence had the Æsir birth,
And the Vanir, and the Dwarf-kind, and all the folk of earth?’”
“For here the tale of the elders doth men a marvel to wit,
That such was the shaping of Sigmund among all earthly kings,
That unhurt he handled adders and other deadly things,
And might drink unscathed of venom: but Sinfiotli was so wrought
That no sting of creeping creatures would harm his body aught.”
“And then King Siggeir’s roof-tree upheaved for its utmost fall,
And its huge walls clashed together, and its mean and lowly things
The fire of death confounded with the tokens of the kings.”
“He drank as he spake the words, and forthwith the venom ran
In a chill flood over his heart, and down fell the mighty man
With never an uttered death-word and never a death-changed look,
And the floor of the hall of the Volsungs beneath his falling shook.
Then up rose the elder of days with a great and bitter cry,
And lifted the head of the fallen; and none durst come anigh
To hearken the words of his sorrow, if any words he said
But such as the Father of all men might speak over Balder dead.
And again, as before the death-stroke, waxed the hall of the
Volsungs dim,
And once more he seemed in the forest, where he spake with naught
but him.”
“But, lo! through the hedge of the war-shafts, a mighty man there
came,
One-eyed and seeming ancient, but his visage shone like flame:
Gleaming gray was his kirtle, and his hood was cloudy blue;
And he bore a mighty twi-bill, as he waded the fight-sheaves
through,
And stood face to face with Sigmund, and upheaved the bill to smite.
Once more round the head of the Volsung fierce glittered the
Branstock’s light,
The sword that came from Odin: and Sigmund’s cry once more
Rang out to the very heavens above the din of war.
Then clashed the meeting edges with Sigmund’s latest stroke,
And in shivering shards fell earthward that fear of worldly folk.
But changed were the eyes of Sigmund, the war-wrath left his face;
For that gray-clad, mighty Helper was gone, and in his place
Drave on the unbroken spear-wood ’gainst the Volsung’s empty
hands:
And there they smote down Sigmund, the wonder of all lands,
On the foemen, on the death-heap his deeds had piled that day.”
All the Volsung race and army had already succumbed, so Lygni
immediately left the battlefield to hasten on and take possession of
the kingdom and palace, where he fully expected to find the fair
Hiordis and force her to become his wife. As soon as he had gone,
however, the beautiful young queen crept out of her hiding place in
the thicket, ran to the dying Sigmund, caught him to her breast in a
last passionate embrace, and tearfully listened to his dying words.
He then bade her gather up the fragments of his sword, carefully
treasure them, and give them to the son whom he foretold would
soon be born, and who was destined to avenge his death and be far
greater than he.
“‘I have wrought for the Volsungs truly, and yet have I known full well
That a better one than I am shall bear the tale to tell:
And for him shall these shards be smithied; and he shall be my son,
To remember what I have forgotten and to do what I left undone.’”
“And the three were the heart-wise Odin, the Father of the Slain,
And Loki, the World’s Begrudger, who maketh all labor vain,
And Hönir, the Utter-Blameless, who wrought the hope of man,
And his heart and inmost yearnings, when first the work began;—
The God that was aforetime, and hereafter yet shall be
When the new light yet undreamed of shall shine o’er earth and sea.”
These gods had not wandered very far before Loki perceived an
otter basking in the sun. Animated by his usual spirit of destruction,
he slew the unoffending beast—which, as it happened, was the
dwarf king’s second son, Otter—and flung its lifeless body over his
shoulders, thinking it would furnish a good dish when meal time
came.
Following his companions, Loki came at last to Hreidmar’s
house, entered with them, and flung his burden down upon the floor.
The moment the dwarf king’s glance fell upon it he flew into a
towering rage, and before the gods could help themselves they were
bound by his order, and heard him declare that they should never
recover their liberty unless they could satisfy his thirst for gold by
giving him enough of that precious substance to cover the otterskin
inside and out.
In spite of diligent search, however, Loki could not find the dwarf;
but perceiving a salmon sporting in the foaming waters, he shrewdly
concluded the dwarf must have assumed this shape, and borrowing
Ran’s net he soon had the fish in his power. As he had suspected, it
was Andvari, who, in exchange for liberty, reluctantly brought forth
his mighty treasure and surrendered it all, including the Helmet of
Dread and a hauberk of gold, reserving only the ring he wore, which
was gifted with miraculous powers, and, like a magnet, helped him to
collect the precious ore. But the greedy Loki, catching sight of it,
wrenched it away from him and departed laughing, while the dwarf
hurled angry curses after him, declaring that the ring would ever
prove its possessor’s bane and would cause the death of many.
“That gold
Which the dwarf possessed
Shall to two brothers
Be cause of death,
And to eight princes,
Of dissension.
From my wealth no one
Shall good derive.”
Sæmund’s Edda (Thorpe’s tr.).