Stephan van Hulst wrote:Java doesn't have an equivalent. The idea is that a tuple is a data construct that doesn't convey *what* it is that a method returns.
esam kan wrote:...plus giving the option to programmers is always good (at least the one's who are full of wisdom).
Jesper de Jong wrote:If you really want this, you can easily create your own class Tuple with generics.
But Stephan is right: it is better to define a value class, because that will make it much more clear what each of the values in the class mean. If I look at the declaration of your method, and I see its return type Tuple<string, int, int, bool> then I have no idea what the four components in the Tuple are. With a value class, it's immediately clear what each of the components means.
esam kan wrote:that is a reason why comments exist.
so you want a program to contain god knows how many value objects and how much complexity it add to the maintenance of the program ,just to save a line of comment explaining the return value ?
Mike. J. Thompson wrote:Tuples don't make much sense in java firstly because java is statically typed and secondly because java does not have a rich enough syntax to allow you to unpack the tuple directly into variables.
Static typing in java means that the general idiom for communicating information about returns from methods is through the type name, which hopefully conveys some domain information if the names are good. This means that it is definitely a good idea to use value objects in Java.
The static typing also makes it impossible to have a generic tuple that can have an unlimited number of differently typed contents. I think C++ gets around this by having multiple tuple classes, each with one more templated parameter than the last, up to a 'reasonable' number.
Python both allows you to unpack tuples into variables and to have any number of different types in the tuple. You do require good documentation to know what is being returned, but this fits in with the general idiom for python and in my opinion works very well.
C++ has a rich enough syntax that a technique was found to allow you to unpack tuples directly into variables. it is a little more unwieldy than python, and consequently a little harder to read. It's ok now and then, but probably not something to use all of the time.
If tuples were used extensively in Java I think they would have all of the downsides of tuples with none of the benefits, would make code less readable, and would be non-idiomatic.
esam kan wrote:i would say comments to the rescue here. i write a lot of comments . my code is so clear to anyone reading it. i actually over explain things in my comments , you feel like reading a tutorial .
esam kan wrote:
i think this is one example of people get used to things that have limitations.
Mike. J. Thompson wrote:
esam kan wrote:
i think this is one example of people get used to things that have limitations.
I'm not sure I understand your point, but I can only assume you misunderstood my post. I never said that Tuples are a bad thing (i use them all the time). What I did was list the reasons why I don't think they work very well in Java with its current syntax. Different languages have different strengths and weaknesses and it makes sense to use languages to their strengths and produce the most maintainable code possible.
Junilu Lacar wrote:
esam kan wrote:i would say comments to the rescue here. i write a lot of comments . my code is so clear to anyone reading it. i actually over explain things in my comments , you feel like reading a tutorial .
I would wager that hardly anyone reads your comments, especially the ones that read like a tutorial. Ain't nobody got time for that. Clear, self-documenting code is far better than comments. There are certainly some kinds of comments that are useful but I prefer reading clear code that reveals its intent over reading gratuitously commented code. Show me some "well-commented" code and I could probably show you an uncommented version of it that reads better.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Uncle Bob wrote:One of the more common motivations for writing comments is bad code. We write a module and we know it is confusing and disorganized. We know it’s a mess. So we say to ourselves, “Ooh, I’d better comment that!” No! You’d better clean it!
Junilu Lacar wrote:I don't want to stray too much from the OP's topic so I'll be brief about the comments thing. I reiterate: I'm not against comments. Good comments are very useful. The problem is that the vast majority of comments written are not good. Here's an excerpt from the "Clean Code" book by Robert Martin, aka "Uncle Bob" (emphasis mine):
Uncle Bob wrote:One of the more common motivations for writing comments is bad code. We write a module and we know it is confusing and disorganized. We know it’s a mess. So we say to ourselves, “Ooh, I’d better comment that!” No! You’d better clean it!
Tim Holloway wrote:
I have a long history with C and C++. In fact, I created one of the very first commercial C++ development systems. But I don't use C++ much any more, because as one pundit said: "Java is C++ without the mace and knives". A large part of the "mace and knives" in C++ came from how you could royally screw yourself with type leakage on pointers. Which is basically what tuples are.
Tim Holloway wrote:
Tuples as an abstract mathematical concept are important. And tuples on quick-and-dirty languages are very convenient. But tuples (including arrays of heterogeneous types) are not supported (as pure tuples) or encouraged (as arrays) in Java, because Java is designed for a more rigorous environment where you do all your work at compile time to minimize the "gotchas" that can spring up at run time. As someone just mentioned, a "tuple" is just a lazy sloppy way of defining an anonymous class on the fly. And while Java has gotten more tolerant of such things in the form of lambdas, they're not yet ready to get that casual.
Mike. J. Thompson wrote:
Some of the cleanest code I've produced has been in Python, and it's allowed me to do things that I wouldn't even bother attempting in Java or C++ because it would take too long. ...
I think the problem comes when a programmer moves between languages but tries to use the new language as if it was the old language (especially when the languages have different paradigms at their heart). They end up with non-idiomatic code that feels clunky and doesn't use the strengths of the language, and then people conclude that the new language is no good. I saw a great talk on Youtube by Raymond Hettinger that was pretty much on this topic.
I'm definitely guilty of this myself, especially when it comes to Pythons idiom of extending classes to provide specialization which in Java would result in bad design. But then inheritance in Python works differently to inheritance in Java so what would be bad design in Java does not translate to bad design in Python.
esam kan wrote:i would say comments to the rescue here. i write a lot of comments . my code is so clear to anyone reading it. i actually over explain things in my comments , you feel like reading a tutorial .
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|