views:

316

answers:

8

As far as I know I've never encountered a SHOULD construct in a computer language, but then again I don't know that many languages, compared to the hundreds out there.

Anyways SHOULD and other modal verbs are very common in natural languages, and their meanings are quite clear when writing documentation and legal-binding contracts, so they aren't really gray terms, and theoretically could be expressed in programming terms (I guess).

For example an ASSERT, upholds in a sense a MUST construct.

Are there any actual examples of this sort of thing? Any research about it? I'm guessing some Rule-Based systems, and perhaps fuzzy logic algorithms work like this.

+1  A: 

Well Should might be found in a prolog type language, as a softer inference? Ie the result logically should be x but might not be. You can say the result is probably x but not unequivocally?

Spence
+5  A: 

I think of try as "should" and catch and finally as "in case it doesn't"

Simon
interesting, I was also considering the same thing, but was wondering if I was just delusional :)
Robert Gould
+1  A: 

@Simon Perhaps a Try/finally is closest to should. anything in the Try should run, but not always. A webservice should open the socket but if it doesn't, we don't care.

Spence
Maybe, yes. However I think it is just as valid to say try open file and read line catch io error print message finally close file. This is like saying the file should exist and contain something but there are several things that can go wrong so deal with them separately. Both are valid.
Simon
+2  A: 

This sort of modality is used in RSpec - dsl for building tests in behavior-driven style.

Maxim Ananyev
Thanks hadn't heard of this, its interesting and the kind of thing I'm looking for.
Robert Gould
+1  A: 

What is the behavior you expect from the program if the result is not as it SHOULD? In the ASSERT case, it is an exception (AssertException or similar). SHOULD the program throw an exception or just ignore the result? To me it seems that there is nothing in between. Either the result is accepted or not.

Otherwise you SHOULD specify what behaviour you expect. :-)

Back to the assertion: If the assertion fails, an exception is thrown. It is up to you what you do with that exception. In java/C#, e.g., you can catch it and then do anything you want, so you define whether the assert has a MUST or a SHOULD semantic.

chiccodoro
SHOULDN'T the purpose of an assert be to enforce MUST? Of course the actual underlying system of exceptions can handle either. But the idea of assertions is to imply invariants (==MUST?)
Robert Gould
Thinking more about your point a bit more an IF/ELSE is perhaps the same as an SHOULD/OR...
Robert Gould
Assertions: Hm... you might be right. The native assert construct in java is certainly aimed in what you say. I also mixed up the native assert with the assert-functions in JUnit (even though these may also be designated for MUST constructs).
chiccodoro
Still I am interested in what behaviour you expect in a SHOULD construct?
chiccodoro
+4  A: 

The exact meaning of should in natural language is also not clear cut. When you say "the wheel should fit in the row" - what does that mean exactly? It may mean the same as must, but then there is no point in a construct for it. Else, at what confidence do you need to be for this to be satisfied? What is the action in case the wheel does not fit?

In the senses you referred to, there are some equivalents, though I do not know of language that use the word should for them:

Testing/assertion

ASSERT is often a language directive, macro, or testing library function. In the sense that ASSERT corresponds to must, some languages and testing frameworks define macros for "warning assertions" which will spit a warning message if the check fails but not bail out or fail the test - that would correspond to should.

Exception handling

In some terms, you can consider exception thrown as analogues - if an exception is caught, the program can handle the case where something is not as it should be. But sometimes the exception describes the failure of something to be as it must be for the program to work, in which case the exception will not be caught or the handler will make the program fail gracefully. This is however not always the case - sometimes code is executed to test something that may be or perhaps is even unlikely to be, and an exception is caught expecting that it will usually be thrown.

Constraint logic

One common meaning of must and should in various formal natural language documents is in terms of constraints - must specifying a constraint that you always have to satisfy, and if you cannot then your state is incompatible, while should means that you will always satisfy the constraint if it is possible given the state and the constraints implied by must, but if it is not possible that is still valid. In informal constraint logic, this occurs when there are "external constraints" in the context - so verifying that the "solution" is satisfactory with respect to the "should constraints" may be possible only with knowledge of the context, and given the context you may also be able to satisfy different subsets of the "should constraints" but not at the same time. For that reason, some constraint logic specification languages (whether you call them "programming languages" depends on your definition) have the concepts of ordering of constraints - the first level of constraints corresponds to must, the next level corresponds to should, and a constraint has to be satisfied if possible given all constraints external to it (in previous levels), even if that conflicts with some constraints in the next levels which will then not be satisfied.

Tom Alsberg
Very nice answer, do you have any quotes or its purely yours?
Robert Gould
No ideas here are originally mine, other than the interpretation and poor explanation... Obviously the constraint logic part is based on bits I learned about the subject at various times - I did not invent it. No direct quotes - but if you're interested the web and books are full with resources.
Tom Alsberg
+1: Constraint logic is a good one. I was thinking perhaps NULL in SQL to mean 'should have a value but it is currently missing', leading perhaps to COALESCE(), which is similar to TRY CATCH FINALLY. But NULL is contraversial relationally :)
onedaywhen
A: 

Well, Java2K has similar concepts. It SHOULD be doing what it's told...

SCNR.

christian studer
Won't downvote just because its soo stupid :)
Robert Gould
+2  A: 

Modal verbs like "should", "may", "might" may cause confusion, so RFC 2119 gives an definition to point all noses in the same direction:

SHOULD   This word, or the adjective "RECOMMENDED", mean that there
may exist valid reasons in particular circumstances to ignore a
particular item, but the full implications must be understood and
carefully weighed before choosing a different course.

From this definition it should (no pun intended) be clear that it's meant to be used in specifications, rather than program code, where you want things deterministic. At least I do. I can imagine it's usable in AI, though.

stevenvh
thanks for the official stance on the word, I was wondering if such a thing existed
Robert Gould