1

Java has these strange vocabulary words like "POJO" and "Bean" and it's very weird for me to hear that I don't have the right magic beans to grow some sort of program beanstalk but here we are.

By contrast, I find Haskell's data declarations sublimely simple.

I came up with a mental model for beans and I want to know if it is too simple -- if there is something that it is not capturing or if there is something more restrictive than that. This is to compare it to Haskell's Last monoid combined with its product types, so that a bean like

@Data
@NoArgsConstructor
class ClientContact implements Serializable {
  private Long clientId;
  private String displayName;
  private String emailAddress;
  private String notes;
}

is perhaps meant to just be a monoid like,

data ClientContact = 
  ClientContact { clientId :: Last Integer
                , displayName :: Last Name
                , emailAddress :: Last Email
                , notes :: Last Notes
                } deriving (Eq, Read, Show)

-- why can't we derive Monoid? sigh...
instance Semigroup ClientContact where
  ClientContact a b c d  <> ClientContact a' b' c' d' =
      ClientContact (a <> a') (b <> b') (c <> c') (d <> d')

instance Monoid ClientContact where
  mempty = ClientContact mempty mempty mempty mempty

This "looks like" the sorts of Java code I am seeing, e.g.

-- maybe some of the setters/getters are more common in the codebase
instance HasClientId ClientContact where
  getClientId = clientId
  setClientId cid = mempty {clientId = Last(Just cid)}

-- maybe some have some extra functionality on them?
setDisplayName name = mempty {displayName = trim name}

-- but the point is, ~= return myContact.setClientId(123).setDisplayName("CR Drost")
return $ myContact <> setClientId 123 <> setDisplayName "CR Drost"

-- Or, ~= BeanUtils.copyProperties(postData, dbContact, "clientId", "emailAddress")
let newContact = dbContact <> postData{clientId = mempty, emailAddress = mempty}

Is this what beans are meant to capture? Something about "I am gonna have a parser that needs to initialize an empty ClientContact and then slowly add parameters to it, I want to ensure that there is a sort of basic algebra to these things and I am willing to make everything nullable to ensure that can happen" or so?

Or are beans meant to also be used with, say, arbitrary side-effects and perhaps other stateful or monoidal behaviors, and this description is too simple? Is the contract something other than "after a set, every get must return that value until some other set happens...?" For example is something still a Bean if every setWhatever() has a side-effect of saving the whatever to a database?

CR Drost
  • 9,637
  • 1
  • 25
  • 36
  • 1
    Hmm? JavaBeans are just mutable records. No functional theory, etc.; imperative languages are all about side effects. It's a record that you can modify. – chrylis -cautiouslyoptimistic- Jul 09 '21 at 23:40
  • 3
    "Java" does not have those words. "Bean" occurs nowhere in the Java Language Specification. Some people who write code in Java have chosen to use silly names to refer to what they design. – iggy Jul 09 '21 at 23:46
  • Though, for what it's worth, POJO has an understandable heritage - PO, for "plain old", is somewhat of a cliche in tech circles.. The oldest similar thing I can recall is [POTS](https://en.wikipedia.org/wiki/Plain_old_telephone_service). A somewhat similar construction is [JBOD](https://en.wikipedia.org/wiki/Non-RAID_drive_architectures#JBOD). – iggy Jul 09 '21 at 23:50
  • This is possible to derive, I am in the process of adding [`Generically`](https://gitlab.haskell.org/ghc/ghc/-/merge_requests/5726) to `GHC.Generics`. At that point you add the following clauses: `deriving stock (Eq, Read, Show, Generic)`, `deriving (Semigroup, Monoid) via Generically ClientContact`. – Iceland_jack Jul 10 '21 at 03:47
  • @cr-drost It is possible to use the more familiar `Maybe` instad of `Last` in the declaration of `ClientContact` -- only specifying `Last` in the *via* clause: `deriving .. via (Last Integer, Last Name, Last Email, Last Notes)` – Iceland_jack Jul 10 '21 at 03:57

0 Answers0