KEMBAR78
Thoughts On Learning A New Programming Language | PDF
@pati_gallardo
TurtleSec
@pati_gallardo
Turtle
Sec
@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
@pati_gallardo
TurtleSec
3
How do you learn a new
programming language?
@pati_gallardo
TurtleSec
4
I tried to learn C#
You might too today
@pati_gallardo
TurtleSec
5
Where to start?
Write some code Get environmentTake notes
Videos / BooksFreak out
Procrastinate Ask on TwitterMake flow diagram
@pati_gallardo
Turtle
Sec
Thoughts On Learning
A New Programming Language
Trying to learn C#
Patricia Aas
OOP 2020
@pati_gallardo
Turtle
Sec
Patricia Aas - Trainer & Consultant
C++ Programmer, Application Security
Currently : TurtleSec
Previously : Vivaldi, Cisco Systems, Knowit, Opera Software
Master in Computer Science - main language Java
Pronouns: she/her
@pati_gallardo
TurtleSec
@pati_gallardo
The Genealogy of
programming languages
@pati_gallardo
TurtleSec
9@pati_gallardo
Cross
Pollination
Good? Bad? Both?
@pati_gallardo
TurtleSec
1. #define DEBUG 1
2.
3. namespace Communication {
4. class Telephone {
5. bool log = false;
6.
7. Telephone() {
8. #if DEBUG
9. log = true;
10. #endif
11. }
12.
13. ~Telephone() {
14. log = false;
15. }
16. };
17. }
@pati_gallardo 10
1. #define DEBUG
2.
3. namespace Communication {
4. class Telephone {
5. bool log = false;
6.
7. Telephone() {
8. #if DEBUG
9. log = true;
10. #endif
11. }
12.
13. ~Telephone() {
14. log = false;
15. }
16. }
17. }
Which
Programming
Language is
this?
What about
this?
@pati_gallardo
TurtleSec
1. #define DEBUG 1
2.
3. namespace Communication {
4. class Telephone {
5. bool log = false;
6.
7. Telephone() {
8. #if DEBUG
9. log = true;
10. #endif
11. }
12.
13. ~Telephone() {
14. log = false;
15. }
16. };
17. }
@pati_gallardo 11
1. #define DEBUG
2.
3. namespace Communication {
4. class Telephone {
5. bool log = false;
6.
7. Telephone() {
8. #if DEBUG
9. log = true;
10. #endif
11. }
12.
13. ~Telephone() {
14. log = false;
15. }
16. }
17. }
telephone.cpp
Telephone.cs
C++
C#
@pati_gallardo
TurtleSec
12@pati_gallardo
Evil Twin
@pati_gallardo
TurtleSec
@pati_gallardo
How do you
learn a
language?
@pati_gallardo
TurtleSec
14
Telephone Teléfono?
English
Spanish
Cognates
Same Origin, Form and Meaning
@pati_gallardo
TurtleSec
15
Much Mucho?
English
SpanishSame Form and Meaning
False Cognates
Not Same Origin!
@pati_gallardo
TurtleSec
16@pati_gallardo
Cognates or False
Cognates?
Does it matter?
Your mental shortcuts
are working!
@pati_gallardo
TurtleSec
C# : An Imperative C-like language?
1. var list = new List<int>(){ 40, 5, 9, 39, 19 };
2.
3. for (int i = 0; i < list.Count; i++) {
4. Console.WriteLine(list[i]);
5. }
6.
7. foreach (var item in list) {
8. Console.WriteLine(item);
9. }
@pati_gallardo 17
No Surprises
@pati_gallardo
TurtleSec
C# : A Functional Lisp-like language?
1. var list = new List<int>(){ 40, 5, 9, 39, 19 };
2.
3. list
4. .Where(s => s < 10)
5. .ToList()
6. .ForEach(s => Console.WriteLine(s));
@pati_gallardo 18
No Surprises?
@pati_gallardo
TurtleSec
19
Pigg Pig?
Norwegian
English
False Friends
“I had bad pigs in my tires”
Same Form
Stud / Nail
Not Same Meaning!
@pati_gallardo
TurtleSec
20@pati_gallardo
False Friends
Bad Assumptions
Your mental shortcuts
are hurting you
Drift in Meaning
Special case of
False Friends
@pati_gallardo
TurtleSec
21
Frokost Frokost?
Norwegian
Danish
LunchDrift in Meaning
Same Origin and Form
Breakfast
Not Same Meaning!
@pati_gallardo
TurtleSec
22
std::list List?
C++
C#
ArrayList/
Vector
Not Same Meaning!
Same Origin and Form
Linked List
Drift in Meaning
@pati_gallardo
TurtleSec
23
FormMeaning
Origin
Cognates
False Friends
False Cognates
Drift in MeaningDrift in Form
I’ll figure it out
Trip me up
@pati_gallardo
TurtleSec
@pati_gallardo
How you learn
depends on what
you know?
@pati_gallardo
TurtleSec
25@pati_gallardo
First language Scheme (Lisp)
I know Java - university
I know C++ and C
I have coded in probably 10-15
other languages.
Examples: Perl, Python, JavaScript
Programming for almost 20 years
So what do I know?
@pati_gallardo
TurtleSec
@pati_gallardo
You can’t start
from scratch
@pati_gallardo
TurtleSec
27
What to skip?
Languages inherit from each other
What is normal? What is different?
Danger: False Friends
Danger: Unknown Unknowns
@pati_gallardo
TurtleSec
28
Plan
Skip over Cognates
For loop, break, continue, if, else…
If it’s “normal”, skip it
@pati_gallardo
TurtleSec
@pati_gallardo
How do you know that you’re
not missing things? Skim?
@pati_gallardo
TurtleSec
30
Learn the basics
Find surprises
Too much
repetition
Cognates
@pati_gallardo
TurtleSec
31@pati_gallardo
Namespaces
Defines
Drift in Meaning
@pati_gallardo
TurtleSec
32
1. namespace PackageName {
2. public class ClassName {
3. // Class definition
4. }
5. }
[C++] Namespaces
Recognition
Language
C#More like C++
modules really?
Drift in Meaning
@pati_gallardo
TurtleSec
[C & C++] C# has defines!
1. #define HERE_I_AM
2. using NUnit.Framework;
3.
4. public class Preprocessor {
5. [Test]
6. public void TestPreprocessor() {
7. #if HERE_I_AM
8. Assert.Pass();
9. #else
10. Assert.Fail();
11. #endif
12. }
13. }
@pati_gallardo 33
N really though.
No preprocessor
Drift in Meaning
@pati_gallardo
TurtleSec
34@pati_gallardo
Operator Overloading
Optional Arguments
Named Parameters
Struct Value Type
Cognates
@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
@pati_gallardo
TurtleSec
36
Bounding box
x = 1
y = 1
width = 5
height = 6
(1,1)
(3,4)
Box boundingBox = first + second;
first
second
@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
@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
@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
@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
@pati_gallardo
TurtleSec
41@pati_gallardo
Properties
Out Parameters
Drift in Form
@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”
@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
@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
@pati_gallardo
TurtleSec
45@pati_gallardo
Destructor / Finalize
False Friends
@pati_gallardo
TurtleSec
46
Point out false friends
C# finalizers (also called destructors!)
look like C++ destructors
but act like Java’s finalize()
@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
@pati_gallardo
TurtleSec
@pati_gallardo What are the semantics?
@pati_gallardo
TurtleSec
49
Learn the middle
What’s the
middle?
Beyond
basic
syntax?
@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
@pati_gallardo
TurtleSec
[Python] yield return (Generators)
1. public IEnumerable<int> YieldReturn() {
2. yield return 3;
3. yield return 4;
4. yield return 5;
5. }
6. [Test]
7. public void TestingYieldReturn() {
8. using var it = YieldReturn().GetEnumerator();
9. it.MoveNext();
10. Assert.AreEqual(3, it.Current);
11. it.MoveNext();
12. Assert.AreEqual(4, it.Current);
13. it.MoveNext();
14. Assert.AreEqual(5, it.Current);
15. }
@pati_gallardo 51
@pati_gallardo
TurtleSec
[Python, Java, C++] IEnumerable (Lazy Iterators)
1. // C# In Depth, Jon Skeet
2. // Lazy Evaluation
3. static IEnumerable<int> Fibonacci() {
4. int current = 0;
5. int next = 1;
6. while (true) {
7. yield return current;
8. int oldCurrent = current;
9. current = next;
10. next = next + oldCurrent;
11. }
12. }
@pati_gallardo 52
@pati_gallardo
TurtleSec
[?] Extension Methods (Extend 3-party Classes)
1. public class Target {}
2. public static class Extension {
3. public static void PrintHello(this Target t) {
4. Console.WriteLine("Hello World");
5. }
6. }
7. public class Test {
8. [Test]
9. public void TestExtension() {
10. Target target = new Target();
11. target.PrintHello();
12. }
13. }
@pati_gallardo 53
@pati_gallardo
TurtleSec
@pati_gallardo
How do we teach?
@pati_gallardo
TurtleSec
55
Use Terms People Recognize
Explain how this is different
But give folks a frame of reference
Delegate -> Fancy Function Pointer
@pati_gallardo
TurtleSec
[C & C++] Delegates ( Fancy Function Pointers)
1. public class DelegateTest {
2. public delegate void printMe();
3. [Test]
4. public void TestExtension() {
5. printMe printHello =
6. delegate {
7. Console.WriteLine("Hello World!");
8. };
9. printHello();
10. }
11. }
@pati_gallardo 56
Function Pointer
Type
Function
DefinitionUsing Function
Pointer
@pati_gallardo
TurtleSec
[C & C++] Delegates ( Fancy Function Pointers)
1. printMe first = delegate { Console.WriteLine("Hello"); };
2. printMe second = delegate { Console.WriteLine("World"); };
3.
4. var helloWorld = first + second;
5. helloWorld();
57
Composable
Function Pointers!
Operator + defined on
“function pointers”
@pati_gallardo
@pati_gallardo
TurtleSec
[C++ Qt] Events (Event Handlers)
1. EventHandler handler =
2. delegate {
3. Console.WriteLine("Hello Event!");
4. };
5. handler?.Invoke(this, EventArgs.Empty);
@pati_gallardo 58
@pati_gallardo
TurtleSec
59@pati_gallardo
Non Zero Based Array
Nullable Types
Decimal
What about
surprises?
128 bit floating
point type with
28-29 significant
digit precision
What else
have I
missed?
@pati_gallardo
TurtleSec
Non Zero Based Array
1. Array array = Array.CreateInstance(typeof(int),
2. new[] {2},
3. new[] {42});
4. array.SetValue(5, 42);
5. array.SetValue(6, 43);
6. Assert.AreEqual(2, array.GetLength(0));
7. Assert.AreEqual(42, array.GetLowerBound(0));
8. Assert.AreEqual(43, array.GetUpperBound(0));
9. Assert.AreEqual(5, array.GetValue(42));
10. Assert.AreEqual(6, array.GetValue(43));
@pati_gallardo 60
I mean…. Wat?
@pati_gallardo
TurtleSec
Nullable Types
1. bool? maybe = null;
2.
3. if (maybe ?? true)
4. Console.WriteLine("Print Me!");
5.
6. maybe = false;
7.
8. if (!maybe ?? false)
9. Console.WriteLine("Print me too!");
@pati_gallardo 61
Maybe??
@pati_gallardo
TurtleSec
62@pati_gallardo
Anonymous Types
ExpandoObject
JavaScript
Love
@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
@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!
@pati_gallardo
TurtleSec
65
Killer features
Why C#?
N just
Nice To
Have
@pati_gallardo
TurtleSec
66
Why would I choose C#?
Platform Integration
LINQ
@pati_gallardo
TurtleSec
@pati_gallardo
How do we learn?
@pati_gallardo
TurtleSec
68
Rewrite an application you have written already
Focus on the language
and n the application
What are the Unknown
Unknowns?
Hard to get confident
@pati_gallardo
TurtleSec
69
FormMeaning
Origin
Cognates
False Friends
False Cognates
Drift in MeaningDrift in Form
Unknown
Unknowns!
How do you find out
what you don’t know?
@pati_gallardo
TurtleSec
70
The best way to learn
a programming language is
to make something interesting
next to someone who’s
knowledgeable and kind
@pati_gallardo
TurtleSec
1. #define DEBUG 1
2.
3. namespace Communication {
4. class Telephone {
5. bool log = false;
6.
7. Telephone() {
8. #if DEBUG
9. log = true;
10. #endif
11. }
12.
13. ~Telephone() {
14. log = false;
15. }
16. };
17. }
@pati_gallardo 71
1. #define DEBUG
2.
3. namespace Communication {
4. class Telephone {
5. bool log = false;
6.
7. Telephone() {
8. #if DEBUG
9. log = true;
10. #endif
11. }
12.
13. ~Telephone() {
14. log = false;
15. }
16. }
17. }
telephone.cpp
Telephone.cs
C++
C#
You can have an
accent in code!
Or “not idiomatic”
@pati_gallardo
TurtleSec
72
Where to start?
Procrastinate
Anything that gets you
out of this is good
Maybe even
submitting a talk
to a conference?
@pati_gallardo
TurtleSec
@pati_gallardo
@pati_gallardo
Turtle
Sec
Questions?
Photos from pixabay.com
Patricia Aas, TurtleSec
@pati_gallardo
TurtleSec
@pati_gallardo
Turtle
Sec

Thoughts On Learning A New Programming Language