- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Time to encourage meaningful type parameter names? Discuss
This comes from thinking about the alleged complexity of Scala (others' arguments have convinced me it is a real problem in the corporate world), my own experiences as I start to penetrate a little bit of the "inner core" of Scala, and most of all, trying to be productive and ask, "Are there simple ways to mitigate the complexity problem?" (Assuming you believe it is a problem.)
First, consider the following method:
def someMeaningfulName(x: Int, y: String, z: Long) {...}
I don't know about you, but I have never worked in an environment where this would be considered acceptable code (unless x, y, and z actually had special meaning in the problem space). I think it's a given that good software engineering demands meaningful variable names, both internal and visible.
Now consider the following type signature:flatMap [B, That] (f: (A) ⇒ GenTraversableOnce[B])(implicit bf: CanBuildFrom[List[A], B, That]): That
There are three type parameters, A, B, and That. None are meaningful. Due to the fortunate inclusion of f: in the type, it's fairly easy to figure out the meanings of A and B. We can finally figure out the meaning of That when we reach the end of the line, at which point we need to reread the entire line to put everything together. (OK, you may not. I do, and I submit that most programmers will need to do so once or more.)
In fairness, there was a valid reason for using one-char type param back when type params were new; the declarations were simply (usually container classes), and the one-char rule made it obvious what was a type param and what was an actual type. Note the above def'n has already violated the one-char rule, without really adding value.
How about this
flatMap [resultElementType, resultType] (f: (inputElementType) ⇒ GenTraversableOnce[resultElementType])(implicit bf: CanBuildFrom[List[inputElementType], resultElementType, resultType]): resultType
This is much more verbose, but to someone unaccustomed to Scala collections, it is also much clearer. I've taken the liberty of using initial lowercase to denote a parameter rather than an actual type; I believe this is illegal, but I simply wanted to show how things could be made more readable.
If you're an experienced Scala user, this may not seem a big deal, just extra characters. Consider the process of reading the type signature from left to right, understanding as we go:
flatMap [B, That]
vs. flatMap [resultElementType, resultType]
The second has immediate meaning. The first does not.
The example I chose was mid-level in complexity--it is as the top of what a Scala novice should encounter, but nowhere near as complex as one might encounter in Scala internals or scalaz.
I believe that encouraging meaninful type parameter names would help the perceived "Scala complexity" problem in two ways. First, it makes the more complex "beginner's" Scala signatures more immediately understandable to beginners. Second, it make intellectually sophisticated Scala code more acceptable to the mainstream by making it more readable.
OK, that's it. Discuss :-)
Thanks,Ken
First, consider the following method:
def someMeaningfulName(x: Int, y: String, z: Long) {...}
I don't know about you, but I have never worked in an environment where this would be considered acceptable code (unless x, y, and z actually had special meaning in the problem space). I think it's a given that good software engineering demands meaningful variable names, both internal and visible.
Now consider the following type signature:flatMap [B, That] (f: (A) ⇒ GenTraversableOnce[B])(implicit bf: CanBuildFrom[List[A], B, That]): That
There are three type parameters, A, B, and That. None are meaningful. Due to the fortunate inclusion of f: in the type, it's fairly easy to figure out the meanings of A and B. We can finally figure out the meaning of That when we reach the end of the line, at which point we need to reread the entire line to put everything together. (OK, you may not. I do, and I submit that most programmers will need to do so once or more.)
In fairness, there was a valid reason for using one-char type param back when type params were new; the declarations were simply (usually container classes), and the one-char rule made it obvious what was a type param and what was an actual type. Note the above def'n has already violated the one-char rule, without really adding value.
How about this
flatMap [resultElementType, resultType] (f: (inputElementType) ⇒ GenTraversableOnce[resultElementType])(implicit bf: CanBuildFrom[List[inputElementType], resultElementType, resultType]): resultType
This is much more verbose, but to someone unaccustomed to Scala collections, it is also much clearer. I've taken the liberty of using initial lowercase to denote a parameter rather than an actual type; I believe this is illegal, but I simply wanted to show how things could be made more readable.
If you're an experienced Scala user, this may not seem a big deal, just extra characters. Consider the process of reading the type signature from left to right, understanding as we go:
flatMap [B, That]
vs. flatMap [resultElementType, resultType]
The second has immediate meaning. The first does not.
The example I chose was mid-level in complexity--it is as the top of what a Scala novice should encounter, but nowhere near as complex as one might encounter in Scala internals or scalaz.
I believe that encouraging meaninful type parameter names would help the perceived "Scala complexity" problem in two ways. First, it makes the more complex "beginner's" Scala signatures more immediately understandable to beginners. Second, it make intellectually sophisticated Scala code more acceptable to the mainstream by making it more readable.
OK, that's it. Discuss :-)
Thanks,Ken










Re: Time to encourage meaningful type parameter names? Discuss
Yet another thread that probably belongs on scala-debate.
AND to answer with my opinion : I think using meaningful type parameter names can make sense, but there's a balance to be had.
On Nov 16, 2011 1:53 PM, "Ken McDonald" <ykkenmcd [at] gmail [dot] com> wrote:Re: Time to encourage meaningful type parameter names? Discuss
Yet another thread that probably belongs on scala-debate.
AND to answer with my opinion : I think using meaningful type parameter names can make sense, but there's a balance to be had.
On Nov 16, 2011 1:53 PM, "Ken McDonald" <ykkenmcd [at] gmail [dot] com> wrote:Re: Time to encourage meaningful type parameter names? Discuss
On Thursday, November 17, 2011 6:51:01 AM UTC-6, Josh Suereth wrote:Always there must be balance, young friend. (Yoda waves his light-saber).
OK, a number of people have recommended scala-debate as the proper forum for such ideas. I regard that as the "graveyard of ideas", and I try to put a lot of consideration into what I post so that I don't post about either minor or too-difficult-to-fix ideas. So what is the proper forum to someone who is novice/intermediate to Scala, and wants to post real concerns about the language that, by their own experience, could cause Scala to fail or do poorly? I would suggest that at the moment, there is not an adequate feedback mechanism from the real world as to what Scala needs to do to succeed.
And yes, I want to to succeed. I'm put a lot of time into it already, dagnabit!
Ken
Re: Time to encourage meaningful type parameter names? Discuss
On Thursday, November 17, 2011 6:51:01 AM UTC-6, Josh Suereth wrote:Always there must be balance, young friend. (Yoda waves his light-saber).
OK, a number of people have recommended scala-debate as the proper forum for such ideas. I regard that as the "graveyard of ideas", and I try to put a lot of consideration into what I post so that I don't post about either minor or too-difficult-to-fix ideas. So what is the proper forum to someone who is novice/intermediate to Scala, and wants to post real concerns about the language that, by their own experience, could cause Scala to fail or do poorly? I would suggest that at the moment, there is not an adequate feedback mechanism from the real world as to what Scala needs to do to succeed.
And yes, I want to to succeed. I'm put a lot of time into it already, dagnabit!
Ken
Re: Time to encourage meaningful type parameter names? Discuss
On 17/11/2011 13:51, Josh Suereth wrote:
> Yet another thread that probably belongs on scala-debate.
>
> AND to answer with my opinion : I think using meaningful type parameter names can make
> sense, but there's a balance to be had.
Yes.
When I discovered Scala, I was disconcerted, not to say "shocked", by these one-letter
type names. Looked like those old C programs where developers valued conciseness over
expressiveness. You know, lines like: if (lh->v.u.s.aux == v->u.s.info)
Then I read that's a tradition in computer science, so I get used to it...
But I still believe that some well chosen names can help.
Of course, you can put signatures like those mentioned, and be more explicit in the
documentation of the API, but indeed, that's two places to look for.
On the other hand, there can be some confusion between real types (classes, etc.) and
abstract types (those used in collections, not knowing in advance what they would be);
perhaps even potential clashes (?).
Ie. is a ResultElement (I agree with Viktor, the "Type" part is a bit redundant) a real,
application level type or some abstract type? Can I have a ResultElement class in my app.
without conflict?
Specialized APIs like Graph[Node, Edge] are more OK, I suppose. Not sure about conflicts,
again.
Re: Re: Time to encourage meaningful type parameter names? Disc
On 17 November 2011 14:34, Philippe Lhoste <PhiLho [at] gmx [dot] net> wrote:
Same here. I'd previously done a lot of Java and a few years of Haskell. I found much of the haskell libs to be impenetrable due to single-letter (and often non-mnemonic) type names, even after quite a while working with them.
Agreed. I have type parameters rendered in italic and type alases as italic+bold in intellij idea. I'm not a fan of Christmas-tree highlighting, but this seems to help. Matthew
Re: Re: Time to encourage meaningful type parameter names? Disc
On 17 November 2011 14:34, Philippe Lhoste <PhiLho [at] gmx [dot] net> wrote:
Same here. I'd previously done a lot of Java and a few years of Haskell. I found much of the haskell libs to be impenetrable due to single-letter (and often non-mnemonic) type names, even after quite a while working with them.
Agreed. I have type parameters rendered in italic and type alases as italic+bold in intellij idea. I'm not a fan of Christmas-tree highlighting, but this seems to help. Matthew
Re: Re: Time to encourage meaningful type parameter names? Disc
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am 17.11.2011 15:47, schrieb Matthew Pocock:
> Hi,
>
> Agreed. I have type parameters rendered in italic and type alases as
> italic+bold in intellij idea. I'm not a fan of Christmas-tree highlighting,
> but this seems to help.
+1 for Christmas-tree highlighting :) (let's abbr. it to CTH).
Maybe we should start a feature request for jline, for CTH in the REPL.
Re: Re: Re: Time to encourage meaningful type parameter names?
Guys, it seems The utility of this discussion has declined. Let's refrain from bloodying noses by continuing in the current vein.
I'll throw something out there.
Type parameters *are parameters * just like method parameters. If you think method parameters require a certain naming style, it is probably worthwhile to use this on type parameters.
If a type parameter looks like a class/type, that's similar to a method parameter looking like a member / term. Notice the adjustments to the unofficial style guide for this.
On Nov 17, 2011 11:39 PM, "Stefan Wagner" <hirnstrom [at] arcor [dot] de> wrote:Re: Re: Re: Time to encourage meaningful type parameter names?
On Thursday, November 17, 2011 10:55:59 PM UTC-6, Josh Suereth wrote:Could you throw out a ref to the unofficial style guide?
Ken
Re: Re: Re: Time to encourage meaningful type parameter names?
It seems that most up-to-date lives now here: http://docs.scala-lang.org/style/
On Sat, Nov 19, 2011 at 5:31 PM, Kenneth McDonald <ykkenmcd [at] gmail [dot] com> wrote:
Re: Re: Re: Time to encourage meaningful type parameter names?
Yes and no. Method parameters have a type, type parameterscan have constraints but often don't.
A type parameter without constraints is like a method parameter of type 'Any'. When you are trying to assign meaningful names to them you end up doing something like
def substring(str: Any, startPos: Any, endPos: Any)
which is good indicator that you are doing something wrong.
On Thu, Nov 17, 2011 at 8:55 PM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
Re: Re: Re: Time to encourage meaningful type parameter names?
Type parameters have kinds. Method parameters have types. I don't see any value to your logic.
This is like saying if I accept a method of type Any, then its name is meaningless.
On Nov 18, 2011 12:15 AM, "Artem Khodyush" <greenkaa [at] gmail [dot] com> wrote:Re: Re: Re: Time to encourage meaningful type parameter names?
> This is like saying if I accept a method of type Any, then its name is meaningless.
Yes it is meaningless because you can't do anything meaningful with Any. To beginwith, there aren't that many useful methods that take Any. Let's look at one:
override def equals(that: Any) = that match { ...
}
Note how it's immediately begins with the code that figures out at run time what 'that' means, and you can make a good guess what the code doesbased on the method name, but parameter name is utterly irrelevant.
The same goes for unconstrained type parameters - there are alwaysbetter and explicit ways to express their meaning than encoding it innames and hoping for people to follow conventions.
, exceptfor calling methods like equals, toString, and hashCode, defined by mistake in the root object. In fact, ideally there should't be any value of type Any mentioned in statically checked program.
Types, on the other hand,
On Fri, Nov 18, 2011 at 5:55 AM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
Re: Re: Re: Time to encourage meaningful type parameter names?
Oops sorry I send almost finished draft - please ignore the last paragraph whichstarts with a comma and does not make sense anyway.
On Fri, Nov 18, 2011 at 9:25 AM, Artem Khodyush <greenkaa [at] gmail [dot] com> wrote:
Re: Time to encourage meaningful type parameter names? Discuss
On 16/11/2011 19:53, Ken McDonald wrote:
> OK, that's it. Discuss :-)
Aren't this kind of thread, and the others you started, belonging more to scala-debate?
scala-user description states it is about "questions and discussions about the Scala
programming" (mm, is there an extra "the" or a missing "language"? the meaning isn't the
same...)
scala-debate is about "questions and suggestions around the future of Scala" among others.
In fact, most of these discussions end to scala-debate, but often late.
Perhaps I am too picky about these questions, maybe because I am moderator in some
forums... :-)
Re: Time to encourage meaningful type parameter names? Discuss
I find GenTraversableOnce and the implicit CanBuildFrom stuff to be the startling bits. However, along with a few examples of how flatMap is used, the mystery dissolves. Or, rather, you understand that you don't need to understand the mystery. Short (and consistent) type signatures along with a few examples of usage, that's the ticket. Long names hurt eyes.
D.
Re: Time to encourage meaningful type parameter names? Discuss
So long variable names hurt eyes?
This is a serious question. Why are long variable names good, and long type parameter names bad?
Ken
Re: Time to encourage meaningful type parameter names? Discuss
For me it makes it harder to visualize what the signature of the method actually is. When I look at flatMap, the signature 'A => M[B]' should be easy to see, and it isn't easy for me to see it in the verbose version. I would end up being dependent on the documentation to tell me what the method does, rather then just looking at the signature.
I have the same problem with java code that uses many long method names all jumbled together.
Re: Time to encourage meaningful type parameter names? Discuss
On 11/17/2011 01:33 PM, Derek Williams wrote:
>
> For me it makes it harder to visualize what the signature of the
> method actually is. When I look at flatMap, the signature 'A => M[B]'
> should be easy to see, and it isn't easy for me to see it in the
> verbose version. I would end up being dependent on the documentation
> to tell me what the method does, rather then just looking at the
> signature.
>
> I have the same problem with java code that uses many long method
> names all jumbled together.
>
What's wrong with this? Much easier to read:
FingWhatIsComingOffDaMonadEnvironment =>
FingWhatIsAMonadEnvironment[NewFingWhatTheMonadEnvironmentIsDefinedOver]
PS: Isn't this another -debatey topic?
Re: Time to encourage meaningful type parameter names? Discuss
Tony,
Hi, thanks for the contribution. I'm always aware, when I post topics like this, that perhaps they should be in scala-debate. I try to run my ideas through a filter, which roughly speaking, asks
1) Am I proposing an EASY solution (such as simply lengthening type variable names).2) Does this address a serious perceived problem with Scala (such as the corporate perception that Scala is "too complex")3) Am I proposing something that is closed-ended,in the sense that it will not devolve into (somewhat :-) ) infinite debates over detail. In other words, am I presenting people with an easy choice? The goal of this last point is to avoid flame wars and indefinite posts back to the same subject. Such topics should definitely be in scala-debate.4) Does this address something that I, still a Scala novice, have found to be a genuine problem and believe would be a genuine problem in the broader world. I admit this is a subjective judgement, I do the best I can :-).
I am using myself as a proxy for a "new Scala user". I want Scala to succeed, so I believe that Scala problems that are SERIOUS problems for new users should be discussed in a mainstream group such as scala-user, not in a peripheral group such as scala-debate. I run the above filter on the posts I make to try to ensure that my posts are appropriate to the general Scala user community.
Finally, I monitor the responses to my posts to ensure that I do not generate a useless bunch of messages with no final agreement. For example,I recently asked what the top needs are for Scala to succeed in the enterprise. Obviously there was not complete agreement on this, but a good Eclipse IDE (with the implication that the current Eclipse IDE is till not satisfactory) was a strong result. This was not a surprise, but provides continuing empirical feedback as to what is needed in the Scala ecosystem, which I believe to be valuable.
Sorry for the long answer, but I wanted to explain my rationale for doing what I'm doing. Feel free to post counterarguments.
Cheers,Ken
Re: Time to encourage meaningful type parameter names? Discuss
Scala debate is not just for meaningless discussions. I'm sending this to scala-user so folks understand.
If you want to open a discussion on an idea, scala-debate is perfect for this. If you have a question on using scala, scala-user is good.
The original email seemed to either be a scala-debate discussion or a scala-internals suggestion. In either case don't think moving to scala-debate means you're being "silenced". We just want a place to record these discussions that isn't jumbled with questions like "why doesn't Foo compile"
On Nov 17, 2011 3:11 PM, "Ken McDonald" <ykkenmcd [at] gmail [dot] com> wrote:Re: Time to encourage meaningful type parameter names? Discuss
Ken
Re: Time to encourage meaningful type parameter names? Discuss
I think you're using the wrong criteria for -debate vs. -user. -user is good for things that users care about and which they can employ. -debate is good for things which need to be discussed and perhaps cannot be employed by users (because e.g. they're changes to the core library, and most users don't recompile their own core library).
On Thu, Nov 17, 2011 at 3:11 PM, Ken McDonald <ykkenmcd [at] gmail [dot] com> wrote:
If it's a proposal for users to use longer type variable names, yes, it's easy.
It is _related to_ that discussion, I agree, but it is not obvious that it is a _solution_, nor that -users who really just want to give and receive information on how to use Scala would be widely interested.
The topic doesn't pass this criteron. There is a significant component here that is normally addressed subjectively ("I like long names"), which makes for long debates. Perhaps you were unaware that long names were disliked by a non-negligible number of people, at least for certain use cases; that's understandable and forgivable.
It addresses it, but may go the wrong way. A giant incomprehensible block of text and symbols is no more comprehensible if replaced by an even more giant block (where text and/or symbols have been replaced with longer versions).
Another possible solution is to have a set of links on every collections class to useful resources: "Understanding Collection Types", "Collection Performance", "How to Extend a Collection", etc..
You might then start wishing That were named CB (C meaning "some collection" and B meaning--well, the same B as before), and CanBuildFrom was named Cbf:
def ++[B >: A](elem: B)(implicit cbf: Cbf[Seq[A],B,CB])
Isn't that easy? You take Seq[A], add a B to it, and get a CB?*
def ++[NewElementType >: ExistingType](elem: NewElementType)(implicit cbf: CanBuildFrom[Seq[ExistingType],NewElementType,NewCollectionType])
It's self documenting, yes, but the flow of content has almost entirely been obscured.
Now, in the face of poor documentation, one may wish for the latter form since the type signature is a form of documentation. (Not a very _good_ form of documentation, but it at least gets you thinking in some of the appropriate directions, while brief version may, to the uninitiated, motivate no thinking at all except WTF?)
But the solution is not to obscure program flow with long type names. The solution is to provide easily accessible documentation. If you know what it means, look at it and, presto! all the relevant information leaps out at you in a compact way. If you don't know what it means, read the documentation, and presto! you can work your way through what is going on.
I appreciate your initiative in bringing up these issues. But you have to keep in mind as a new user that while your perspective may reflect common problems of new users, your solutions may or may not be the best ones simply because you--thus far--lack the expertise necessary to appreciate the value of other solutions. So even if your post elicited a whole bunch of "yeah!" and "+1" replies from other users, it wouldn't necessarily follow that your solution was a good one, or that further discussion on -user would lead to a good solution. It _would_ mean that there was a major source of difficulty out there that should be addressed.
I think the solution is documentation, as I've argued, but I _don't_ think it is obvious that all Scala users really want to listen to and take part in that discussion.
--Rex
* The downside of CB, of course, is that it makes you imagine that the collection _must_ have something to do with B. For the majority of CanBuildFrom instances, this is true. As far as the type constraints go, though, A could be String, B could be Option[Option[Boolean]], and CB _could_ be Int. In this sense, "That" is more honest.
Re: Time to encourage meaningful type parameter names? Discuss
On 17 November 2011 05:03, Tony Morris <tonymorris [at] gmail [dot] com> wrote:
Names can be too short or too long. This goes for variables, defs, classes and parametrized types. Picking strawman long-names doesn't invalidate the principle of choosing informative names.
Matthew
--
Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle Universitymailto: turingatemyhamster [at] gmail [dot] com gchat: turingatemyhamster [at] gmail [dot] commsn: matthew_pocock [at] yahoo [dot] co [dot] uk irc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
Re: Time to encourage meaningful type parameter names? Discuss
On 17/11/11 20:48, Matthew Pocock wrote:
> On 17 November 2011 05:03, Tony Morris wrote:
>
>
>> What's wrong with this? Much easier to read:
>>
>> FingWhatIsComingOffDaMonadEnvironment =>
>> FingWhatIsAMonadEnvironment[NewFingWhatTheMonadEnvironmentIsDefinedOver]
>>
>>
> Names can be too short or too long. This goes for variables, defs, classes
> and parametrized types. Picking strawman long-names doesn't invalidate the
> principle of choosing informative names.
>
> Matthew
>
>
>> --
>> Tony Morris
>> http://tmorris.net/
>>
>>
>>
>
It is extremely important to pick meaningless names. The moment you
project meaning is the moment you start making all those mistakes that
we so often see.
Re: Time to encourage meaningful type parameter names? Discuss
Many times the types are not devoid of meaning. Consider:
trait Graph[A, B]
vs
trait Graph[N, E]
vs
trait Graph[Node, Edge]
vs
trait Graph[TheNodeType, TheEdgeType]
Option 3 of these is (imao) optimal, as it clearly describes what role the types play without using unnecessarily verbose names. If you're not 'projecting meaning' on these two type parameters, you're not understanding the API. I've no problem with List[A], as there's nothing much to tell users about A. In a library like scalaz, consistent use of single-letter type parameters goes a long way, but then you have things like what the default letter should be for Monad vs Monoid.
I'll bug out here, as I agree this should go to -debate until/unless it results in an agreement to go through the core libs renaming things, in which case I'd be happy to give up a 'documentation saturday' to helping rename things.
Matthew
--
Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle Universitymailto: turingatemyhamster [at] gmail [dot] com gchat: turingatemyhamster [at] gmail [dot] commsn: matthew_pocock [at] yahoo [dot] co [dot] uk irc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
Re: Time to encourage meaningful type parameter names? Discuss
Bad. Now I have to remember whether your graphs have nodes first or edges first. I don't want to have to do that.
Good! Now I don't have to remember!
Why all the extra letters? I already knew everything I needed to know.
This is just mean.
Actually, these are all bad. Shouldn't it be
trait Graph[N <: Node, E <: Edge]
where Node and Edge are some traits that define the minimal possible interface required for something to be considered a node or an edge? Maybe even
trait Graph[N <: Node, E <: Edge[N]]
depending on the edge interface?
Now we can see why Graph[Node, Edge] is the wrong choice. You have labeled the types with _what you want_ without specifying that _they must be what you want_. The more you insist with your names that you will in fact put the correct thing in that slot, the less likely you are to make the compiler insist on your behalf, and therefore the more burden you are taking upon yourself.
This is why I think the sweet spot is [N,E]. You want _some_ mnemonic because you want to know for yourself (as well as the compiler knowing) that nodes come before edges. But [Node, Edge] is deceptive, and also steals the name Node and Edge away from what you probably really want (which are Node and Edge traits or classes).
--Rex
Re: Time to encourage meaningful type parameter names? Discuss
On Thursday, November 17, 2011 12:45:56 PM UTC-6, Rex Kerr wrote:I disagree completely. To you, these extra characters may be unneeded but harmless; to another, they may be vital. This is nothing more than that argument as to whether the loop counter should be n or loopCounter; IMHO, it should clearly be the latter (assuming you must do imperative programming.)
Why the freak are people so opposed to a few extra characters that don't even interfere with reading speed? (Humans read in phrases and words not in characters.) just don't get it.
Ken
Re: Re: Time to encourage meaningful type parameter names? Dis
Anyone who doesn't know that graphs are made up of nodes and edges should stay far, far away from a graph library until they take some graph theory.
Yes, this is very similar. The extra ten characters are just visual clutter. Which is easier to understand at a glance:
for (n <- 1 to 10) a(n+1) = a(n) + a(n-1)
or
for (n <- 1 to 10) a(loopCounter+1) = a(loopCounter) + a(loopCounter-1)
Because they do interfere with reading speed. I'm not reading "loopCounter" alone, I'm reading a(n+1). Instead of reading the whole operation, like a*b + c, in one go, I have to parse each word.
I don't object out of some fit of pique. I object because it gets in the way of grasping the concept quickly, both visually and conceptually. It's like touch-typing. You could argue that the keyboard should be laid out ABCDEFG... because it's so much easier for people who look at the keys. Also, people don't type words, they type letters, so what's the big deal about using one finger at a time?
There is a place for longer variable names, but I don't think index variables or generic types (which serve a similar role) are the place.
--Rex
Re: Re: Time to encourage meaningful type parameter names? Dis
Am 17.11.2011 19:45, schrieb Rex Kerr:
Re: Re: Time to encourage meaningful type parameter names? Dis
On Thu, Nov 17, 2011 at 1:45 PM, Rex Kerr wrote:
> This is why I think the sweet spot is [N,E]. You want _some_ mnemonic
> because you want to know for yourself (as well as the compiler knowing) that
> nodes come before edges. But [Node, Edge] is deceptive, and also steals the
> name Node and Edge away from what you probably really want (which are Node
> and Edge traits or classes).
+1
Re: Re: Time to encourage meaningful type parameter names? Dis
On Thu, Nov 17, 2011 at 01:45:56PM -0500, Rex Kerr wrote:
> This is why I think the sweet spot is [N,E]. You want _some_ mnemonic
> because you want to know for yourself (as well as the compiler knowing)
> that nodes come before edges. But [Node, Edge] is deceptive, and also
> steals the name Node and Edge away from what you probably really want
> (which are Node and Edge traits or classes).
Agreed.
I find the single letter convention actually makes the use of generics
more obvious to me and easier to reason about. If I had to figure out
whether everything that looked like "Node" was a type parameter or a
concrete type I would be sad. [1]
Re: Re: Time to encourage meaningful type parameter names? Dis
Ken
Re: Re: Time to encourage meaningful type parameter names? Dis
Re: Re: Time to encourage meaningful type parameter names? Dis
+1
On Thu, Nov 17, 2011 at 11:01 AM, Erik Osheim wrote:
> On Thu, Nov 17, 2011 at 01:45:56PM -0500, Rex Kerr wrote:
>> This is why I think the sweet spot is [N,E]. You want _some_ mnemonic
>> because you want to know for yourself (as well as the compiler knowing)
>> that nodes come before edges. But [Node, Edge] is deceptive, and also
>> steals the name Node and Edge away from what you probably really want
>> (which are Node and Edge traits or classes).
>
> Agreed.
>
> I find the single letter convention actually makes the use of generics
> more obvious to me and easier to reason about. If I had to figure out
> whether everything that looked like "Node" was a type parameter or a
> concrete type I would be sad. [1]
>
Re: Time to encourage meaningful type parameter names? Discuss
On Thu, Nov 17, 2011 at 7:56 AM, Matthew Pocock <turingatemyhamster [at] gmail [dot] com> wrote:
Re: Time to encourage meaningful type parameter names? Discuss
On 17/11/11 22:56, Matthew Pocock wrote:
> On 17 November 2011 12:11, Tony Morris wrote:
>
>> It is extremely important to pick meaningless names. The moment you
>> project meaning is the moment you start making all those mistakes that
>> we so often see.
>>
> Many times the types are not devoid of meaning. Consider:
>
> trait Graph[A, B]
>
> vs
>
> trait Graph[N, E]
>
> vs
>
> trait Graph[Node, Edge]
>
> vs
>
> trait Graph[TheNodeType, TheEdgeType]
>
> Option 3 of these is (imao) optimal, as it clearly describes what role the
> types play without using unnecessarily verbose names. If you're not
> 'projecting meaning' on these two type parameters, you're not understanding
> the API. I've no problem with List[A], as there's nothing much to tell
> users about A. In a library like scalaz, consistent use of single-letter
> type parameters goes a long way, but then you have things like what the
> default letter should be for Monad vs Monoid.
>
> I'll bug out here, as I agree this should go to -debate until/unless it
> results in an agreement to go through the core libs renaming things, in
> which case I'd be happy to give up a 'documentation saturday' to helping
> rename things.
>
> Matthew
>
>
>> --
>> Tony Morris
>> http://tmorris.net/
>>
>>
>>
>
This is exactly the mistake I refer to. Now define your operations on
the data type. What do those operations (the important bits) say about
nodes and edges? Exactly, nothing, nothing at all -- now you're stuck in
a cognitive quagmire. Please allow me to politely decline your invitation.
Re: Time to encourage meaningful type parameter names? Discuss
On 17 November 2011 13:13, Tony Morris <tonymorris [at] gmail [dot] com> wrote:
There is no meaningful isomorphism that I can repeatedly apply to Graph[Node, Edge] to get a Graph[Edge, Node] and then apply again to get back to the original graph. The nodes and edges play distinct roles. Naming them after these roles is useful documentation for the user.
It tells me a great deal about the graph API. It doesn't tell me anything about the specific choices of concrete types for Node/Edge, but that is not what type parameter names are for. It tells me what role they play in the graph, which is what type parameter names are for.
Re: Time to encourage meaningful type parameter names? Discuss
Write a few of your graph operations. Use the type parameters as if they were nodes or edges. Notice how you can't. The proposal is akin to projecting meaning onto universally quantified variables of a logical proposition. We say, paraphrased:
For all c such that c is an element of the set Cars...
Notice how we call the variable c, not car or any other particular meaning. Doing so would degrade readability to the extent that it may distract from the fact that this variable should contain no further context than its existence. The constraint of set membership is simply an application to an operation, no different to a list sort operation requiring ordering on its elements.
It is extremely important that type variable names contain no context (that is, call it what you want but I won't be projecting an imaginary context onto it). I am only motivated to point this out because one day someone is going to bring this point up IRL and I am going to have to deal with it. I don't want to; this elusive insight that I am apparently missing according to "the naming guys" is repeatedly shown to be a failed thesis and now it is crawling into the least welcome of places (type variables).
Please modify the thesis for practical purposes.
On Nov 17, 2011 11:58 PM, "Matthew Pocock" <turingatemyhamster [at] gmail [dot] com> wrote:Re: Time to encourage meaningful type parameter names? Discuss
On 2011-11-17 13:42:08 -0600, Tony Morris said:
>
> Write a few of your graph operations. Use the type parameters as if they
> were nodes or edges. Notice how you can't. The proposal is akin to
> projecting meaning onto universally quantified variables of a logical
> proposition. We say, paraphrased:
>
> For all c such that c is an element of the set Cars...
>
> Notice how we call the variable c, not car or any other particular meaning.
Hmm. Why did you choose "c"?
Which one would ease the cognitive burden on the user as they continue
reading the "....." part:
For all c such that c is an element of the set Cars ...
or
For all x such that x is an element of the set Cars ...
If you agree it is the former, then we are just left with "what string
will make it easier for that user to associate my variable with a Car
... c? ca? car? aCar?).
Personally, I would be OK with
Map[K,V]
IF whenever I looked it up (e.g. in IDE, docs, auto-completion etc.) I
saw something that also said "K is the Key type, and V is the Value
type". Then the short K and V work quite nicely.
But not Map[A,B].
Re: Re: Time to encourage meaningful type parameter names? Dis
I chose c because it first pooped into my head. If some other variable name replaced it, then readability is unaffected.
However, the point was to demonstrate that the compulsion to name type variables in ways that they are not, while pretending (even insisting) otherwise is proactively unhelpful and also results in a contradiction and subsequent inconsistency.
On Dec 1, 2011 4:16 AM, "Sophie" <itsme213 [at] hotmail [dot] com> wrote:Re: Time to encourage meaningful type parameter names? Discuss
On 2011-11-30 13:48:43 -0600, Tony Morris said:
>
> I chose c because it first pooped into my head. If some other variable name
> replaced it, then readability is unaffected.
I politely but emphatically disagree. Formal meaning (or lack of it) is
quite different from cognitive burden. Human brains are not computers
or compilers. Programming, especially the bit around trying to grok
unfamiliar code, is about much more than formal symbol table lookup.
Otherwise the entire code obfuscation industry would be spectacularly
redundant :-)
Re: Re: Time to encourage meaningful type parameter names? Dis
On 12/01/2011 12:08 PM, Sophie wrote:
> On 2011-11-30 13:48:43 -0600, Tony Morris said:
>
>>
>> I chose c because it first pooped into my head. If some other
>> variable name
>> replaced it, then readability is unaffected.
>
> I politely but emphatically disagree. Formal meaning (or lack of it)
> is quite different from cognitive burden. Human brains are not
> computers or compilers. Programming, especially the bit around trying
> to grok unfamiliar code, is about much more than formal symbol table
> lookup.
>
> Otherwise the entire code obfuscation industry would be spectacularly
> redundant :-)
>
>
Let me be clear. I find the compulsion for names (alone) less
objectionable than naming things as they are not.
What is happening here, on this thread, on other threads in the past, is
that these three things are happening FAR FAR more than is being
acknowledged:
1) Naming things as they are not, then parading this as some kind of
virtue, and wondering why I respond with, "no thanks, won't be buying
one of those today."
2) Drawing fallacious conclusions from names (then wondering why the
program does not work?).
3) Drawing correct conclusions from names that could have been arrived
at using more rigorous and reliable methods (that is, the fact that the
conclusion holds is a coincidence, not a consequence).
It is these three things that provoke my objection and until it is
acknowledged that this happens far more than is realised, then I am
going to take the hard line: please stop doing this and expecting (at
least) me to consider the merits of any of this. And it's not because of
some utopian ideal; it's because of the practical implications of doing
these three things -- the consequences are distracting at best.
As for naming type parameters, well, it's just got all a bit out of hand
innit? Exactly, humans are not computers or compilers; so why are we
doing these crazy things again? I don't see the point in stating the
obvious -- I was required to know what a human is in order to achieve my
psychology education.
Re: Re: Time to encourage meaningful type parameter names? Dis
My favorite read from my C++ days:http://www.stateslab.org/HowToWriteUnmaintainableCode-Green00.html
On Wed, Nov 30, 2011 at 9:08 PM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
Re: Re: Time to encourage meaningful type parameter names? Dis
On 11/30/2011 09:21 PM, Josh Suereth wrote:
Re: Re: Time to encourage meaningful type parameter names? Dis
On Wed, Nov 30, 2011 at 11:48 AM, Tony Morris <tmorris [at] tmorris [dot] net> wrote:
This must have been very unpleasant.
-- Cédric
Re: Re: Time to encourage meaningful type parameter names? Dis
2011/11/30 Cédric Beust ♔ <cedric [at] beust [dot] com>
:-)
-- Martin
Re: Re: Time to encourage meaningful type parameter names? Dis
On Wed, Nov 30, 2011 at 1:16 PM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
Re: Time to encourage meaningful type parameter names? Discuss
Ken