views:

176

answers:

2

Writing a embeddable cross-browser plugin that is scriptable via JavaScript i am unsure how to handle read-only and write-only properties the best way.

Is it more common or intuitive to

  • discard values silently on write for read-only and
  • return dummy value for write-only

or to

  • indicate failure to the browser, most likely resulting in a scripting error

Suggestion? Or are there any good examples from widespread plugins like flash?

update:
I am not interested in wether write-only properties are useful or not - i don't like the idea but i have to support that for historic reasons.

+2  A: 

Existing practice follows Postel's Law: "Be conservative in what you do; be liberal in what you accept from others."

In this case, it means your plugin's documentation should show correct usage, but it should tolerate poor usage. The wisdom of this approach should be obvious from studying the success of HTML vs. every other hypertext system that came before or has been invented since. Most of the failures were very bondage-and-discipline style designs, where the smallest error caused the document to be unreadable.

Just as people regularly write bad HTML, some of your users are likely to pass you bad input. Say you have a Boolean property, documented to take "true" or "false". What if you get 1 or 0? Should it crash, or just cope? Postel's Law says, cope. What if you get -1? Again, cope, such as by choosing to use C's rule that nonzero is "true". Getting to your specific question, if the Boolean property is documented as read-only, and someone gives it a value, your plugin should just eat it.

All this special case handling means more coding for you, but makes your plugin easier to use, and thus more likely to be popular.

The other half of Postel's Law means that read-only properties should only have documented values. Again, take the Boolean property case: if your docs say the value is "true" or "false", never return "1" instead, even though JavaScript understands that to be a truthy value. Follow your own spec, even as you write code that lets others violate it with impunity.

By the way, I don't like the idea of write-only properties. You should make these functions instead. That is:

pluginInstance.setWriteOnlyProperty(5);

not:

pluginInstance.writeOnlyProperty = 5;

Read-only variables are well-understood things. Write-only variables are rare to see. The only time I've ever seen one is in a little 8-bit microcontroller, and the reason for it was clear, due to the hardware design. I don't see room for such good excuses in software.

Warren Young
The problem with this approach is that if the bad usage works, yo have implicitly committed to always supporting it.
erikkallen
See also http://www.joelonsoftware.com/articles/Unicode.html, where Joel Spolsky talks about how Postel's Law "is quite frankly not a good engineering principle."
Kev
That's life in a world-wide distributed system. You cannot force good behavior, and you can't drag all clients forward into current practice. If you aren't willing to follow Postel's Law, you have no business trying to make something that will be used worldwide, over the course of a decade or more, because people will end up frustrated by any system that either keeps changing its protocol or breaks when you throw bad data at it.
Warren Young
Spolsky may be right when it comes to relatively small systems, where the technology provider has some reasonable hope of influencing all of its users. It totally breaks down in any world-scale system, like HTML or browser plugins from vendors with aspirations of popularity. Everyone who plays in that sandbox eventually discovers Postel's Law.
Warren Young
Supporting all possible misuses is not really useful - if i expect a boolean property and i try to handle objects or floats implicitly as well its really getting weird.
Georg Fritzsche
And: I don't like write only properties as well, but breaking an existing specification which i inherited is worse.
Georg Fritzsche
Re: Handling all possible boolean values, you don't have to literally special-case all possibilities. You just have to make your parser tolerant enough to at least yield a reasonable default value when you run out of known cases to try. Don't yell at the user, don't crash, just cope. Re: write-only properties, I didn't know you were implementing an existing spec. Since you are, you have to follow Postel's Law here, too: don't change behavior out from under existing users.
Warren Young
Ah, I didn't realize this was a requirement either.
Kev
In the spec there is no defined behaviour for writing read-only and reading write-only properties - thus the question.
Georg Fritzsche
The read-only cases I covered above in my answer. For the write-only cases, it depends on whether this spec has been released to others, and coded against. If so, I would add a parallel set of setter functions, document the property form as unsupported, but still accept inputs via these interfaces. Any extensions to these interfaces should be made only to the setter functions. If no one has yet written code against the broken spec, you can safely change it. If you're told you can't change it, point out that no spec survives the implementation. :)
Warren Young
While i'd prefer Kevs approach, i think i have to agree with you in the context of the question.
Georg Fritzsche
+1  A: 

I wouldn't discard values silently, even if you make it very clear in the docs. A statement not returning any kind of error makes it seem like it should be having an effect. It's easier to debug if you get a clear error message ("Property x is read-only").

I agree with Warren Young's answer, though, as far as write-only functions go. Write-only properties are unintuitive.

Kev
2 answers, 2 opinions. Nice. I don't like write only properties as well, but breaking an existing specification which i inherited is worse.
Georg Fritzsche
Actually, I like the "fail early" philosophy, too. Eating errors does make debugging harder. However, it still violates the robustness principle, which is important when your users are outside your influence. You can split the difference by offering a debug mode, where errors are flagged, and a production mode, where errors are silently coped with. The Flash plugin does this, for example.
Warren Young
By the way, it's instructive to install the Flash debug plugin, then go roam the Internet for a few days. Observe how many sites are "broken" according to its stricter interpretation of the plugin's "protocol", yet which actually do function quite well enough that no one not running the debug plugin notices. Adobe knows Postel's Law.
Warren Young