How should we teach a new language to folks that already know how to program?
How do we use what we already know to leapfrog the learning process?
Based on my personal experience and snippets of natural language theory, we will try to explore the cheats and pitfalls when learning a new programming language, but also dig into how we can make it easier.
2. @pati_gallardo
TurtleSec
To learn the basics - write an application you know
To learn the middle and remove the accent, maintain it with the feedback of a native speaker
2
35. @pati_gallardo
TurtleSec
[C++] Operator Overloading Use
1. Box first = new Box(new Point(1, 1), 3, 2);
2. Box second = new Box(new Point(3, 4), 3, 3);
3. Box boundingBox = first + second;
@pati_gallardo 35
Some constructs are quite elegant
with operator overloading
37. @pati_gallardo
TurtleSec
[C++] Operator Overloading Definition
1. public static Box operator +(Box left, Box right) {
2. Point top =
3. new Point(Math.Min(left.top().x, right.top().x),
4. Math.Min(left.top().y, right.top().y));
5. Point bottom =
6. new Point(Math.Max(left.bottom().x, right.bottom().x),
7. Math.Max(left.bottom().y, right.bottom().y));
8. int width = bottom.x - top.x;
9. int height = bottom.y - top.y;
10. return new Box(top, width, height);
11. }
@pati_gallardo 37
38. @pati_gallardo
TurtleSec
[C++] Optional Arguments
1. public class OptionalArguments {
2. private static int increase(int x, int y = 2) {
3. return x + y;
4. }
5.
6. [Test]
7. public void TestOptionalArguments() {
8. Assert.AreEqual(5, increase(3));
9. }
10. }
@pati_gallardo 38
39. @pati_gallardo
TurtleSec
[Objective-C] Named Parameters
1. public class NamedParameters {
2. private static int named(int x, int y) {
3. return x + y;
4. }
5.
6. [Test]
7. public void TestNamedParameters() {
8. Assert.AreEqual(5, named(y: 3, x: 2));
9. }
10. }
@pati_gallardo 39
40. @pati_gallardo
TurtleSec
[C++] Struct - value type
1. struct Point {
2. public int x;
3. public int y;
4. }
@pati_gallardo 40
Similar in meaning to C++Drift in Form
In C++ structs are classes with default
public access - in C# structs are
classes that don’t allow inheritance
In C++ all types
are value types
Cognates
42. @pati_gallardo
TurtleSec
[Qt?] Properties
1. public class Properties {
2. public string ReadWriteProperty { get; set; } = "Hello";
3. public string ReadOnlyProperty { get; }
4. // Auto-implemented properties must have get accessors.
5. // public string WriteOnlyProperty { set; }
6. public string PrivateReadWriteProperty { private get; set; }
7. public string ReadPrivateWriteProperty { get; private set; }
8.
9. private string PrivateReadWriteField;
10. public string WrapField {
11. set => PrivateReadWriteField = value;
12. get => PrivateReadWriteField;
13. }
14. }
@pati_gallardo 42
Magic “value”
43. @pati_gallardo
TurtleSec
[C & C++] (Inline) Out Parameters
1. private static int outParam(int x, int y, out int mod) {
2. mod = x % y;
3. return x / y;
4. }
5.
6. [Test]
7. public void TestOutParameters() {
8. int quotient = outParam(25, 5, out var remainder);
9. Assert.AreEqual(0, remainder);
10. Assert.AreEqual(5, quotient);
11. }
@pati_gallardo 43
Passing int
by ref
44. @pati_gallardo
TurtleSec
[] Null-conditional operators ?. and ?[]
1. int GetCoffee(List<Shop> coffeeShops, int index) {
2. // If there are any coffeeShops and
3. // if there is a shop at that index,
4. // please get me that coffee,
5. // if not, just get me 0
6. return coffeeShops?[index]?.Coffee ?? 0;
7. }
@pati_gallardo 44
47. @pati_gallardo
TurtleSec
[C++ & Java] Destructor / Finalize
1. public class DestroyMe {
2. ~DestroyMe() {
3. // Might be called by the
4. // Garbage Collector
5. }
6. }
@pati_gallardo 47
More like Java
finalize
Drift in Meaning
50. @pati_gallardo
TurtleSec
[Java & C++] IDisposable
1. public class DisposeMe : IDisposable {
2. private FileStream fileStream;
3.
4. public void Dispose() {
5. fileStream?.Dispose();
6. }
7. }
@pati_gallardo 50
Similar to Javas
Closeable
Drift in Form
Same semantics
Different syntax
63. @pati_gallardo
TurtleSec
[JavaScript] Anonymous Types
1. var gilmoreGirls =
2. new[] {
3. new { Name = "Loralai", Age = "36" },
4. new { Name = "Rory", Age = "20" }
5. };
@pati_gallardo 63
If you squint it looks
a bit like JS objects
64. @pati_gallardo
TurtleSec
[JavaScript] ExpandoObject
1. public delegate void printMe();
2.
3. [Test]
4. public void Test() {
5. dynamic expando = new ExpandoObject();
6. // Add a field
7. expando.GilmoreGirl = "Rory";
8. Assert.AreEqual("Rory", expando.GilmoreGirl);
9. // Add a function
10. printMe order = delegate { Console.WriteLine("Coffee!"); };
11. expando.getCoffee = order;
12. expando.getCoffee();
13. }
14.
@pati_gallardo 64
Full on JS!