Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Assuming a function with a signature of

boolean isTransposed(Date date1, Date date2);

Example outcomes:

date1   date2           Outcome
06/02/2000  02/06/2000  true
02/06/2000  06/02/2000  true
02/06/2000        null  false
06/02/2000  02/06/1987  false
11/11/2000  11/11/2000  true
04/04/2001  04/04/2000  false

Granted the behavior can be documented but what are your thoughts on isTransposed('11/11/2000', '11/11/2000') returning true verse false?

share|improve this question
    
Code Review is for reviewing code, but this isn't code. As for your actual question: what is the purpose of the function? i.e. why do you care if two dates are transposed? –  Winston Ewert Mar 8 '12 at 18:08
    
So we can't get a code review on an interface definition? –  Brian Mar 8 '12 at 19:11
    
I don't think asking a specific question about the correct output for a specific function qualifies as a code review. –  Winston Ewert Mar 8 '12 at 22:37
add comment

migrated from codereview.stackexchange.com Mar 8 '12 at 18:07

This question came from our site for peer programmer code reviews.

3 Answers

A boolean is not a sufficient return value to return the comparison. If you are writing generic code, you want something thats tri state. Probable an enum like

enum TransposeResult
{
   notTransposed,
   transposed,
   inputIdentical,
};

TransposeResult calcTransposeState(...)

Then leave it up to the user to figure out what to do when they're already identical.

share|improve this answer
1  
I understand your point in terms of absolute correctness. But I would feel compelled to lose the 'is' in the method name and call it something calcTransposeState(date1, date2). Thoughts on an alternate method name? Any isXYZ method that did not return true or false would be bad design in my opinion - sort of like isEmpty(null) returns true. –  Brian Mar 8 '12 at 19:17
    
@Brian Good point, I agree. Updated. –  Doug T. Mar 8 '12 at 20:43
add comment

You ask what the code should do, I ask back what does the customer want it to do.

Any other answer is a bad answer even it is correct, as at best it's a guess, not requirements based.

Requirements is where significant numbers of software projects fail, and this is a clear example of a requirements problem. It's insignificant and won't derail your project on it's own, just one more one hole in the proverbial Swiss Cheese. Put away you compiler and get out you requirements management tool and resolve the requirements problem.

share|improve this answer
1  
@RossPatterson: So, aside from posting the actual result and the obvious "Granted the behavior can be documented but what are your thoughts on [method] returning [actual result] verse [some other result]?", what exactly is missing from that question? I understand that the expected behavior is specifically what OP is inquiring about. As far as this answer goes, it's totally missing the point IMO, although I wish I could say otherwise. –  netcoder Mar 9 '12 at 2:11
1  
@netcoder: Your answer, which addresses the question of which of several possible interpretations of "transposed", is a great example of why mattnz's answer is important. You directly address the question of requirements in stating two possible intentions, but you had to reverse-engineer them from Brian's stated outputs and function name. –  Ross Patterson Mar 9 '12 at 2:32
2  
The fact this conversation is occurring is why the code should be parked and the actual requirement determined. If you cannot be certain that you are implement what is required, how can you be certain that you are implementing the correct solution. –  mattnz Mar 9 '12 at 3:34
1  
@mattnz: This just seems to me like a generic answer you could post to a lot of questions both on SO and SE, and probably be right 50% of the time, if not more. I can't say it's not true, just that I don't find it particularly useful. –  netcoder Mar 9 '12 at 13:57
1  
@netcoder, it is more useful than your answer because this is a case when we don't know what the user wants and clearly neither does the developer and the developer must go back to the users to find out. Making a choice as a guess as to how you think it should be (and in this case we don't even have a clue as to why he is doing this to give a hint of what would be the best behavior) is a poor programming technique. Requirements questions are the domain of the users not the developers. –  HLGEM Mar 9 '12 at 19:39
show 4 more comments

As far as I'm concerned, if your function is called isTransposed, then checking the values of 11/11 and 11/11 should yield true.

The definition of transpose is:

Cause (two or more things) to change places with each other.[1]

...it doesn't have anything to do with input and output being identical.

Think of it as:

/**
 * Check whether date2 is the same as date1 with the month and day
 * transposed.
 */
boolean isTransposed(Date date1, Date date2);

However, having:

/**
 * Determine whether or not the month and day of date1 have been
 * transposed in date2.
 */
boolean hasBeenTransposed(Date date1, Date date2);

Then it could fail, because it would not have a reasonable way to figure out whether or not they really were successfully transposed.

Update: After receiving numerous comments about how "I can only guess what the expected result would be", let me clear it all up once and for all.

The question is:

When checking for transposed day and month values between two Dates - should comparing 11/11/2000 and 11/11/2000 return true or false?

And the function is:

boolean isTransposed();

Now, this function contains two words: is and transposed. We all know what is refers to: to be. Not may be: to be. You can find the definition for tranposed above.

There is no context whatsoever, and none of these two words are up to interpretation: they each have their own clear definition.

The question is not "Is this function name appropriate?" or "What should this function check for?". If one thinks otherwise, then he is the one guessing.

share|improve this answer
    
But if they're identical, how would you know that they were really transposed? –  FrustratedWithFormsDesigner Mar 8 '12 at 18:34
    
@FrustratedWithFormsDesigner: You don't want to know if they were transposed, you want to know if you transpose date1 as described in the question, will become date2? And the answer in this case is yes. Whether or not you need to change the input to make it output is irrelevant. If the function signature was Date tranpose(Date input);, it would be different, because the function wouldn't have any effect, and failing in this case could be appropriate. –  netcoder Mar 8 '12 at 18:41
    
Ah, the difference between "A and B were transposed (and we know it for certain)" and "A and B might have been transposed (but we don't know for sure)". –  FrustratedWithFormsDesigner Mar 8 '12 at 18:49
1  
@netcoder: I suggest that you have no way of knowing that. Either interpretation is equally valid. The only reasonable solution is to define what the actual requirement is in a way that covers the case we're considering. Resolving that, given just the name isTransposed, is not possible. Once it's been defined, implementing it is trivial. –  Keith Thompson Mar 9 '12 at 2:37
    
@KeithThompson: Semantically, is (as in isTransposed) defines that if you transpose the input and it gives the output (whether they're the same or not is irrelevant in semantics), then it's true. –  netcoder Mar 9 '12 at 2:44
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.