Modelling Time - Eric Evans - DDD Europe 2018

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Clojure part starts at @15:55

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/k0t0n0 πŸ“…οΈŽ︎ Sep 30 2019 πŸ—«︎ replies

Does he actually use Clojure? He seems like a staunch OO guy.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/editor_of_the_beast πŸ“…οΈŽ︎ Oct 01 2019 πŸ—«︎ replies
Captions
well that's quite an introduction so I hope to live up to that as he said I like to I like to practice but when I think about what my favorite parts of software are you know it's it's not really the strategic design stuff I must admit that that's something well I mean I enjoy that yeah but I mostly gave that thought because I found it necessary in order to do the other part which is what I really love and that is sort of taking some difficult domain and you know thinking about it differently breaking out of a box that we're in a mental box and and of course building software around that you know it's always about the software and to do this really can be really fun I mean so many software projects are such grinds and most of the ones I've been involved in are are not that much fun but some of them are especially when people are ready to actually break out and so I've had a lot of experiences where you know we come up with wonderful new concepts of the domain and build software round it but many of those I can't talk about because you know they're proprietary business things with NDA's all over the place or because they're so arcane you know such a specialty area of some obscure domain that I would have to spend all the time I have just explaining the domain but I'll get to that in a minute well I love code really I love playing in code but I think also that it's extremely useful I love distilling these concepts and that's related to the playing in the code and then making this thing I call a supple design and I want to try to illustrate all this with an example which is not arcane and is not proprietary so and I'll try to show like just an example of how you can you know do these things and then in the end you know you just rip that up and try again and say okay well that was fun now let's see what else we could do and the thing is for the reasons I was saying I think that generic subdomains and by generic subdomain I mean you know domains that appear in many places that are not that you know not proprietary not specialized to a particular company or even in particular industry and they make good practice grounds I think they're good for an individual to practice and I think they could also be good for a community to sort of practice and explore how we do these things more concretely now I'm talking of course here at the tactical level mostly you know there let's design very specific things so this is where time comes in time is a good generic subdomain that everyone has to deal with and most people are familiar with either joda-time which is was a for a long time the standard library for Java programmers and then was sort of adopted into the Java library so now if you use the Java library as time as of Java eight you'd be using essentially joda-time with a little more refinement and I find that most of the other time libraries in most common languages are pretty similar so this is a good reference point and you know the thing that I'll talk about is how I tried to break out of the particulars of that mindset let's some so let's just jump in with a little look at joda-time nothing specific but I kind of look at the language of joda-time and you see like chronology instant is an important concept right this is an instant in time this is another instant in time between those two instances an interval that interval has a duration there's a lot of actually quite nice stuff in there and so you can do things like we could say well when was this session you know when was it scheduled to start so in joda-time you would have a an instant like that and then the interval of the of this session is an hour starting from 10 so that would be represented something like this now duration is like how long is it and so we can express that in Jo to time as well so we could say you know other ways of presenting it there's quite a lot there you can say well if I know that it's one hour long and it starts at this time then the end of it should be you know that starting instant plus 60 minutes gives us you know an hour later although that's wrong on the slide now but you know what time it ends or you know what time it was scheduled to end really now briefly the history of time libraries in Java and Greene I was there for it all because I started using Java when it was just this Java util date and I'm not gonna waste our time today talking about that but it was one of the worst examples of software design Java util calendar didn't improve it but joda-time was an open source library that started in about 2004 so I also started an open source library about that time which I think kind of fed a few ideas into joda-time I'd like to think but at any rate then joda-time matured probably about two thousand seven and then well nothing much happened for a long time I would say in fact until say it was adopted into the main java library and that wasn't really much changed you know it was just they fixed up a few little awkward parts but nothing really so why is it for more than ten years there's really not been much change in this most basic of generic subdomains and one reason I think is that Georgia time is good enough for most purposes most business applications it does what you really needed to do and so we stopped searching for alternatives and you know we get used to it then after you stop searching for alternatives you get used to it and then it seems natural and then the next thing that happens is that you forget that there even could have been another way to do it and you become unable to even think about another way and then happens this thing that I sometimes call legacy blindness where the legacy makes us blind to the possibilities of the domain because we get so used to it we can't think another way this happens to us I think on nearly every project but on nearly every project one of the obstacles to thinking really having original ideas is that our familiarity with the domain model that we already have and this is especially powerful force when you have a good legacy right when the legacy actually works pretty well as joda-time does or java time but I'll just say two at a time and so as I said we're going to look at this not really with an view of you know oh we're gonna replace joda-time with a new time library I mean that maybe someone will decide to do that at some point but I don't think that's really necessary in order to justify playing around it so one thing I get asked a lot is how do you break out you know if you've got this legacy blindness and your mind is just frozen how do you get out of it and so there are a few things that I do and some of you have seen this whirlpool diagram that I use sometimes and I try to emphasize how I tend to approach software modeling with this whirlpool but I won't explain it in detail right now just highlight two aspects of it and one is that in this whirlpool you have to come up with concrete scenarios concrete examples and that's very critical anyway but it's especially critical when you're trying to escape from this legacy blindness because what you're looking for is the example that shows you something awkward about that legacy diagram about that legacy model if you can't get that then you're and you may have trouble with figuring out what that is because the blindness makes you blind many things even where the awkwardness lies but you know you find a little bit of messy code and you say yeah that's more complicated than it seems like it ought to be so that's that's really key and then you need lots of variation you need to break out of that box you need a lot of variation and true variation not like minor incremental improvements those are important when you're trying to improve an existing model but when you're trying to escape from that model you don't want to be refining it you want to be thinking of real alternatives so let's think about that awkward example and so if we were trying to figure out some simple kind of business logic in joda-time we could say like did this session start on time so we have a planned start time and that plan start time would be represented as an instant in joda-time and then we have of course because we are going to start at an instant in time so we have some sort of grace period right I mean if if you really think about what it means to start on time it can't possibly mean start at that instant because well what is an instant anyway right I mean how long does an instant lasts doesn't last any time at all right so then um we have a grace period now that grace periods somewhat culture are specific right in American culture and many European cultures we might say maybe three minutes if it starts within three minutes of that start time we would consider to be an on-time start maybe the German configuration would be different like safe but the but in but in most cultures you've got a little room but I'd ask you what culture has this expectation because the the joda-time instance is represented to the millisecond that is literally the only way to represent time in in Java time right the nearest millisecond I think they may have changed it to nanosecond recently but you get the idea we'll stick with milliseconds that's enough pressure so I would say of course that this doesn't correspond to the realities of the domain right this millisecond thing it doesn't have anything to do with starting presentations right it takes me long how many milliseconds does it take me just to press this button so now if I looked at the conference website I see something like this right well I see actually exactly this and you see that it's put down 1700 so they've specified it to the minute they didn't say 1700 although I suspected the software that generated that fixer does put it like that and then has more code to say well we're only going to look at the we're only going to display that first part right and then of course the ending is 1800 so this millisecond anchor I think is one of the awkward things about at a time so we'll spend a little time thinking about that conference website says this joda-time says this and so and it's not so easy to manage that sometimes this is no doubt related to you know the the underlying representation is really the unix time that is if I were to parse I can you know Jota Tama can parse a string I could take this string representation of the time that's in the program and then I could parse it and I could produce of course it would stick all of zeros at the end and I can say get Milly's and and I'll find out the underlying representation is this number which is the number of milliseconds since January 1st 1970 in Greenwich or someplace so okay that sounds really natural so here I'm just gonna now I thought I said we've got to have a an example that illustrates some awkwardness is just one but now let's look at the other thing I said that generate lots of variation right real variation break out of the box a bit so you know I thought about time a lot and I thought well one of the things I hate is whenever I'm trying to write an example in code or when I'm trying to write a test and you have to write something like a new date time and then a number like 2018 and then you know oh one and so on it's like it's very awkward just even construct these things and I thought and then the result always comes out in something you know like lots of zeros and things and I thought what if and I was working in closure at the time enclosure you know it's like nothing really everything's dynamically talked anyway and I thought well what if instead of like using the way it's usually done I just said what if what if I just use strings as the data structure so if you look at the way closure time libraries are done they're nearly always wrappers around joda-time so they work exactly like joda-time does so for example in two at a time I could say new day time and put all that stuff in and then I'd get out a date time object and closer I might use a one of the libraries that would just say daytime and those same things go in with slightly different syntax and the same thing comes out wrapped in a you know Java I mean in a closure wrapper so but see that's not how most things in closed your work like closure has a very nice property for many of the data structures where the way it's represented is is like both you know they say both code and data and and so for example if I typed in a vector of these strings you know this ABC and I evaluated that that would evaluate to vector ABC it's it's the same you see that's very nice it's makes more difference than you think even if you haven't actually worked in a language that has this kind of stuff and but and so then they came out with these user-defined primitive or rather user-defined literals in closure at some point I thought oh cool we'll finally be able to have kind of a nice literal for time but they they put one in there and you can type inst and 20 1802 you know you can type that string and you evaluate this and you get this and I was so disappointed I thought well in what way is this literal right I mean it's not a literal in the way that ABC evaluates to ABC in it but it but that's the way they did it and I think it's because of this legacy blindness that's you know it's an instant and an instant is specified for the millisecond that's just the way it's done well in string time it's not done that way so in strength time we would say even I decided well why not use ISO 8601 because you know there and it's pretty universal and so the primitive for let's say today's date would be the string 2018 Oh 201 and that is the data and all the nm and or you could you know specify to the minute 28 o 201 T 1700 minus 7 is for the time zone for the time zone offset and you can also specify using you can specify durations and intervals everything's you know ISO 8601 very nice probably most of you dealt with it as something to format to a parse from but I said well why not why don't we just use it so then what would that mean of course it would be nice for storing things maybe because you can or passing them through some kind of messaging because they're using the string I'll just pass the string but operations in string time then worked on these strings so at a public interface level and and I had basically the same operations that joda-time had like plus plus lets you add you know as I showed before a time and a and a duration and then you get another time so that's how that worked and now I did it how did I do it well I wrapped joda-time right I mean under the hood I took that and I parsed it into a Joe did time date time and then I parsed that duration into a Jody time duration and then I used the joda-time plus operation to add them back and then I used the formatter to turn it back into a string you know that part wasn't all that pretty but it lets the it would if you were using this as a real library it lets the developer work at this level right now you may not think that sounds too good I can think of many drawbacks myself but remember our purpose here is not to say hey use this instead of joda-time our purpose is to start to break away from the legacy blindness to try to break away from the box that were in and I found this to be a really refreshing way to think about it you know because it's such a nice representation and it can be homo iconic that is I can put the same kind of things in that are coming out and I can read them tests just look beautiful right there so readable so I never did go very far down the string time path I wanted to explore it but I was looking for you know that that thing that would spur me then I had another idea I was out walking in the woods or something thinking about time as one does and I thought most of the time I'm interested in expressing at time like you know a lot of this I've been talking about it's been more about the expression of the time value than it is about the computations I mean I we did do that plus computation but mostly we're just trying to make a representation of something and I thought sometimes for example you want to represent the time of day but not to date sometimes you know you've got partial things so so I said well could I do that so I'd have like an expression of a date and and the actual syntax of this one was a little more complicated so I'm not going to show the actual code but it was basically something you could represent a date let's say but you could also represent an amount of time so for it's a lot like joda-time except you can represent dates only but you could also represent things like later so instead of plus now I was going to have a concept called later that's what plus does it says well I want to know the time this much later than that right so I thought well later then let's make later an actual object and so then you could put together a composite expression and there was a way to say well then I'm going to take two days I'm going to compose it was later which now means instead of the length of time two days it now means two days later and you could compose that with the date and now you have an expression that means two days later then you know that day then of course you'd evaluate that expression and somehow get so you'd go from January 30th to of course to February first hmm and and I got this working after a fashion you know not a fully general one and it made me think differently about time right I again I don't think if I were programming I would always want to have to deal with this but but it really made me think differently you could take a date and you could take a time of day those could be expressed separately as fine expressions and then you could there was another expression remember I had the later expression well yeah I also had an of expression so you could say 1700 of February first and that could be evaluated to be the date time and so on you know I I had this whole thing about time system I mean naming systems and I thought maybe times just a sort of system of names and I I in my notebooks I have this design notebook I scribble in a lot the design ideas I actually have a couple because I have one that I use for my real project and I have one that I use for this crazy stuff and there's there's pages and pages of this in there if anyone ever decides to you know try to prove that I'm you know mentally incompetent so they can steal my money or something all they have to do is get ahold of that look I started to think well then I really needed things I want to have an expression or a an operation called simplify what am I doing here I'm taking these different expressions and converting them into other expressions so if I said you know later two days and January 30th and I want to say well I want to simplify this expression and then I will produce that and I got that working the you know with some limitations and I said well these two are expressions of an equal value so what does simplify do it tries to reduce that thing to a simpler expression with the same value and so I had this idea that you know every expression would express a time value well not all did so I wasn't completely uniform but you could take things like notice that there's no year on January 28th but you can still say and it's a still a meaningful statement say five days later than January 28 so you could evaluate that and actually got that to work February 2nd is five days later than January 28 and those are equal expressions and or equal values but then you know you've got other situations like what about February 28 five days later than February 28 oh right how do you simplify that one what does simplify what would be the correct return value of this so you'd say well I could throw an exception I don't like the throw exception you could return nil or well actually one day I was probably taking a shower because that's the best creative thinking situation and so oh I know the reason I write I know the right answer to this the it's February 28 later five days of course this is all simplified does all it guarantees is that you take an expression and you pass in and expel it expression and you get out of all that expression all right it doesn't say that they won't be the same expression after all what if I pass in just you know February 28 by itself what would you expect to come out I'd expect February 28 right well same here so you can by the way start going down this path and pretty soon you're you know trying to invent you probably start worrying about computability and stuff so I didn't I pulled myself out of that spin but nonetheless you know you can compose very 28th of 2018 and you get the full day very just like putting the time together with the year I mean with the day or and remember I can't simplify this but I can simplify that so you know the composition together with the simplification it was just very interesting that really you know starts to change the way you think about this and suddenly you know you you think well what do we really need right we need the ability to anchor it some kind of timeline maybe some expressions do that some offset within the timeline some compose other things then you've got this simplify which can take expressions and produce other expressions and there are some I mean what happens with February 29 2018 I don't know see that's not a valid expression you could say right there's no such thing in I I didn't bother making pluggable chronologies in this case it's just you know the usual calendar and there is no February 29th so of 2018 that is right there is a February 29th in 2020 but not in 2018 so I thought well maybe this is the time to return in there or something I don't know now usually when I emphasize and I emphasize very heavily and for good reason that the main criterion for evaluating a model is usefulness that is you need to know what you're trying to solve with that model right models aren't for general purpose they're for particular use say in a particular application that has particular features that you have to make work and you want a model that is useful that allows you to express the things you need to express there to make the computations you need to compute and also maybe be you know understandable but there's another criterion I use when I'm exploring and I've come to call it productive rather than useful that is is a model productive of other models does he does the idea trigger new ideas some things that are useful are not very productive and some things that are productive or not very useful I'm not so sure how useful the time expressions thing yes but it's somewhat productive so does it give me new ideas and like java.util had marginal usefulness and very little productiveness unless you are just looking for that awkward example and then it's tremendous if you wanted to show like what doesn't work very well to inspire you but really I didn't count that as very productive but Jodha times pretty productive the ideas in joda-time and the ones that were in my library which is a bit like two at a time those were ideas that tended to lead to other ideas and for a while they did the way that interval led this duration and so on but you know then that stopped but I think they were productive in there I'm time expressions somewhere in between my coding scheme is that the greener it is the more productive and then string time I found very productive and there are other things that I did I can't even remember all the different time models I've come up with some of them were really dumb and I would look at them for like two days and be bored and I'm not gonna bore you with those now let's look in another awkward example so joda-time has this nice computation plus in take an instant you can add a period and get another instant so you can take you know January 20th and you can add a month to it and then you get February 20th and you can add two months to it and you get March twenty twentieth and so this looks pretty nice but what is January thirtieth plus one month what do you think that would be I can think of at least two possible answers right it might mean that I would add like I could go to just the end of the month or it could mean that I go a couple more days you know at the length of January well joda-time of course does have an answer to that it is the jhen it is that it is the end of the month so January 30th plus one month is February 28th and suddenly this plus doesn't seem quite so clear right it seems like you know that's a reasonable default but it isn't at all obvious last year I used this example in my presentation about imperfect designs and how to deal with that and I used this example and then I went on to say well we can mitigate this by giving it an awkward name for example giving it a name that presents what it really does like plus a round or something like that plus our end but this time I'm gonna use it as an inspiration to say well how could a time library be different right let's look at this a little closer as I said last year you know one of the things that you started to realize is it's not associative plus is usually associated meaning if I add a you know if I add one plus two and then add three it's the same as adding two and three and then adding one but that's not true with joda-time plus so if I say that for example you know if I said March 30th plus one month is April thirtieth grade but March 31st plus one month is also April 30th I feel like you know a plus B and you know eight or let's say a plus B and a plus C where B and C are not equal I feel like it a plus B should equal a plus C but they do in this sometimes and so anyway we've got the usual problems with months that have different links March 31st plus one month is April 30th but what about March 31st plus two months well I'll just tell you because you can't really guess but it's it's May 31st which would almost imply that if you took April 30th plus one month you would get May 31st but you wouldn't and if so so in other words plus two months is different than plus one month plus one month right this is just showing that there's something about this concept that isn't quite right but I'm not dissing joda-time by the way because every model has its awkward points and joda-time is better than most and and it's really quite good alright so here was another idea and this one was by the way very productive what if we stop thinking of time of the way we usually do is kind of quantity and just think of it as part of a sequence so where it's a kind of weird counting system so if in this scenario you know you could just count February 26th February 27th every 28 and just a list just a long list of dates or you could have like just a month right the months could be a long list of months like February and then March and April of a particular year and you know after 2017 December we get 2018 January and so you've got these different lists sequences of times this one I decided to refine somewhat and and I'm going to so I'll put this up on the last slide too you can go and look at some of the code for this if you're interested be kind though because it's just play code right it's it's not put there too well anyway it's in its enclosure so but I think that and I'm going to show some examples now enclosure code but I don't think it will really be a problem because it's pretty basic closure so let's say and I do need to explain a little tiny bit of closure so we've got threading macro so you can make an expression like five and then you could add three you know plus three is a operation divided by two and so it just takes the value of each one and as long and you would ultimately get four from that so I made a slight variation of this a threading macro four times and so this kind combines my sequence thing with the string time thing because what it does is it takes a string turns it into the internal representation which isn't so pretty and then at the end we'll turn it back so if time is a sequence it means that you could take a time value like say 2017 April 9 and you could and then you could parse that that's the from ISO and then you could just say okay give me the next one and then you can turn it back into an ISO strength or using my fancy macro you can just do it like that so we take a date we say next and then end so if I said well what's next after April 9th its April 10th it's kind of straightforward right and this is just code straight out of some of the test cases and the particular testing framework I use uses the word fat to say this is an assertion about the code so you know April 9th goes to April 10th but if you just put in April then it'll just go to May right so what's next after April of 2017 May of 2017 what's next after 2017 itself 2018 right or what's next after a particular minute the next minute or what's next after a particular date the next day I guess I already did that one but what's oh I see but what's the next day after to February 28th well it's March 1st if it's February 28th 2017 but if it's 2016 then it would be February 29th right it's a list so all the irregularities just fit right in for the list the list of days the list of you know it doesn't really matter that the months don't have day because Alyssa's don't have to have the same number of elements in any kind of conception you have about lists will carry over and or you know there's different representations you could say that kind of year and day of the year thing so the the 71st day of or the 70th day of 2017 the next day is the 71st day and you can roll over to the next year that way but it's not rolling over it's just continuing the list right it's not some kind of complicated rolling over thing or you can do it with weeks these are all valid ISO expressions of time now when you look at these difference lists okay that's kind of intriguing but to really get versatile any kind of power out of this I needed another concept and that was that these different representations were nestings so by that I mean that you have a sequence of years and you have a sequence of months but you could take some of those months and you could say they are nested within a particular year so nested within 2018 is 2018 January 2018 February and so on twelve months and nested within the months or days and nested within the days are hours and nested within the hours of our minutes or you can say well I'll nest directly that the days within the year so then in that case 2018 has day one day two day three all the way up to day 365 this turns out to be really useful well so you can but you know so you can do things like alright I've got 20 1804 that's April and then I nest days within it and I've got a new sequence which is 30 days long but if I nest day with it directly within 2017 I get a list 365 long or if I use 2016 its 366 because 2016 was a leap year and so on so now what can I do with this well of course I have some fairly obvious than operations like I want to know if this is a nested thing I want to know what like what's it enclosed in so if I have a date I can say what's the enclosing year if I have you know a minute I could say what's the enclosing hour or what is it enclosed within so now this this say this you know date plus month thing that is not in this list of nested things it isn't really an expression that makes sense you could ask what's the next month you could ask you know but you can't really say well plus one month but then that didn't work too well in joda-time and then I think oh okay so how do you reconcile that but one thing to always remember is maybe it wasn't really something we did enjoy at a time I mean yes we do use plus but do we use it for that kind of thing so what are we really trying to do alright so here's another idea that I kind of you'll see mix and match tie did it somewhat with the time expressions now I'm going to do it with these lists so if you were to take the sequence operations because once you make a sequence out of something you can use all of the base libraries capabilities on that sequence right and the navigating the nested hierarchy and with that you can do things like well instead of plus let's take later you can't do this you could say all right I want to say you know one month later I can say that with months so if I had like 20 1704 that's April and then I say later five I could go five later that's unambiguous or so I made this operation I mean I just cook that up I didn't that's not really part of the library but you can make it very easily so you just say take time and get a sequence starting from that time and then take the nth one from that sequence and that gives you something kind of like but not quite like so what are we trying to do with that plus sometimes we're trying to figure things out like I know I've used it when I needed to get the end of the month so I would you know go forward one month and then figure out the end of the month or something so let's say you wanted to do that well this is really natural with these sequence things take a time any time more specific than a month anyway and take the enclosing month then say and then that's the days in it and take the last one right the last day of the month is a sequence of statement when you say what you want it's actually a sequence Damon end of the month last so you know if I had April 19th and I said in the end of month it would be April 30th or I could just say April in two months the same function works or I could have some specific time I had just get that notice I just get the date end of month is really more of a day concept I mean I could work out something if I actually wanted to know the last minute of the month I could get that usually when you say into month you mean the day how about a few more what kind of wild ideas one day I was thinking you know month cycle and that cycle has days embedded in it so that's kind of another cycle the irregular one and then you've got hours cycling and minutes and I had this whole sort of wheels within wheels thing I wrote code for that one in Java it was it was really something but I think I lost that code long ago these are pages from my notebook don't they look old like parchment or something well that's what happens when your basement floods anyway cycles and epics were fun though it was an idea that got me thinking about ideas statistical patterns in time was this one was for a real project this wasn't just a while I was working on a project where I needed to find patterns in events and so I wasn't so much interested in specific times as I was a pattern of how they you know the frequency is variation and so you know you could look at that one on the left is sort of just a sequence of time stamps and then on the right you've got this kind of trying to identify these bursts and anyway I won't go into the details it was quite a fascinating project this of course is a totally different model driven by a totally different kind of goals you know different kinds of operations that you would want to do with it different things you want to represent so this model isn't useful for the same things but it's very useful for some things which joda-time and the and its ilk are not useful for hardly at all or I went down a path of like we had time intervals could that just be a more generic concept which it certainly could be so instead of having interval of time you could have interval of anything like a number or price and then you'd have all these things that could operate on intervals like basic set operations you could find gaps between intervals but then once you've got it it wouldn't just work for time it would work for prices and so on and I did that you know that was that I was thinking some but here's an idea that turned out very productive remember I was saying what is you know what is an instant like what an instant is it right now I mean now I mean well the word now takes time to say so means some other way of saying it what if we didn't have instance they make certain calculations confusing like if I say you know midnight of you know today at midnight but midnight at the end of the day is that the end of today or is at the beginning of tomorrow or in an instant of course it doesn't matter because it's never at that time anyway it separates today from tomorrow but if you have it you have to decide joda-time actually has a convention that the beginning if it the the midnight or the time whatever at the beginning is included in the interval and the one at the end is not so the instant of - so for today the instant that began today is included in today and the instant that ends today is not included so that works but you know if you just say well today is an interval what do I need the instant for what if I just got rid of the whole concept of instant every place I was saying time and you know I said well time is a sequence of these things instead of saying time is a sequence of instance or I didn't specify notice I was a little vague when I say what are these sequences of but what if I say instead of being sequences of instance they're sequences of intervals a lot of things get kind of make more sense all of a sudden you can talk about you know these different scales the nested scales you know date is an interval one day long a a minute the specifying to the minute is one minute long you can also make intervals by saying the beginning and ending of it but instead of saying those are instance at the beginning and end what if I just say those are that's the first and that's the last right that's so the January 30th starts the interval and and February 2nd ends the interval that's so if sequences those sequences I talked about our sequences of intervals the only thing I'd have to say is that those intervals are of the same scale the same nesting and they meet I want those intervals it meets this interval so the you know April 9th meets April 10th and April 10th beats April 11 or April meets May or you know you can do it with a specific time of day so you know I get a lot of resistance from this one and so it's a good place to make the point that a lot of point of this is you've you've got to practice letting go of ideas you're attached to maybe instant really is a useful concept I mean I haven't actually found a point in trying to calculate things using intervals that I really have to have it but maybe I will and anyway I'm quite open to the idea that it would but sometimes it's really good for you to just say well this I am so attached to this idea I'm just going to get rid of it and see how far I can go without it I do that a lot sometimes I pick what seems like the most useful object the most useful concept that we haven't throw it and say for the next two days we cannot use that word we cannot talk about this sometimes you just prove how vital it is but sometimes you say wow that was getting in our way sometimes in much later you bring it back but in a smarter form a slight revision of it that makes it work so anyway just bear with me just let's not use incense right but then I was thinking okay so how do you actually compare these things so this might be that awkward example that breaks this whole everything's an interval model right you need to try to find such things so let's go back to this example that we had did the sessions start on time so in joda-time remember we had an instant of the beginning and then we take a grace period say 3 minutes so we get an instant that's 3 minutes later and then we need a time stand there's a timestamp just reading a clock right so we get this timestamp which is a that's something pretty close to and so then you say well alright that was our timestamp and is it before the start time with grace period and yes it is so you'd do it that way in joda-time how would you do it with only intervals though because the thing is you know in joda-time you've got before and after but they only work on instance and before is confusing with intervals right like if you think about intervals that sort of overlap each other is it before is it after I mean you can make a case for this one being before and after that other one because it's completely overlaps it so I thought oh boy maybe this is it maybe this is the one that proves why we need instance and if that's the case then I'm happy again to use instance because I know why I need them sometimes you know you don't want to just think of stuff sometimes you don't want to just try to reinvent everything yourself especially in generic subdomains there's a lot of prior work and sometimes it's done in a nice formalized way so I went searching I do a little research sometimes and I went searching for an answer to this and um you know and so we'll just have a little interlude for research you know a little googling read an article here and there Wikipedia and then one day I hit the jackpot there is this thing that was from the 1980s called Allen's interval algebra it was a from a mathematician that when you find something from a mathematician you think ah you know there's some those people you know they've got a few things figured out they're not quite so sloppy so so anyway Allen proved that they are exactly 13 basic relations between two intervals these are them before is one of them well you know if if an interval takes place before he said well that's when it's completely over before the next one starts right if they touch that's meats if they overlap for the first one so on every possibility and then an inverse right so you could say well X meets Y or X or Y is met by X so that's so the total adds up to 13 why are not number because the inverse of equals is equals right so you get 13 now with this you can unambiguously say the relationship between any two intervals so to use that example you know you could say for example that 2017 is equal to 2017 2015 is before 2015 and inverses and so you could have this one starts like January 2017 starts 2017 and December 2017 ends 2017 or you can if you start using different different nestings you can make things that overlap like 2017 week five overlaps 2017 February so anyway now let's go back to that question did the session start on time well we can represent the time much the same except we'll use minutes instead of millisecond so there's a three minute long interval of the acceptable start and then we have a timestamp which is a millisecond and then we say and and you know which is to say this and by the way now we have to think well now how does that relate I could nest I'll just nest seconds within hours and milliseconds within seconds and I'm happy to do that but in another aspect of this is like what did we really read from the clock what we really read from the clock was a unix time which is another sequence right that that's just a sequence of milliseconds it doesn't have nesting because it's only one it just happens they have a trivial nesting right just milliseconds like years so we have that and so what we're really doing is taking one of those and then we are going to map it to a different nesting much the way we might map you know the tenth day of 2018 to January 10th of 2018 we could map that nesting we can map these nestings to these kind of milliseconds so I can so that's how I can get from there to there and now we can say I don't quite know why it says well anyway now we have to say well what relation which of Allens relations apply so if I take the interval of the acceptable start and that one millisecond long interval that represents the time we read from the clock because what is a time stamp it's not an instant it takes actually takes time to read the clock how accurately said is your clock actually it's less accurate you know if it was the Java the spec actually doesn't even restrain it to one millisecond it's weird that it gives you a millisecond but it actually doesn't promise to be within the mill set I think it generally is but never mind that the point is it at the best it's giving you an interval of a millisecond right and I'm just saying well what's the relationship between that interval and the interval of the acceptable start well I'd be happy if it's in the middle somewhere over to the beginning of the end but not if it's outside in anyway the three the three Allen's it intervals are starts during and finishes any of those three and I'm happy so I make a set of starts during and finishes and I say is the relation in that set here's code that does that soon so this line down here at the bottom is actual executable code that's how you represent a set enclosure I was just saying calculate the relation of these two times and see if it's in that set and a lot of time logic revolves around this I know that using more traditional time libraries I've gotten into some very confusing kind of things where how do I say if this is within a certain interval and or even or if it's you know I have to write could say well if it's at the beginning of it separately demand or so anyway this just takes all that away I was quite pleased with that going back to my little map then we've got time series we got the everything's in interval those were all quite productive and then Alan's interval lounge was fantastically productive so I gave it a sort of greener than green color here you know you can sort of make the library less tied to the clock why is everything a millisecond in joda-time I suspect it's because the early use cases were all related to you know read the time from the clock the current timestamp was the first case and then all the other stuff was built around that but actually a lot of time logic doesn't relate to that if the contract says that the contract I mean this data file says as a contract expires on March 31st you know do I need to convert that into a millisecond I may need to at some point but most of the reasoning about the contract is going to involve you know different kinds of date/time stuff by the way I'm not going to talk much about time zones they are a hairy thing I do think that joda-time did a fairly good job of dealing with time zones and I think that the Java version actually made some nice refinements in that area but how does that relate to this whole thing I've been talking about well if you think about it you could say I'm gonna skip through this all these different time zones and stuff could be viewed as just different sequences so if I say that just I'm going to say well you know a certain minute what's the next time it's the next minute I could just stick a timezone offset on there and I'd say well then I get the next minute in that time zone offset actually a great deal of logic we do works just with this then you need some mappings between by the way their offsets and then their actual time zones and you need to be able to map between different offsets of course but that's not too hard but the time zones are quite distinct from the offsets one of the weirdest arias things is about daylight savings time right so I'll just talk a little about this and you know close because I know we're a little over time if I say take my home time zone and the awkward way it's done is to you know these names of a basically prominent city within that time zone but that's the way it's done and that time zone has daylight savings time so in 2017 11:05 at 2:00 a.m. you know they switch from offset - for to offset - five and but if you're just doing a sequence this is actually weirdly easy to do because I'm saying that the sequence of New York time zone is not a particular UTC offset sequence it has elements of the -4 and then it switches that elements from -5 so that whole thing like we're you know we're 130 shows up twice each year I mean and it does you know we got like what's after 159 minus 4 is 100 minus 5 and but what's after 159 minus 5 is just 2 minus 5 right 200 minus 5 so it just sort of slid into place and I thought that actually was kind of intriguing but offsets don't have time zones right the UTC offset itself and this is one of the tricky things because the time of the time libraries have a tendency to strip the time zone away and just leave you with the offset and then you're screwed because you know you come to the savings time point and you don't have the time zone you just got the offset so instead of going from 159 minus 4 you go to 4 to 100 minus 5 you end up going to 200 minus 4 that's happened to me actually all right so I'll wrap up here I I think that there are there are basically three of these concepts that are explored in this time count library so if you're interested you can look at it it's in closure but even without knowing closure I think you could read the tests and the explainer files that I put in there so it kind of explores the sequence of time concept the everything is an interval concept of Allens algebra composing these operations a bit and it has a different language than then joda-time more revolving around sequences as nesting relation next it's important and so I encourage you to look at that if you're interested just to reiterate then some of the main points it's good to practice and to explore and I think a good way to do it is to find a Jeanette is to find one of these generic subdomains that's been a bit neglected because we're all used to it and just see if you can break out see if you can get that awkward example see if you can just think of some other crazy way it could be done even if you're confident that that other way is worse than the way it's usually done now that's ok you just need it to be different right not a minor tweak of the existing one that doesn't get you very far and then get into code and try these ideas out because well sometimes when you get in the code you realize they don't work or sometimes you realize the new possibilities you know code is a very it's a good medium to work in you know sculptors don't just think about what their sculptures should be they go in and mold clay and and it takes shape and also you know the language that you're working in can have an influence this sequences of times I think if I had not been riding in a lisp based language where everything is sequence is everything is lists of things and so it's very natural to say well what if this was just a list so again it's kind of like if you think of a sculptor or sculptor who's molding clay he's not going to produce the same form that a sculptor who is chiseling a marble or a sculptor who is going to you know carve wood the medium that you're working in or won't create the same image as a painter would create right medium you're working in matters and it's inspirational and it's also constraint and so I encourage people to do this thing that I often do just go deeper and deeper into a domain and take time you know I've been working on time off and on for many years but I also gone into a few other ones that interested me some though more are kings so they're not as easy to talk about but it doesn't matter just something that makes sense to you and then also I emphasize will go ahead and get variation even if the variation is inferior to what you already have but at the same time practice rigor to you know we want models that are tight and that you know you can really make assertions about and you know and and do computations that won't have a lot of edge cases and surprises so work on that and a good other thing to do related to that is to research the established formalisms you know I've found a very specific one related to time intervals but there's tons of stuff out there some of them are more abstract do you know more general and now just I'm closing so I'll just say you know sometime when I'm gonna be here at the drinks reception and all day tomorrow and if you corner me you can ask me how do you calculate Thanksgiving using this it's kind of fun it's you know you if you think about it's a very sequence oriented thing what's the fourth for a Thursday of November it's actually the question so anyway on that note thank you for your attention and I'll see you all at the rest of the conference [Applause]
Info
Channel: Domain-Driven Design Europe
Views: 13,806
Rating: 4.8270268 out of 5
Keywords: ddd, dddeu, dddeurope, domain-driven design, software, software architecture
Id: T29WzvaPNc8
Channel Id: undefined
Length: 72min 30sec (4350 seconds)
Published: Tue Aug 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.