5 ms·
Could you share an example of this? I could be misinterpreting this but say you're building a survey system and you need to keep track of answer types (e.g. tex
by amingilani 6y ago
Could you share an example of this? I could be misinterpreting this but say you're building a survey system and you need to keep track of answer types (e.g. text, date, etc) and the answer itself, how can you collapse that into one field?
- winrid 6y agoYou have an Answer object that probably contains a type enum. However, you don't need to name all variables "answerObject".
- novembermike 6y agoIn something like typescript you could represent this as a union type. If I'm expecting a location I could take a string ("Baghdad"), lat long pairs, an enum, etc. With a union type I can specify that it could be any of these but they fill the same purpose. In Java I'd either do method overloading or expect to receive an object that implements a method that would give the location in a common format. The wrong way to do it would be to have a function that has both a lat/long input and a string input and just say that they're nullable and we only expect to get one of them.
- numlocked 6y agoI think they are referring to this sort of thing (the reader will have to use their imagination and assume there is additional functionality in this class): class ReadingMaterial: is_magazine = False def is_periodical(): return is_magazine Using a base class (e.g. a Magazine and a Book class, or something) and inheritance, is much clearer than monkeying around with state.
- gumby 6y agoYour thermos does not have a switch which you must set to "hot" or "cold" before inserting a liquid, handles solids as well as liquids, and doesn't require you to even think where the theshold might lie between the "hot" and "cold" settings. Instead it just does its best to prevent an energy exchange in either direction without having to even know the variables involved. And the "same subroutine" is basically used for different sized thermoses. In code I see people not understand this all the time. Here's an example: let's say you present the user with their previous orders, and give them the option to filter those orders by some criterion. The shitty way to do this is to have two variables: Filter* orderFilter = NULL; bool filterSpecified = false; void setOrderFilter (filter* newFilter) { orderFilter = newFilter; filterSpecified = true; } So instead of just checking if a filter has been assigned, you have a separate boolean. What happens if the boolean is true but the filter is null? What would the vice versa case even mean? The global codebase is riddled with dumb errors like this.
- nicoburns 6y agoYep! On of the things I point out most frequently in code reviews are things like this. However, null is an imperfect system for capturing such state. It's not self-documenting, and it breaks down when you have more than 2 states to represent. Languages with Sum Types represent this much more elegantly with arbitrary numbers of variants and force you to check which one you have before accessing the more specific data (e.g. the filter) inside.
- zbuf 6y agoEven better, let's just have the default orderFilter be the equivalent of '*'. If there's always a filter, then there's no longer a branch at the point it's used -- that needs to be tested and maintained. I used to work with a programmer who I found difficult because their code was an ever-increasing number of "if" statements as each new case came along. Conversely he would say coding is relatively easy and that I was overcomplicating a problem by thinking about it; all I needed to do is "add an if statement here".
- roelschroeven 6y agoHaving the two separated gives the ability to disable/re-enable the filter without losing it. That's often very useful. In other cases you're very right.
- diarrhea 6y agoThen you'd go with `filter_enabled` or similar, which is an entirely different, separate flag. It is not coupled to what the filter itself looks like.
- gfody 6y agothis is the essence of Linus Torvald’s doctrine of good taste
- foota 6y agoAnother way of expressing this concept is to "make invalid states unrepresentable"
- 6y ago
- layoutIfNeeded 6y agoIn graphics programming the typical example of this pattern would be when someone does bespoke computations on coordinates with a bunch of if-elses instead of deriving the proper matrix equation that "just works" due to math.