c# light - a proposal for a new c# syntax

Download C# Light - A proposal for a new C# syntax

Post on 27-Aug-2014

4.604 views

Category:

Software

1 download

Embed Size (px)

DESCRIPTION

A proposal for a new C# syntax. Reasons why: * Why not compete with scripting languages? Lightweight, but keep the power of static typing. * Times are changing. Defaults should change too. Make immutability the new default. * Many common patterns are too hard. Make good practices easy (but allow deviations).

TRANSCRIPT

  • C# Light A proposal for a new C# syntax Inspired by a post by PhilTrelford UPGRADE YOUR MONITOR Read these slides and you might get a free triple height monitor* *or visual equivalent
  • Why change something that works?
  • Why change something that works? Because... Why not compete with scripting languages? Lightweight, but keep the power of static typing.
  • Why change something that works? Because... Why not compete with scripting languages? Lightweight, but keep the power of static typing. Because... Times are changing. Defaults should change too. Make immutability the new default.
  • Why change something that works? Because... Why not compete with scripting languages? Lightweight, but keep the power of static typing. Because... Times are changing. Defaults should change too. Make immutability the new default. Because... Many common patterns are too hard. Make good practices easy (but allow deviations).
  • ! HEALTH WARNING ! These slides are for consumption by open-minded programmers ONLY. When taken with pre-existing closed-mindedness, can cause shock, high blood pressure, anxiety, nausea, confusion and panic.
  • You must be at least this open minded to read these slides How open minded are you? Slime mould Bony fishes People who don't use LINQ People who don't use generics Opposable thumbs People who want C# v6 People who use lambdas
  • Vision for C# Light Clean and lightweight code. Immutability by default. Common scenarios are easy. We make mutability a special case, rather than the other way around.
  • public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// /// Full name /// public string Name { get { return _name; } } /// /// Birthday /// public DateTime Birthday { get { return _birthday; } } } Here is a typical immutable class in C# which we'll use as an example throughout.
  • Here is a typical immutable class in C# which we'll use as an example throughout. 27 lines of code. Can we do better? public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// /// Full name /// public string Name { get { return _name; } } /// /// Birthday /// public DateTime Birthday { get { return _birthday; } } }
  • Here is a typical immutable class in C# which we'll use as an example throughout. 27 lines of code. Can we do better? Why not remove the lines that don't contain useful information? public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// /// Full name /// public string Name { get { return _name; } } /// /// Birthday /// public DateTime Birthday { get { return _birthday; } } }
  • In a typical C# project less than 50% of the lines contain useful information! Surely we can do better?
  • Proposal 1: Simplify doc strings
  • We can start by simplifying the doc strings. Let's make "summary" the default. public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// /// Full name /// public string Name { get { return _name; } } /// /// Birthday /// public DateTime Birthday { get { return _birthday; } } }
  • We can start by simplifying the doc strings. Let's make "summary" the default. public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// /// Full name /// public string Name { get { return _name; } } /// /// Birthday /// public DateTime Birthday { get { return _birthday; } } }
  • public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// Full name public string Name { get { return _name; } } /// Birthday public DateTime Birthday { get { return _birthday; } } } We can start by simplifying the doc strings. Let's make "summary" the default. 4 lines saved!
  • Proposal 2: Automatically create backing fields from constructor parameters Yes, I know C# 6 has something similar. Keep reading...
  • public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// Full name public string Name { get { return _name; } } /// Birthday public DateTime Birthday { get { return _birthday; } } } Because immutability is the default, we can simplify further.
  • public class Person { public Person(string name, DateTime birthday) { _name = name; _birthday = birthday; } private readonly string _name; private readonly DateTime _birthday; /// Full name public string Name { get { return _name; } } /// Birthday public DateTime Birthday { get { return _birthday; } } } Why not automatically define and initialize read- only backing fields from the constructor parameters?
  • public class Person { public Person(string name, DateTime birthday) { } /// Full name public string Name { get { return name; } } /// Birthday public DateTime Birthday { get { return birthday; } } } Of course, you can still define mutable private fields in the usual way. Unlike C# 6, you don't need to in the immutable case. 4 lines saved! Why not automatically define and initialize read-only backing fields from the constructor parameters?
  • Proposal 3: Merge the primary constructor with the class definition C# 6 has this too.
  • public class Person { public Person(string name, DateTime birthday) { } /// Full name public string Name { get { return name; } } /// Birthday public DateTime Birthday { get { return birthday; } } } How often do you have more than one constructor?
  • public class Person { public Person(string name, DateTime birthday) { } /// Full name public string Name { get { return name; } } /// Birthday public DateTime Birthday { get { return birthday; } } } How often do you have more than one constructor? Why not merge the constructor with the class definition?
  • public class Person(string name, DateTime birthday) { /// Full name public string Name { get { return name; } } /// Birthday public DateTime Birthday { get { return birthday; } } } How often do you have more than one constructor? 4 more lines saved! Why not merge the constructor with the class definition? You can still define secondary constructors separately if you need to.
  • Proposal 4: (this one is controversial)
  • Proposal 4: Use indentation instead of curly braces
  • public class Person(string name, DateTime birthday) { /// Full name public string Name { get { return name; } } /// Birthday public DateTime Birthday { get { return birthday; } } } Do we really need the braces? The indentation already gives us all the clues we need.
  • public class Person(string name, DateTime birthday) = /// Full name public string Name = get { return name; } /// Birthday public DateTime Birthday = get { return birthday; } Do we really need the braces? The indentation already gives us all the clues we need. 6 more lines saved! You can still have explicit begin/end markers for blocks if you need to.
  • public class Person(string name, DateTime birthday) = /// Full name public string Name = get { return name; } /// Birthday public DateTime Birthday = get { return birthday; } Add "equals" as an indicator to start a new block.
  • People who complain about using a language with syntactic whitespace People who have spent time using a language with syntactic whitespace "Oddly enough, Python's use of whitespace stopped feeling unnatural after about twenty minutes. I just indented code, pretty much as I would have done in a C program anyway, and it worked." - Eric Raymond Helpful Venn Diagram Overlap
  • Observat

Recommended

View more >