64 points · by CmonDev · 11y ago · 108 comments
1. Variables should not be allowed to change their type.
2. Objects containing the same values should be equal by default.
3. Comparing objects of different types is a compile-time error.
4. Objects must always be initialized to a valid state. Not doing so is a compile-time error.
5. Once created, objects and collections must be immutable.
6. No nulls allowed.
7. Missing data or errors must be made explicit in the function signature.
I think the writer also provides a pretty practical definition of what it means for a program to be "reasonable". Really nice job!
EDIT: I pasted the wrong list as dsp1234 pointed out :)
This is the preservation component of the formal concept "type safety". Thus any type safe language must achieve this.
> Objects containing the same values should be equal by default.
This one really depends on a lot of things. You may have abstract types in which case equality should not be implicitly defined. You may also have codata types where you cannot merely compare "value containment" since it's no longer a concept. These are both important things which are sometimes called "objects" and shouldn't be eliminated from languages.
Really, this is a lot more complex than it seems. But at first swing: "structural equality should be everywhere and reference equality isn't a genuine concept" is pretty great.
> Comparing objects of different types is a compile-time error.
This is a-OK so long as it's easy to prove when two dissimilar types are actually the same. In cases where that's not true there are other forms of equality which are useful.
> Once created, objects and collections must be immutable.
This is a little overly strong. Regionalized mutability can be made to be referentially transparent from the outside.
---
But man, yeah, this are tiny nits. This list is great.
I think it's important to distinguish equality from equivalence. There are many cases (e.g., enumeration types) in which equality is a perfectly appropriate definition of equivalence; but there are also many cases (e.g., some codata types) in which equality is an inappropriate definition of equivalence. Therefore, equality-as-equivalence as default behaviour is probably a poor choice— though it should be easy to opt for that behaviour.
As for equality (i.e., intensional equality) itself, I think memberwise equality (or perhaps even equivalence?) is a perfectly reasonable definition. There are times when you really want to discriminate on object identity (in the storage-location sense), but you needn't restrict equality to object identity to allow that.
This is very true. In particular, equality ought to be merely a subset of equivalence. For codata types, as mentioned, equality may not be computable (or well-defined at all), but we can compute equivalences such as bisimulation if we like.
For any type which has a nice equality then merely setting equivalence to equality should be fast, I agree. This is essentially `deriving Eq` in Haskell.
Memberwise equality works for some types, but notably fails when we have abstraction. You can get away with it for (pure) functions on a finite domain if you really want but that's a little hacky---or rather, it involves (implicitly) injecting your function into some kind of container [0] and computing equality there.
As you note, codata types aren't likely to have nice equalities. In these cases they're not going to have memberwise equality, either.
[0] First chapter or so of these lecture notes if I remember correctly, http://lambda-the-ultimate.org/node/4804
The first "issue" is that ``DoSomething accesses x directly rather than through the parameter'', but then goes on to talk about types.
I think this is a big part of why we are bad at programming: A because 6 therefore Go Fish.
I have noticed that when a program is small enough that I can see all of it, then I can reason about all of it, and when I cannot see all of the code, then I cannot. I may resort to note-taking or trying to remember while flipping tabs quickly, or even outright guessing, but I certainly am not reasoning about it.
I do not buy that typing variables, or lambdas, or long method names (like `Equal`), or having "compile time", or `null` or "function signatures" offers anything like the gains had by able to see all of the program, so I'm disinclined to waste energy on what I view as premature optimisation.
You've made a powerful observation about how programmers and human minds in general interface with complex systems. What if you had language semantics that could let you ignore most of your codebase with 100% reliability?
I've had a long career dealing with very complex "enterprise" software for multi-nationals. Semantic tricks that let you make far-reaching deductions and securely ignore most of the code are absolutely vital in contexts like that. It was precisely in the parts of the code where things weren't structured to allow this, that things broke down, and everyone dreaded to check-in code.
I'm disinclined to waste energy on what I view as premature optimisation.
There are certain contexts where one already knows that this sort of thing is not premature optimisation. (Granted, lots of programmers seem to make a fetish of acting like they are in such a context, when they really aren't. Analogous to: http://xkcd.com/538/ )
Another thing I've noticed is that having well documented interfaces between small components is extremely effective at improving isolation time.
Another thing I've noticed is that interfaces with two or fewer arguments (none of which boolean) tend to be used correctly more often than interfaces with a large number of arguments, or booleans.
I would very much enjoy having a conversation about those language semantics. I haven't noticed any language semantics that would be useful in that way, however I have noticed a number of these "disciplines" that are helpful at reducing search time, reducing defect count and increase performance (run-time and programmer).
What exactly are you thinking about?
It can be hard to believe how useful good types can be.
Do you believe "good types" in a 1000 line program is easier to reason about than a 20 line program?
Collections I understand, but what's the point of immutable objects? If you're eschewing mutability, why have objects at all? The whole point of objects is to encapsulate mutable state, together with methods for mutating that state.
If you said Once created, data structures must be immutable I would be more inclined to agree.
Citation needed. I use my objects to hold immutable state with methods that does not mutate that state.
push : ('a queue, 'a) -> 'a queue
pop : 'a queue -> ('a queue, 'a) option
In other words, instead of mandating that state updates in objects occurs via global ambient state simply cause objects to return a new version of themselves with the suitable updated state. All your standard immutability tricks now apply. Objects maintain their meaning through their opaqueness/type-abstraction.That's what Clojure does (and what lots of other functional languages do), only they're not "objects," they're just data structures.
Again, if you're not using mutability or inheritance, then why do you need objects at all? What do objects buy you, compared to data types + functions?
In the absence of those features, isn't an object class really just a module with a data type definition and some functions in it?
Here's a counterexample of runtime polymorphism without objects: http://clojure.org/runtime_polymorphism
I'm not arguing against those two conclusions as a pair, just with the order in which the article presents them.
They mean 'can we logically reason about programs written in this language' rather than 'is the language a good choice' for solving problems.
At the risk of baiting flame, I find this distinction oddly amusing. If you can't logically reason about programs written in a language, how is it ever a good choice to use?
You can never perfectly logically reason about any program – your ability to reason about a given program varies on a scale of 'easy to reason about' to 'difficult to reason about'.
While it's not always the case, it's easy to see that in some situations the cost of making a language easier to reason about can outweigh the benefits. The 'goodness' or otherwise of a language must factor in the difficult of reasoning about it, but that itself is not a sufficient condition to describe its quality.
I don't actually disagree with most of his guidelines, but I really don't like his use of that term. There are many situations where his preferences are actually unreasonable and the the opposite of what he has concluded is appropriate, mutable state being an easy example. It should not be a "I'm right and you're unreasonable" type of discussion.
While reasonableness and usefulness are not the same thing, it does seem a language needs to be both. Otherwise you could end up with a language that is both useful and unreasonable, which would be maddening.
function DoSomething (foo) { x = false}
var x = 2;
DoSomething(x);
var y = x - 1;
If you change "foo" to "x" then this works as expected. Who writes code that looks like this anyway? Decent JS developers know that declarations elevate themselves, and they also would not write code that looks like this.
This is like taking a couple words out of the middle of a sentence and complaining because the meaning changing versus the whole overall sentence.
var a_number = 10;
a_number = { blah: 10 };
var a_plus_two = a_number + 4;
Better yet you can simply say "Javascript does not have strict typing" and point people at the wiki article on strict typing. This is clear and concise as an explanation.
Showing confusing examples and saying "oh look this is horrible types can change" does not make the point stronger.
The article uses a whole lot of words and examples to point out problems that professional JS developers are aware of and work hard to avoid.
It also implies that such languages without strict typing are "unreasonable". It's not unreasonable. It's that way on purpose.
If we look at tools used in practice, we should be picking C,C++,Java,C#,VB,Python. If we look at functional languages we should be picking -- not sure, Erlang perhaps.
However, I don't think all of the comparisons are without value, and I would not want all of the discussion to go away. But a little more emphasis on "here's this useful thing I build with language 'X'" would certainly be welcome.
Complaining works cause it makes the shit more visible so they can fix it in later versions of the standard.
In comparison if you write Objective C for iOS, you can debug the Objective C, as opposed to needing to debug in the compiled assembler.
I rather use CoffeeScript instead of JavaScript, and I've forced teams to use CoffeeScript, but the fact that all browsers use JavaScript and everyone "knows" JavaScript make it easier for teams to stick with it.
You can do it sneakily but it could be a problem when you need to follow certain code conventions stablished by the team.
Take a look at this snippet from example 6:
// create a repository
var repo = new CustomerRepository();
// find a customer by id
var customer = repo.GetById(42);
The fault is "I can't tell by looking at this code whether a null is returned or not."The improvement is this (in a language without nulls or exceptions):
// create a repository
var repo = new CustomerRepository();
// find a customer by id and
// return a CustomerOrError result
var customerOrError = repo.GetById(42);
Aside from the comments and a change to the variable name, isn't this code identical? The fact that subsequent code must process the result specially if the input (42) is garbage is true of any language, whether or not null is allowed. I still can't tell "by looking at it" what is happening.In fact, I would reason differently about this code if the comments were removed and the names changed:
var a = new B();
var c = d.E(42);
In this context, it seems the real error is that an invalid parameter value is passed, so I'd see if I could fix that first, possibly eliminating the need for any error handling code. That approach might be common for a functional mindset, but not exclusively so.There are definite benefits to using a language that it's easy to reason about. In most cases, there's also a cost. That's fine – it means we've got the ability to trade off one against the other as the situation requires.
I'm unlikely to write tiny, single-use scripts in a strict, statically-typed language – mostly because the overhead isn't worth it. Similarly, systems that require higher levels of correctness can benefit from the guarantees that are offered by stricter languages.
We have to bear in mind that the entire field of programming languages is still very much evolving, as we gain experience of what works and what doesn't. Type inference, for example, can drastically reduce the overhead of type checking. Or option types, which eliminate a class of errors by providing support for common patterns. No doubt as the field evolves, we'll see that languages emerge which deal with the pain points of both 'reasonable' and 'unreasonable' languages.
There is a big problem with this, however. The entrenched infrastructure and toolset reinforce the "separate" mindset. A useful analogy with which to think about this: The level of granularity we inherited for computer security from the 60's and 70's was to protect one user from another on the same machine. Now, we know we need much finer levels of control, which is why mainstream OS now have "capabilities" or their functional equivalent. However, this took a long time and met much resistance, because the industry as a whole couldn't mentally move beyond a security model developed for timeshare users on a 60's "mainframe."
I'm sorry, it's just too much for me. Yes, PL theory advances and yes, there are many new, interesting ideas being presented each year. That's true.
But your example, "type inference", dates back to the sixties. Similarly for "option types". Using these as an example of "evolution of entire field of programming languages" would suggest this evolution happens at a glacial pace. Which is indeed true for the so-called mainstream languages, but "the entire field" is much, much larger than those, moves far quicker and comes up with much more interesting ideas every year.
We don't just pull language features out of our collective arses and start using them. Evolution does happen at a glacial pace – we stick features into minority-appeal languages; they kick around a bit; people rediscover these features later, and maybe they eventually end up in popular languages. But there's a huge amount more involved in that process than simply coming up with the feature - testing, maintainability, long-term implications, unforeseen complexity, programmer acceptance, performance constraints… the list goes on.
No, it's actually just "programmer acceptance" and secondary reasons like amount of money poured into marketing or language being tied to an OS. I'm not very pleased with this. In this talk: https://thestrangeloop.com/sessions/the-sociology-of-program... it's said that probability of language being used is "potential gains/pain of adoption" ratio. That's true. Language designers, at least the ones who want their languages to succeed, constrain themselves to make it as painless as possible for "normal programmers" to pick up. Nobody seems to notice that there's another option of making denominator smaller: all you need is to become better at adopting new technologies. But this somehow doesn't work, because... Probably because of long-term implications or performance constraints, right.
There's a cost to all of this, and there are better options. But it's really not hard to see why Javascript is gaining traction on the server side, as rubbish as it is.
I really want to like F#, but that's a really big "in general". I love the idea of option types, but I often hear with regards to F#, "forget about NullReferenceExceptions!". Yeah, that's nice, except I sometimes use strings in my code...
What I would've liked is to have F# strictly forbid nullable references, and treat all reference types in interop situations as (implicitly) options. Not sure if that would be possible, haven't given it too much thought and I'm sure there are tons of tricky cases that haven't occurred to me, but I haven't really seen this discussed anywhere.
You can write pretty functional code if you want but you also have the option to write more mutable/side effecty/stateful object code if you want.
Yeah... another batch of innovative, interesting languages died because "it's not practical", another pile of language features didn't manage to get implemented because "but look at syntax, it's ugly", another bunch of languages took some decades old features, dumbed them down as much as possible and then some more (because "it would be to hard to use otherwise") and marketed them as ground-breaking improvements to the art of programming.
Very good year for PLs, indeed.
The entire field of programming language design is more complex than you seem to be admitting.
But other than that I really, really hope that struct-only coding takes over in Swift given time... perhaps lots of time.
Completely disagree.
In the author's example in support of "Variables should not be allowed to change their type," the variable "x" is subject to coercion, but the real problem was poor scoping by using the global value instead of the parameter.
This would be the same scoping problem, even if the variable type was not changed. Any non-deterministic function that modifies existing state can be said to produce unexpected/unpredictable/unreasonable results. In this case, the unpredictability was not due to a variable changing type.