Search

What the Quote?

"I think the Pope resembles a jack-o-lantern. He's just got the kind of face you wanna carve into a pumpkin."

Pete Oberlin

"Weight off shoulder, nail through head."

Dan Huddlestun

"I'm aware of pizza. It's on Gandhi's face."

Laura Tripcony

« swimming in liquid crystal | Main| McCain will still win tomorrow... »

I was wrong: plus is better than ampersand

Category lotusscript concatenation operators
A deeply ingrained habit of mine is using ampersand to concatenate strings in LotusScript (when I'm not bypassing operators entirely, such as using NotesStream or StringBuffer to optimize massive concatenations) - so deeply ingrained that when NTF asked me today why I don't use plus instead, for a moment I couldn't remember my original reason for switching... but after mulling it over briefly I remembered that I hate type coercion with a passion. It bugs me because it tries to be too smart and often fails. Just like extended syntax. For example:

"100" + "200" = "100200"
This may seem obvious: we're concatenating 2 strings, so it takes the value of one and appends the other. But...
100 + "200" = 300
The LotusScript interpreter decides that, since it's starting with a numeric value, you're obviously trying to add another numeric value. Since in this case the string can be converted to a number, it does so and adds the operands instead of concatenating them as strings. So, logically:
"100" + "200" = ...300?
That's right: plus is primarily an addition operator, so if there's any possible way it can remain one, it will. In fact:
100 + "two hundred" = type mismatch - at compile time if it knows the latter is a string, at runtime if it's a variant... reason #372 why variants should be used sparingly and with caution. Speaking of which, I think it was Nathan who once told me, "Variant should have been called Deviant... 'cause it'll accept any object". Yeah, I know, ew. But you gotta admit, that's darned funny.

So how did he convince me plus is better than ampersand despite my pedantic aversion to coercion?
  1. (Author's note: this was actually a third realization, which came after the discussion.) In LotusScript string concatenation, some coercion is unavoidable. Since ampersand's only valid use is as a concatenation operator, any scalar entering the operation is treated as a string whether you wanted it to be or not. So unless everything's a string to begin with, it's coercing anyway, it's just safer. But if we're subscribing to a "flexible input, strict output" philosophy, we're already making sure we know what data type we're dealing with and handling it appropriately, so ampersand doesn't really gain us much... it's only when we're being lazy about this that ampersand saves us from returning unexpected results.
  2. Whenever possible, it's helpful to use conventions that are optional in one language but required in another. In Formula, but - more importantly, over the long haul - also in JavaScript and Java, we don't have a choice: we have to concatenate with plus; might as well do it in LotusScript too so that we're spending more time thinking about how to implement the actual functionality at hand and less time trying to remember what operators to use in the current language... same reason I always surround my conditional expressions in parentheses: I know LotusScript doesn't force me to, but JavaScript and Java do, so why erode a good habit just because LotusScript would allow me to be lazier than that?
  3. In XML, an ampersand must be entity-encoded; a plus can be left alone. This was the "you had me at hello" argument. For the foreseeable future, any convention that allows my code to be handled more easily within an XML context is likely to appeal to me, even if it forces m e to abandon a long-held alternate approach.
Anything you can think of that I'm overlooking? If not, I'm switching to plus effective immediately. That is, as soon as I get back to writing LotusScript... it's been nothin' but Java for me all week.

Comments

Gravatar Image1 - You should throw C in with Javascript/Java -- there's a BIG difference between + and &.

That, and the BASIC on my Commodore 64 didn't use &. Emoticon

Gravatar Image2 - @Rich - Amen... falsy is a pain in the butt, and equality operators are definitely confusing to / overlooked by most (and in Java, the difference between == and .equals() can at times be baffling). I love how JSLint is integrated directly into Aptana, and wish that there were an LSLint (hm... perhaps something to add to Envoy...).

@Erik - very true; I'd left it out of the list because, as a Domino developer, C is likely to continue to be something we "used to use", whereas Java and JavaScript are and will increasingly be crucial tools in my programmatic toolbox as time goes by.

Gravatar Image3 - Honestly I'm not in favor of any of the scripting languages that do type coercion. JavaScript does in several cases and it really just opens up room for novice developers to make mistakes. The languages are trying to be 'nice' and figure out what the developer is trying to do, but honestly it would be better to just force the developers to fix the problems in the first place.

Along the same lines in the list of my JavaScript pet peeves is the laundry list of 'falsy' values, and he fact that != and == do not really do what they should, meaning you should use !== and === which just feels really odd.

Gravatar Image4 - Call me lazy, but I just love the fact that LS ampersand converts items to be concatenated to strings, without me having to convert them using a hard to remember conversion function. This is especially true when concatenating doc fields like:
doc.Street(0) & " " & doc.StreetNr(0)
or for dates:
"Email Sent: " & Now
I don't have to remember whether StreetNr was a number or a text, or convert my date.
So call me lazy.
BTW there are other languages that use & to concatenate.

Gravatar Image5 - For me it's a matter of consistency. I always use & for string concatenation and always use + for arithmetic operations. It just helps me keep straight what I'm doing at any given point. I do extensive validation to ensure I have the appropriate data types, but it's still cleaner to me to use separate operators so I don't have to guess which operation is happening.

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)