Created
May 12, 2024 17:24
-
-
Save worldofgeese/959f1d371a908ec803ef1ae7f7031ab4 to your computer and use it in GitHub Desktop.
Double a number in C# using the design method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Double a number in C# using the design method\n", | |
"We are asked to create a function that doubles a number. The signature is the \"type\" of input consumed and its output. Since the input is a number and the output is also a number. Write the number, an arrow (->) and the output number." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Signature\n", | |
"Number -> Number" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Purpose\n", | |
"\n", | |
"A stub's purpose should capture what we wish to create in terms of what data we have to work with.\n", | |
"\n", | |
"A good purpose could be (from Gregor Kiczales), \"Produce 2 times the given number\".\n", | |
"\n", | |
"<img width=\"454\" alt=\"Screenshot 2024-05-12 at 15 00 48\" src=\"https://gist.github.com/assets/59834693/3fafdbd4-a2a2-4225-90a9-931e16a94142\">\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Stub\n", | |
"\n", | |
"The stub doesn't need to do anything other than return a value of the same type as the signature expects. Inside our code notebook we are able to run the C# cell below this Markdown cell to guarantee our stub returns the correct type.\n", | |
"\n", | |
"<img width=\"454\" alt=\"Screenshot 2024-05-12 at 15 02 30\" src=\"https://gist.github.com/assets/59834693/7f8cd279-3467-4536-9e43-6c4186f06be5\">\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
}, | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div class=\"dni-plaintext\"><pre>0</pre></div><style>\r\n", | |
".dni-code-hint {\r\n", | |
" font-style: italic;\r\n", | |
" overflow: hidden;\r\n", | |
" white-space: nowrap;\r\n", | |
"}\r\n", | |
".dni-treeview {\r\n", | |
" white-space: nowrap;\r\n", | |
"}\r\n", | |
".dni-treeview td {\r\n", | |
" vertical-align: top;\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"details.dni-treeview {\r\n", | |
" padding-left: 1em;\r\n", | |
"}\r\n", | |
"table td {\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"table tr { \r\n", | |
" vertical-align: top; \r\n", | |
" margin: 0em 0px;\r\n", | |
"}\r\n", | |
"table tr td pre \r\n", | |
"{ \r\n", | |
" vertical-align: top !important; \r\n", | |
" margin: 0em 0px !important;\r\n", | |
"} \r\n", | |
"table th {\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"</style>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"static int DoubleNumber(int n)\n", | |
"{\n", | |
" return 0;\n", | |
"}\n", | |
"DoubleNumber(2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Write tests\n", | |
"\n", | |
"Exercism uses the test framework, [xUnit](https://xunit.net/docs/getting-started/netcore/cmdline#write-first-tests), to create \"unit tests\". [Unit tests](https://en.wikipedia.org/wiki/Unit_testing) test individual parts of a program in isolation, like a function. Notice how in a Markdown cell I can add clickable links like the getting started guide for xUnit or the Wikipedia article for unit testing, to provide additional context to the reader.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
}, | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"// Install the xUnit.net package for unit testing\n", | |
"#r \"nuget:xunit\"\n", | |
"\n", | |
"// Gain access to the package\n", | |
"using Xunit;\n", | |
"\n", | |
"int result = 2 * 2;\n", | |
"Assert.Equal(4, result);" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Code body\n", | |
"\n", | |
"Now we code the function body, copying the stub into a new cell and replacing the interior of the sub (everything but the portions outside the curly brackets). The function body will contain code with the correct behavior: for our `DoubleNumber` function, whatever will return a doubled number." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div class=\"dni-plaintext\"><pre>4</pre></div><style>\r\n", | |
".dni-code-hint {\r\n", | |
" font-style: italic;\r\n", | |
" overflow: hidden;\r\n", | |
" white-space: nowrap;\r\n", | |
"}\r\n", | |
".dni-treeview {\r\n", | |
" white-space: nowrap;\r\n", | |
"}\r\n", | |
".dni-treeview td {\r\n", | |
" vertical-align: top;\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"details.dni-treeview {\r\n", | |
" padding-left: 1em;\r\n", | |
"}\r\n", | |
"table td {\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"table tr { \r\n", | |
" vertical-align: top; \r\n", | |
" margin: 0em 0px;\r\n", | |
"}\r\n", | |
"table tr td pre \r\n", | |
"{ \r\n", | |
" vertical-align: top !important; \r\n", | |
" margin: 0em 0px !important;\r\n", | |
"} \r\n", | |
"table th {\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"</style>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"static int DoubleNumber(int n)\n", | |
"{\n", | |
" return 2 * n;\n", | |
"}\n", | |
"DoubleNumber(2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Test and debug\n", | |
"\n", | |
"When going through exercises in Exercism, you will be provided with tests. It's OK to copy the tests into your own cells in your notebook and run them until you pass. For example, in the strings exercise, \"Log Levels\", a `LogLevelsTests.cs` file is provided. You can copy the function body from any test (say, `Assert.Equal(\"Stack overflow\", LogLine.Message(\"[ERROR]: Stack overflow\"));`) into a C# cell directly into your notebook and run it until you pass. Remember, \"run early, run often\"!\n", | |
"\n", | |
"Note that only a *failing* test will give an output." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".NET (C#)", | |
"language": "C#", | |
"name": ".net-csharp" | |
}, | |
"polyglot_notebook": { | |
"kernelInfo": { | |
"defaultKernelName": "csharp", | |
"items": [ | |
{ | |
"aliases": [], | |
"languageName": "csharp", | |
"name": "csharp" | |
} | |
] | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment