Chapter 3 - Language Design Principles. Thought question. What characteristics should be present in your “perfect language”? What have you liked/disliked about Ruby?. The language design problem. Language design is difficult, and success is hard to predict:
(saw this as a goal for first time in Cobol)
What language features encourage the production of reliable code?Can an IDE compensate? Analogy: word with spell/grammar checking. Make far fewer mistakes.
rigorous type checking
eliminate undefined variables (always init)
runtime checks (array bounds, types)
less implicit computation (force explicit casts)
compiler helps (check for declared but not used)
correct return value types
(if a=b when mean if a==b)
Example: I can ask for ‘each’ element of an array, hash, file, range, etc. It always makes sense and does the expected thing.
At seats, from Ruby give examples of orthogonality.
At seats, from C++ give examples of non-orthogonality.
a relatively small set of primitive constructs can be combined in a relatively small number of ways. Every possible combination is legal.
Is orthogonal if it can be used without consideration as to how its use will affect something else (no side effects)
Language features are orthogonal when they can be understood in isolation and freely combined with no surprises. Orthogonality thus allows a simple definition to cover lots of possibilities.
Orthogonality makes it easier to understand what happens when things combine.
Is closely related to simplicity - the more orthogonal, the fewer rules to remember.
Can make implementation more complicated as implement things that might not really need.
Understatement is a form of speech which contains an expression of less strength than what would be expected.
Where does non-orthogonality come from? If it is “bad design”, why not eliminate it?
Simplicity: make things as simple as possible, but not simpler (Einstein). (Pascal, C)
Expressiveness: make it possible to express conceptual abstractions directly and simply. (Scheme)
Types, operators, create libraries, extend classes.
In Ruby, we were able to extend even built-in types.
If it isn’t clear, there will be differences in implementations.
Example: Declaration in local scope (for loop) unknown/known after exit
Example: implementation of switch statement
Example: constants – expressions or not?
Example: how much accuracy of float?
Example: order of operations
polymorphism - providing a single interface to entities of different types.
– dynamic or compile time resolution