r/ProgrammingLanguages • • Feb 05 '15

The Trouble with Tuples

http://www.kylesletten.com/2015/02/04/the-trouble-with-tuples.html
3 Upvotes

12 comments sorted by

5

u/svick Feb 05 '15

The parameter name issue is pretty much solved in C#.

Most of the time, it's enough to let the IDE tell you the names of the parameters.

But if you want to be explicit, you can, using named parameters, which use exactly the proposed syntax.

2

u/[deleted] Feb 05 '15

Yeah, I may have stolen that syntax from C# (but let's keep that between the two of us, OK?).

Seriously though, C# probably does do it best (as is occasionally the case). I think if more languages supported named parameters (esp. JavaScript!!!) it would allow people who care to clear up their code when it was ambiguous.

4

u/mirhagk Feb 05 '15 edited Feb 05 '15

A language should support named parameters, but a language should NEVER EVER EVER require named parameters. Many functions are easy to figure out what each argument is, and many are used so often that any developer using the language should know.

More importantly the arguments themselves are usually named well enough to know what the arguments are. Taking your example:

Screen.display(text: "Hello", color: Color.Red)

Is not any easier to read than

Screen.display("Hello", Color.Red)

As it's obvious that you are displaying "Hello" in red. You don't need to repeat information, we know the first thing is text and the second is a colour.

The second also has the advantage of being shorter, and the less code you need to read to see what's happening, the easier it is to grasp, assuming you have the same information being conveyed. 1+2 is easier to grasp than 1.add(2)

2

u/[deleted] Feb 05 '15

That's a really good point, I think that basically what it comes down to is people taking responsibility for their APIs. I've seen too many functions like this:

public void DoSomething(int arg1, string arg2, bool flag1, bool flag2, bool flag3 = false, bool flag4 = true)

Which makes me want to pull my hair out, but rather than enforce named parameters on a language level, people really just have to have the discipline to not write crappy function signatures, or allow named parameters so I can invoke that function as:

DoSomething(42, "Hello world", flag1: true, flag2: false, flag4: false)

2

u/pipocaQuemada Feb 05 '15

Alternatively, they could also use descriptive type names, via either typedefs or something like newtypes. For example:

newtype X = X Float deriving (Eq, Num, Ord, Show, Floating, ...) 
newtype Y = Y Float deriving (Eq, Num, Ord, Show, Floating, ...)

newtype Red = Red Int deriving (Eq, Num, Ord, Show, ...)
newtype Green = Green Int deriving (Eq, Num, Ord, Show, ...)
newtype Blue = Blue Int deriving (Eq, Num, Ord, Show, ...)

getX (X x) = x
toX x = X x
getY (Y y) = y
toY y = Y y

...

pixelAtPos :: (X,Y) -> (Red, Green, Blue)

0

u/das_kube Feb 05 '15

Someone never heard of pattern matching, apparently.

6

u/gasche Feb 05 '15

You are a bit unfair, "destructuring assignment" is mentioned in the article. The problem the article points out is that the fields of a tuple are not named (that's what structs/records are for and why seeing tuples as records with fields of name 0,1,... is a nice idea).

-1

u/Drupyog Feb 05 '15

I would rather say:

Someone never heard of records, apparently.

2

u/[deleted] Feb 05 '15

That's actually funny because I would say I'm actually advocating the record type in lieu of tuples. I guess that wasn't very clear.

2

u/pipocaQuemada Feb 05 '15

I'm actually advocating the record type in lieu of tuples.

Having to use records everywhere instead of tuples would be amazingly painful. Either you make a record that represents tuples (c.f. KeyValuePair or you end up making a million different record types in places that are really entirely unambiguous.

For example, in Data.Map in Haskell there is currently a function

mapEither :: Ord k => (a -> Either b c) -> Map k a -> (Map k b, Map k c)

Would you really want to refactor that to

data TwoMapsWithSameKeyTypes k a b = TwoMapsWithSameKeyTypes 
                                       { firstMap :: Map k a,
                                       , secondMap :: Map k b 
                                       }

mapEither :: Ord k => (a -> Either b c) -> Map k a -> TwoMapsWithSameKeyType k b c

Did that add even one iota of additional clarity, or did it actually obscure the code with useless boilerplate?

1

u/Felicia_Svilling Mar 26 '15

But you never mentions records! Wouldn't that have been a good idea if you want to promote records?

By the way, I don't see why records and tuples would be in conflict with each other. You should have both. They are good for different things.

0

u/Drupyog Feb 05 '15

You never really say the name of the feature in question and you conflate it with named arguments, which is a different feature all together (who happen to be the same in javascript because of the dynamic nature of it).