Hi. Just wondering if there's a shortcut to this code?

if (calculation(codeword, s[0]) == calculation(codeword, s[1])
	|| calculation(codeword, s[0]) == calculation(codeword, s[2])
	|| calculation(codeword, s[0]) == calculation(codeword, s[3])
	|| calculation(codeword, s[1]) == calculation(codeword, s[2])
	|| calculation(codeword, s[1]) == calculation(codeword, s[3])
	|| calculation(codeword, s[2]) == calculation(codeword, s[3]))

Thanks in advance!

In general, using a for loop would shorten code like that. But since you don't have many lines of code anyway, using a for loop would not shorten it. Although it might be tricky to write in this case, so as an exercise, I'd recommend that you try it.

I don't think you should try to shorten this code. For the reason that a for loop might not be able to "exhibit" what exactly you are trying to achieve in the code which would make the code unreadable or atleast difficult to understand.
One of the many virtues of a good programmer are keeping it simple, writing code which is readable and easily understandable, in a way that the logic behind the implementation should flow freely in the reader's mind.
And it isn't a very long expression anyways, you do encounter such situations once a while. Yet if you are obssesed with making it short try renaming the caculation method to calc that would save you 84 keystrokes. ;)

I think that BestJewSinceJC is right, a for loop would have been useful if it was longer.

Otherwise, put all your code on one line. This will make it look shorter

Set<Object> check = new HashSet<Object>();
for (Whatever sNum : s) {
  if (!check.add(calculation(codeword, sNum))) {
    // whatever
  }
}

There is no reason to call this "calculation" method multiple times for the same "sNum" (that is extremely ineffecient), and the add method of Set will return false, if the item being added already exists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.