posted May 18, 2008 11:27 AM by Misko Hevery
[
updated May 18, 2008 4:57 PM
]
| So I got blogged again. :-) http://blog.thinkrelevance.com/2008/5/18/java-is-naturally-untestable But I can't say I agree with the "Team Relevance" guys.
I don’t see how your choice of language effects testability. Dynamic languages allow you to reach in and change the definition of any method at runtime. Dynamic languages fans suggest that as a result, testability practices such as Dependency Injection are not relevant and that dynamic language is inherently more testable. However code is global state and you reaching in and changing the global state produces all the same problems as global state in Java except worse since you are not only changing global state of data but also global state of code. So I simply don’t buy this argument. To me changing the definition of method at runtime falls into the same level of testability as subclassing in tests and overriding a method. TDD fans frown on this practice, for good reason, but it is no different then changing method definition at runtime. Dynamic languages are more terse which is a great plus, but I don’t see how you can build a large scale many developer codebase out of dynamic languages. Static typing is wordy but it is a level of information which is very useful in large many team projects. So everything needs to be weight in proportion. |
posted May 8, 2008 9:58 PM by Misko Hevery
| Today I have discovered that my work has been blogged by someone else. Hey, it is the first time someone has blogged about me and so I have a right to get excited. How nice of Alex Miller and his post. From there I discovered P2 Panoptikum post about Google Singleton Detector. Man the world is a very connected / small place. Looking forward to see what others have to say. :-)
Since it was my birthday yesterday we went to celebrate out to our favorite restaurant. Mici Sushi. The food as always was amazing and we came back stuffed to the limit. However somehow today the staff got a wind of the fact that it was my birthday so we got a desert for free. While they were at it they also brought out some Unfiltered Sake :-). So we used our $10 off coupon which we got earlier and paid our bill. Wouldn't you know it, but the bill came back with a new $10 coupon. Sweet. We headed back to our car and the Valet guy left another $10 coupon on the seat of our car. I need to go there more often! |
posted May 6, 2008 10:23 PM by Misko Hevery
When naming flags, or any variables for that matter, use positive concepts so that the user has easier time reading the code.
Let me demonstrate how negative questions are difficult to understand. I have a GPS which routinely asks me this questions.
Your destination route goes through a toll road. Do you not want to avoid the tolls: [ YES ] [ NO ]
If I see this while standing I want to throw the GPS out the window, and when I am driving, I just barely mange not to have an accident. I really have to think hard as to what the YES will do. The problem here is that YES represents positive while the question has a double negative. "Avoid" is already a negative concept, and adding "not" only makes it worse.
But bad UI is not the only problem, sometimes bad colloquialism is at fault.
"Do you mind if I eat the last piece of chocolate?" - "NO!" My initial reaction is that I am not supposed to eat it. "No!", wait, it was negative question so no means yes, ohh, great I can eat it.
- "YES!" Great I can eat it, wait it was negative question, so yes means no.
- "No, I want it" OK now, I am confused, no means yes, which means I can eat it, but "I want it" implies that she wants to eat it. This answer contradicts itself!
- "Yes, you can have it." Great another contradiction, yes, she minds, and yes, I can have it.
But this blog is about coding, so lets get back to the world where ambiguity does not exist...
Recently I came across this piece of code (I wish I was making this up) which took the negative question to a new level of confusing.
if ( ! information_hiding_disabled) { ... }
I don't know about you, but I personally have to think really hard until I can figure out under which conditions this executes. Even knowing what the the value of the flag is, makes it hard to think about what it represents. The issue here is that "information hiding" refers to a negative concept. Adding the word "disabled" to it adds a negation to the concept which I guess makes positive again. Often times, when you actually want to use it in an "if" statement you want the opposite behavior and hence you may need to add "!/NOT" So now you essentially have this:
if ( ! ! ! show_information) { ... }
Lets look at a loop as a good way to name a flag:
boolean done = false; while(!done) { if (...) done = true; }
Now lets look at the opposite and notice that it is harder to read:
boolean notDone = true; while(notDone) { if (...) notDone = false; }
In the second loop the "while(notDone)" is easily readable, but the issue are the assignments, especially the exit assignment "notDone = false" Here again, double negation is going on and it takes my brain just a little longer to figure out what "false" means in this case. Furthermore there may be an "if" statement which would make it worse.
if ( ! notDone)
So in conclusion don't ask negative questions! Variables/flags which are named with positive concepts are much easier to read than variables which represent negative concepts. Please think about this when you write your code. Otherwise, I get a headache. |
posted May 6, 2008 10:03 PM by Misko Hevery
[
updated May 6, 2008 10:24 PM
]
I have given a JavaOne presentation on Google Singleton Detector, Testability Explorer and TestabilityExplorer.org. The presentation was a lot of fun and was very well received. The best part was asking people what they thought about the Singletons; wether they were good or bad. Surprisingly most people seemed to agree that they were bad. But it is always the hold out who make a stink how there is nothing wrong with a global state.
After the talk a lot of people stop by and asked a lot of questions about what the further plans are for TestabilityExplorer. I love to see that people things there is potential in it. Now I am curios to see just how many email I will get asking about it.
http://testabilityexplorer.org/ |
|