That's a design (anti?) pattern I see at work. Essentially they are decoupling the data creation and data usage via the use of a "data holder" object. It's annoying to have as a default design but .. it did make it easier to add new data usage clients.
I ran into this at work the other day - Revit presents drawings as Views which you place on Sheets for printing. Behind the scenes, Views are called Views, but Sheets are called ViewSheets. It's very confusing and I hope they refactor it some day.
It's almost enforced if you use protobufs. You could directly store and use the types but it'd be quite awkward. It's makes the distinction between external interface and inner workings stark.
Lets say that ‘item’ contains more attributes than visible here. In such a scenario what would be a better approach for using a subset of attributes to a construct a different object?
Thing is, why make one property for each day of the week representing whether or not it's that day, rather than just one property representing which day it actually is? It reeks of wanting to use it as "if Monday, else if Tuesday, else if Wednesday," etc, which smells of poor quality business logic.
Can someone please recommend a better way to do that? Because we do it in our project too (mobile app and server backend) and I honestly don’t really know how to do it better. Although it’s just boilerplate.
Then your coupling item to WorkingHours, if you change one you’ll have to change the other. The og code is valid if they need/want these things decoupled.
The only questionable thing about this is the use of is<dayofweek>, instead of something like dayOfWeek
The current solution doesn't remove that coupling though, it just sprinkles it around the project everywhere the conversion happens. I'd make a new static class that has a static method that accepts one and returns the other, and this code would be the body of that method. Same level of coupling, but DRYer
Seeing as you're already creating a static class for this purpose, any reason you do this instead of an extension method like `WorkingHours.FromItem()` (whatever the other class is called)?
I thought about it, but felt using it as an extension method would kind of bring back that coupling a bit more(?) Thinking more about it, I guess it doesn't really, but it feels wrong
If using ES6+ You could shorthand it as new WorkingHours { ...item }; saves some typing and makes it easier to read if you also add additional data to the object.
edit : unless *gasp* someone isn't really writing OOP and this isnt just setting the properties of some object to the values of another object of the same type. Please tell me it isnt so
I actually have a noob question on this, i’ve seen this pattern on low level languages, in order to handle the incoming object, you create a new instace of it. Right?
Everyone seems to be focusing on the mapping (being confused by it or explaining why it makes sense).
The real problem of this code is the design of WorkingHours, which seems to be - as happens very commonly - copying the external data model instead of mapping to a better domain model. Anything that "is Monday" cannot be Tuesday at the same time, so what seems to be something like a bit field in the external data model (e.g. for efficient serialization) should be mapped to a Day enum. The WorkingHours could still have properties IsMonday etc. which should be readonly and return true depending on the respective enum value, but it should not be possible to set 2 days to true (unless it can span multiple days, in which case the naming is bad, it would not "be" that day anymore).
It could look something like this:
var workHours = new WorkingHours
{
Start = item.Start,
End = item.End,
Day = ExtractDay(item)
};
This looks like an image, perhaps a screenshot, comprised of many picture elements, with a common dark grey/gray-ish and a hint of hidden green background, with other picture elements arranged just so, emulating the display of words that seem written in a fixed-width font, such that one might consider it to be of or relating to a programming language.
Within that context, it appears to resemble maybe a kind of factory or builder pattern, maybe of a representation of a six-day work-week, or it could be part of a deserialization block of code. Another option would be a kind of an instance-copying function.
The interesting portion of this text is the trailing comma after item.IsSaturday; if this is indeed C# code, is that syntactically correct?
180
u/LiquidIsLiquid Jan 07 '23
It's for when your working hours can span several days.
104
u/chrisrrawr Jan 07 '23
Just a mapper. Ideally would be generated code with npe protection but sometimes u just gotta hack together something shit to make the data go
60
u/Fenxis Jan 07 '23
That's a design (anti?) pattern I see at work. Essentially they are decoupling the data creation and data usage via the use of a "data holder" object. It's annoying to have as a default design but .. it did make it easier to add new data usage clients.
109
u/ezerinsh Jan 07 '23
Lets say that ‘item’ contains more attributes than visible here. In such a scenario what would be a better approach for using a subset of attributes to a construct a different object?
19
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 07 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/augenvogel Jan 07 '23
When you have to map one external Interface to another external interface.
7
u/[deleted] Jan 07 '23
Can someone please recommend a better way to do that? Because we do it in our project too (mobile app and server backend) and I honestly don’t really know how to do it better. Although it’s just boilerplate.
22
u/Pangocciolo Jan 07 '23
That's 99% of business software. Stupid data copied in stupid data from one side to the other.
6
u/nikstick22 Jan 07 '23
I guess you gotta hope work hours don't cross midnight
9
u/[deleted] Jan 07 '23
I miss “IsSunday”, probably a bug 🤣
2
u/[deleted] Jan 07 '23
Looks like declaring a new set of variables. To avoid bugs, maybe they dont want to modify the original source of variables.
1
u/IxD Jan 07 '23
dropping extra properties before ES6. And using prototypal inheritance for type checking.
1
u/youngbull Jan 07 '23
Well, it sure ain't Sunday.
1
u/kayjaykay87 Jan 07 '23
Oof I feel like I could've wrote this..
1
u/miheishe Jan 07 '23
What Saturday is doing here?
1
u/O_X_E_Y Jan 07 '23
Does JS not have a sum type
1
u/CaitaXD Jan 07 '23 edited Jan 07 '23
had to guess taking a subset of propieties from item to be serialised and posted
Or the other way around receiving a dynamic object from a database/get and converting into a POCO
1
u/Extreme_2Cents Jan 08 '23
It’s the blue print for the biggest Ponzi scheme since taxes…
1
u/toastal Jan 08 '23
Can't see how this data structure could ever be in an invalid state
1
u/[deleted] Jan 08 '23
how is this bad code...?
It depends what the context is i can't see anything wrong with this atm i can't lie
1
u/Classic_Attention_96 Jan 08 '23
why not just write a copy constructor?
edit : unless *gasp* someone isn't really writing OOP and this isnt just setting the properties of some object to the values of another object of the same type. Please tell me it isnt so
1
u/luisantos1986 Jan 08 '23
I actually have a noob question on this, i’ve seen this pattern on low level languages, in order to handle the incoming object, you create a new instace of it. Right?
1
u/Ok-Instruction4441 Jan 16 '23
Everyone seems to be focusing on the mapping (being confused by it or explaining why it makes sense).
The real problem of this code is the design of
WorkingHours, which seems to be - as happens very commonly - copying the external data model instead of mapping to a better domain model. Anything that "is Monday" cannot be Tuesday at the same time, so what seems to be something like a bit field in the external data model (e.g. for efficient serialization) should be mapped to aDayenum. TheWorkingHourscould still have propertiesIsMondayetc. which should be readonly and return true depending on the respective enum value, but it should not be possible to set 2 days to true (unless it can span multiple days, in which case the naming is bad, it would not "be" that day anymore).It could look something like this:
1
u/[deleted] Jan 07 '23
This is inception
-1
u/[deleted] Jan 07 '23
This looks like an image, perhaps a screenshot, comprised of many picture elements, with a common dark grey/gray-ish and a hint of hidden green background, with other picture elements arranged just so, emulating the display of words that seem written in a fixed-width font, such that one might consider it to be of or relating to a programming language.
Within that context, it appears to resemble maybe a kind of factory or builder pattern, maybe of a representation of a six-day work-week, or it could be part of a deserialization block of code. Another option would be a kind of an instance-copying function.
The interesting portion of this text is the trailing comma after
item.IsSaturday; if this is indeed C# code, is that syntactically correct?3
u/[deleted] Jan 07 '23
[removed] — view removed comment