Syntax Design - Expressing Intent and Pronounceability principles #2075
Replies: 2 comments 1 reply
-
I guess my personal preference would be to get rid of both
One advantage of having
But the syntax is easy to change later, so it is more important to get the semantics right for v0.1? |
Beta Was this translation helpful? Give feedback.
-
I think |
Beta Was this translation helpful? Give feedback.
-
I watched the intro, I really liked the back-to "first principles" approach of the language and the Interop method of succeeding C++
I would like to suggest 2 core principles that I think will really help with syntax design.
The first is the Expression of intent
I think we all agree that code should express intent (at least anyone who watched herb's talks).
Taking this to the language design level will not only help make code more readable, but will also make learning and teaching the language much easier too!
Carbon does it right in some places but it gets it wrong in others, I will give some basic examples:
For instance, a good example in current carbon design is inheritance using the
abstract
,impl
andvirtual
keywords.This design is very explicit and easier to understand
making the difference between virtual and pure virtual functions trivially teachable as opposed to C++
another good example is
interface
which is clearer thanconcept
and also utilizes the same syntax as when declaring a class making it easier to teach.A bad example in current design would be the
let
andvar
keywordsImagine asking a developer new to the language the following question:
"In the example below what do you think is the difference between variable
a
andb
?In Carbon:
In C++
The answer is of course that
a
is constant\immutable andb
is mutable.But in which language would the learner have an easier time learning?
The use of the keyword
const
expresses intent and thus makes the code more readable and easier to teach whilelet
does not offer that.another advantage of the C++ style of declaration is that changing the constness of a variable is easier:
You either delete the const or you add it. no delete one word then write the other. this also makes spotting and comprehending changes much easier when reviewing diffs.
My suggested syntax would be to just remove the keyword
let
and combineconst
withvar
this should also not hurt parsing and can be done in two flavors:Either joining the constness with the type:
var a: const i32; var b: i32;
Or joining it with var (which would be a drop and replace with let)
const var a: i32; var b: i32;
perhaps only one should be possible or maybe both (imagine declaring
Vector(const i32)
vsVector(i32)
)Another principle that should be considered is Pronounceability - ensuring the code is as pronounceable as possible(without making it too cumbersome)
This means that it is easy to read out loud or voice in your head, Making it easier to talk about with other teammates, and generally more human-friendly.
This also ties in with readability and works very well with the previous principle.
Making code easier to read and understand.
Try to read out loud each of the following examples:
Good:
Bad:
Good:
function Partition[T as Comparable and Movable](slice: Slice(T)) -> int64 {...}
Bad:
perhaps
is
orrequires
would be better thanas
but either way this is much easier to read, understand and teach.I hope you take my suggestions into consideration and create a language that is both explicit and easy to read and reason about! :)
Beta Was this translation helpful? Give feedback.
All reactions