News for hackers. Stuff that matters.
Re:Parse, Don’t Validate by radicalbyte on Thursday November 07, 2019 @05:32PM (#21478007)
The Java example you've provided is known as Domain Modeling, it's well described (and honestly greatly expanded on) in Domain Driven Design by Eric Evans.

It's applied heavily in the enterprise world simply because it's so powerful. In general as the domain logic becomes more complex the benefits of doing this increase.

Actually, not encountering this style in code-base which is solving a complex problem is a massive warning sign for me. It usually means that concepts are poorly defined and that the logic is scattered randomly all over the code-base.

Apps Hungarian is just a logical style: name functions, variables, types so that they're meaningful within your domain. The result of which is code which is very easy to understand for someone who understands the domain. This doesn't mean long names - if you're doing anything math intensive then using the short names which conform to the norms of the field is perfect. For a business process it probably isn't :)

Re:Parse, Don’t Validate by gambler on Friday November 08, 2019 @02:20AM (#21480500)
>Actually, not encountering this style in code-base which is solving a complex problem is a massive warning sign for me.

There is a fine line between capturing some common meaning in a reusable type (e.g. Optional<T>) and spending most of your time crafting a straight-jacket of type restrictions that are supposed to thwart some imaginary low-level bug in the future.

Java is actually a great example of how this mentality backfires in real life. The language, from day 1, had a zillion ways to force someone else to do something they didn't want to. Abstract classes. Private methods. Final classes. Etc. It was supposed to ensure good design, but in practice just led to the endless reenactment of the banana-gorilla-jungle problem. (https://pastebin.com/uvr99kBE)

At the same time, the language designers didn't add such basic features as lambdas and streams until 1.8. This lead to creation of ungodly amount of easily preventable bugs, since everyone was writing those awful imperative loops.

Re:Parse, Don’t Validate by monoideism on Friday November 08, 2019 @06:56AM (#21481471)
Banana-gorilla-jungle problem results from OOP. This article (and presumably, the GP) is describing encoding domain-driven logic in a statically-typed functional language, and thus the OOP issues are not a problem.

Functional languages don't carry around their environment with them, so you just get a banana. No gorilla or jungle.

Re:Parse, Don’t Validate by marcosdumay on Friday November 08, 2019 @07:38AM (#21481680)
Oh, it does have some banana-gorilla-jungle problem, it's just not at the same place.

If the types are computationally equivalent, there is a plentora of type classes you must inherit, there is a lot of packing and unpacking to make them work with standard libraries (how do you print an address?), and if the values are not completely separable, there are plenty of conversion issues.

It is an improvement, no doubt about it, but the main benefit of the languages with advanced type systems is that you can refactor code easily to add them only to the places where it's a win, only after your code is complex enough to make it a win.

Re:Parse, Don’t Validate by gambler on Friday November 08, 2019 @12:38PM (#21484632)
>Banana-gorilla-jungle problem results from OOP.

Not really. There are specific design decisions that Java made which enabled this issue to manifest. I've recently took a foray into Smalltalk and it doesn't have this problem, because methods depend on object interfaces rather than class types. AFAIK, OCaml also got this issue right while keeping static type checks - they decoupled classes from types.

>Functional languages don't carry around their environment with them

It is entirely possible to recreate the issue in a functional language, as long as there are sufficiently powerful mechanisms to enforce type safety during compilation. In fact, if you don't understand this and follow the advice in the article too far, that's exactly where you will end up. You will have a jungle monad containing gorilla monad holding hostage the banana data structure the user actually cares about.

Re:Parse, Don’t Validate by monoideism on Monday November 18, 2019 @01:05AM (#21562253)
In the off chance you're reading this 9 days later, I'd like to push back against this response (working on an OCaml project at work, actually).

> AFAIK, OCaml also got this issue right while keeping static type checks - they decoupled classes from types.

If you mean that the types correspond with OCaml classes themselves and not the objects that classes create, yes, that's correct. But it sounds like you might mean the opposite of that. In any case, people rarely use the O part of OCaml much. Most people find they prefer the first-class modules for almost everything that needs greater modularity than what functions provide. I've found that the only thing that OCaml object system is good for is modeling/wrapping existing OOP or prototypal APIs, like certain XML ones, or some web APIs.

>You will have a jungle monad containing gorilla monad holding hostage the banana data structure the user actually cares about.

While it's true that monads are a way to make normally explicit environments implicit, monads aren't really what the book I'm talking about is about, nor this article.

Yes, monad-heavy code could (with effort) end up with an application monad displaying problems similar to the banana-gorilla-jungle problem. I've frankly never seen monadic code like that, but perhaps you have.

Instead, what this article is advocating for is simply modeling your data correctly. Really, this is a universal programming theme, that helps a lot regardless of programming language. But the structures available in statically-typed functional languages are more powerful and able to model domains with greater granularity. If you're familiar with the saying "make invalid states unrepresentable", then you understand what I mean.

Re:Parse, Don’t Validate by radicalbyte on Friday November 08, 2019 @04:51AM (#21481008)
Java has just had a terrible steward in Oracle. Plus the community seems to like boilerplate code (see: Angular for more of that if you're into javascript).

I switched to C# as my main language specifically because they were gaining awesome features like generics, lambda, LINQ. Since then it just got better and better.

In the C# world you seem to encounter systems where the logic is either in:

* UI event handlers * Methods on helper classes which consume a model generated from a database. * Domain modeled with classes with the logic split between the classes themselves and services which consume classes.

In the first two cases the code tends to have a massive sprawling dependency tree which makes it harder to maintain. In the latter case you've usually got a better chance of having properly isolated code. Even if the isolation isn't super you at least have the logic well organised.

Re:Parse, Don’t Validate by LoSboccacc on Friday November 08, 2019 @02:50AM (#21480599)
it's extremely useful especially at interface level but also in day to day coding, pity the people whose method signature is (string, string, string) because no ide can save them from the churn.
Re:Parse, Don’t Validate by radicalbyte on Thursday November 07, 2019 @05:36PM (#21478036)
It's particularly powerful if you can great an algebra of your domain. You can define invariants which are then enforced by the type system.

I use this a lot when writing C# code and the whole experience is getting better with every new release. Microsoft keep adding awesome stuff like ADTs which make working with these types a dream.

Re:Parse, Don’t Validate by james_s_tayler on Thursday November 07, 2019 @11:45PM (#21480027)
Do you have any resources you can recommend which describe how to create an algebra of a domain?
Re:Parse, Don’t Validate by jen20 on Friday November 08, 2019 @01:32AM (#21480371)
I’d recommend Scott Wlaschin’s excellent book “Domain Modelling made Functional”. The examples are in F# but the concepts apply much more broadly.
Re:Parse, Don’t Validate by kuroguro on Friday November 08, 2019 @07:17AM (#21481563)
Re:Parse, Don’t Validate by q-base on Friday November 08, 2019 @11:49AM (#21484015)
Thanks a lot for that video. Watched the whole thing on the spot. He is actually quite a good presenter and really gets the points across.
Re:Parse, Don’t Validate by smush on Friday November 08, 2019 @11:29AM (#21483765)
Came here to post this. I second this book heartily. If you design your types to be DD with raw, valid, invalid data then by the time you actually get to the database or API, you are sure that your code has passed through however many compile-time enforced validity checks (if you use F# or similar static strong type PL) before it got to this point.

That way, you don't need nearly as much defensive coding re: null checks etc. because the data has gone from 'some client typed some value in an input box, hope its valid' to 'this is an Address line that has been confirmed to be valid structure to pass to downstream systems'.

Pit of success is so much better than peak of success, and much easier to get in a static typed functional language vs. an OO language in my experience.

Re:Parse, Don’t Validate by goostavos on Friday November 08, 2019 @01:48AM (#21480416)
+1 for that book. It totally changed how I thought about types and their role in programming.
Re:Parse, Don’t Validate by monoideism on Friday November 08, 2019 @07:02AM (#21481499)
Agreed. This is a great book. It's changed the way I use statically-typed functional languages.

This article is very much in the same vein.

Re:Parse, Don’t Validate by tigershark on Friday November 08, 2019 @02:04AM (#21480454)
The best thing is that he writes F# in a way that is perfectly understandable by the domain experts so he can use the code as the ubiquitous language. In this way an intermediate representation to communicate with the users is not needed and there is nothing that needs to be kept in sync with the code.
Re:Parse, Don’t Validate by james_s_tayler on Friday November 08, 2019 @03:27AM (#21480697)
Thanks!
Re:Parse, Don’t Validate by smt88 on Friday November 08, 2019 @01:55AM (#21480429)
> Actually, not encountering this style in code-base which is solving a complex problem is a massive warning sign for me.

This is very narrow-minded. DDD as a broad concept is good and something I practice.

DDD in enterprise is a disastrous nightmare. When people on your team are wasting time Googling lingo to try to figure out what kind of object they need, where to put a file, or where a method should go, it's a huge red flag.

I was on a team that seemingly spent all its time trying to figure out what the hell DDD was prescribing, let alone trying to figure out how to do it.

Re:Parse, Don’t Validate by Doxin on Friday November 08, 2019 @04:03AM (#21480841)
> When people on your team are wasting time Googling lingo to try to figure out what kind of object they need, where to put a file, or where a method should go, it's a huge red flag.

I agree, but for a different reason. If you've got a team of programmers on a project without any understanding of what the client is doing you're bound to implement stuff ass-backwards from the clients perspective.

DDD is fine in enterprise, but the domain does need to be communicated to all involved parties. You can't just dump a codebase on someone and expect them to get to work, that only barely works for non-DDD codebases let alone DDD codebases.

Re:Parse, Don’t Validate by monoideism on Friday November 08, 2019 @07:06AM (#21481520)
The kind of DDD this article is describing is totally different from Java-world enterprise DDD. It's DDD done right, with no 50-member OOP classes. Just a single function that can only take a type that describes your domain in a way that prevents whole classes of errors.
Re:Parse, Don’t Validate by smt88 on Friday November 08, 2019 @11:07AM (#21483517)
I think we're on the same page.

When inheriting a purist DDD project written in C#, I came across several... interesting... charts, including this one:

https://jj09.net/wp-content/uploads/2018/12/ddd-diagram-exam...

A lot of members of my team couldn't figure out where things were "supposed" to go (or what most of the terms mean) after weeks of working on the code, so they just started slapping methods anywhere and worrying about it later.

Re:Parse, Don’t Validate by MrGilbert on Friday November 08, 2019 @02:34AM (#21480543)
> [...] trying to figure out what the hell DDD was prescribing, let alone trying to figure out how to prescribe it.

Well... If the carpenter needs to Google how to use a hammer and nails while at the construction side, something went terribly wrong, didn't it?

Re:Parse, Don’t Validate by btschaegg on Friday November 08, 2019 @03:11AM (#21480657)
I would actually agree with you there, but I suspect that wasn't your point.

A hammer is such a simple and obvious concept that it should not need to be googled. If anybody needs to do that anyway, you should not put the blame on the carpenter, but on the guy who "designed" the hammer.

On a related note, I still very much am convinced that Don Norman's Design of Everyday Things is a great read for anyone who designs APIs; types in this argument function both as affordances and force functions, which are the exact reason why a hammer is such an obvious tool.

Re:Parse, Don’t Validate by smt88 on Friday November 08, 2019 @03:28AM (#21480709)
You seem to be implying that a dev who doesn't understand enterprise DDD is unqualified.

That's not like a carpenter who can't use a hammer. It's like a carpenter who asks for a sketch of a project and instead receives a phone-book sized list of vague instructions that no one can agree on.

Re:Parse, Don’t Validate by MrGilbert on Friday November 08, 2019 @04:33AM (#21480946)
> You seem to be implying that a dev who doesn't understand enterprise DDD is unqualified.

I imply that a dev who doesn't understand DDD in a enterprise environment, is unqualified for working with DDD in an enterprise environment.

As much as a carpenter is unqualified to be a carpenter if he doesn't know how to handle a hammer and nails.

And I appreciate all the downvotes coming in.

Re:Parse, Don’t Validate by hmottestad on Friday November 08, 2019 @05:12AM (#21481068)
I don't think people agree with your analogy. To me your analogy is too simple. I have 6 years of java development experience and a masters in informatics. I have yet to work with enterprise DDD. There were no classes at university that taught it.

In most of my projects I have had to learn a lot of new stuff, it's a blessing if someone on the team knows how to use it already. However I have found that on most projects we've struggled to hire people with substantial experience with the tech stack and methodology that the customer had chosen.

We still wouldn't find an entire team that had experience with everything we needed even if we paid them ten fold the amount of every other company.

On the other hand I reckon every carpenter learns to use a hammer and nails in school.

Re:Parse, Don’t Validate by MrGilbert on Friday November 08, 2019 @07:35AM (#21481655)
> There were no classes at university that taught it.

So? You don't leave university and expect to be "done", right? You learn, learn, and learn - your whole software dev life. Even better, your whole life.

And no - nobody teaches you how to professionally handle a hammer in school. Now you are simplifying here. In school, they show you a hammer and nails. That's it. As much as they show you a compiler, a programming language and a basic architecture in university.

There are a ton of different hammers and nails out there, each with their own special attributes. You have an engineer's hammer, a tiling hammer, a rubber hammer, a roofing hammer, a splitting maul, a sledgehammer and so on... And I won't start talking about nails.

It's up to you to gain knowledge when to use what, and how to use it. That doesn't mean it's useless per se. Or overly complicated. It might just no "be your thing", which is fine.

Re:Parse, Don’t Validate by hmottestad on Friday November 08, 2019 @10:47AM (#21483274)
This was exactly my point. Not all carpenters would know how to use some specialised hammer and specialised nail. Just like most developers wouldn’t know how to use a specialised language and framework. So developers that don’t know how to develop with DDD would just learn as part of joining a project and they would still be qualified to be on the project. Just like how a junior carpenter might not know everything needed to erect a hose by themselves, but as part of a team of carpenters they would learn the things they didn’t know from school including those specialised hammers and nails for that specific house because the owner wanted something unique or what not.

FYI a carpenter in Norway needs 4 years of education to become a carpenter. 2 years in school and 2 years as an apprentice. If they don’t know how to use a bog standard hammer by that time they are not passing the final examination.

Re:Parse, Don’t Validate by MrGilbert on Friday November 08, 2019 @05:52PM (#21487766)
I see - maybe I got your point wrong at first. Sorry for the confusion.

Here in Germany, it's basically the same: A carpenter goes to school and work in a dual system for 3 years, and can then put a master on top of it after several years of working experience. This takes another 2 - 4 years and a ton of money.

Re:Parse, Don’t Validate by ewngzen on Friday November 08, 2019 @07:17AM (#21481561)
My take is that DDD is actually a convinient intuitive way to work with, if it is not, probably something needs to be discussed and refactor ed
Re:Parse, Don’t Validate by cjfd on Friday November 08, 2019 @04:52AM (#21481016)
This can go two ways. One can end up with lots of different types that do nothing but wrap a string. On the other hand I have also seen a float being used to represent a price. The correct way is somewhere in between those two extremes. If you have some logic specific to a value, by all means, make it a type but if there isn't it just ends up with lots of boilerplate.
Re:Parse, Don’t Validate by rzzzt on Friday November 08, 2019 @12:56PM (#21484871)
I've heard "micro-types" being mentioned in this context: https://markhneedham.com/blog/2009/03/10/oo-micro-types/