Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you deal with strings that have structure?
    primarykey
    data
    text
    <p>Suppose I have an object representing a person, with getter and setter methods for the person's email address. The setter method definition might look something like this:</p> <pre><code>setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } </code></pre> <p>Calling <code>person.setEmailAddress(0)</code>, then, would generate a type error, but calling <code>person.setEmailAddress("asdf")</code> would not - even though "asdf" is in no way a valid email address.</p> <p>In my experience, so-called strings are almost <em>never</em> arbitrary sequences of characters, with no restriction on length or format. URIs come to mind - as do street addresses, as do phone numbers, as do first names ... you get the idea. Yet these data types are most often stored as "just strings".</p> <p>Returning to my person object, suppose I modify <code>setEmailAddress()</code> like so</p> <pre><code>setEmailAddress(EmailAddress emailAddress) // ... </code></pre> <p>where <code>EmailAddress</code> is a class ... whose constructor takes a string representation of an email address. Have I gained anything?</p> <p>OK, so an email address is kind of a bad example. What about a URI class that takes a string representation of a URI as a constructor parameter, and provides methods for managing that URI - setting the path, fetching a query parameter, etc. The validity of the source string becomes important.</p> <p>So I ask all of you, how do you deal with strings that have structure? And how do you make your structural expectations clear in your interfaces?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload