Tag Info

Hot answers tagged

47

If all you want to do is create class X with certain arguments, subclassing is an odd way of expressing that intent, because you aren't using any of the features that classes and inheritance give you. It's not really an anti-pattern, it's just strange and a bit pointless (unless you have some other reasons for it). A more natural way of expressing this ...


13

Which of these intuitions is correct? Your coworker is correct (assuming standard type systems). Think about it, classes represent possible legal values. If class A has one byte field F, you might be inclined to think that A has 256 legal values, but that intuition is incorrect. A is restricting "every permutation of values ever" to "must have field F ...


7

No, it's not an anti-pattern. I can think of a number of practical use cases for this: If you want to use compile time checking to make sure collections of objects only conform to one particular subclass. For example, if you have MySQLDao and SqliteDao in your system, but for some reason you want to make sure a collection only contains data from one ...


6

Because there's no obvious way to complete the tail. Any choice on how to do it would result in a non-obvious tail. The trick is to explicitly lengthen your shortest list to match the length of the longest with values you expect. If zip did that for you, you couldn't know what values it was filling in intuitively. Did it cycle the list? Did it repeat a ...


6

This is difficult to answer without looking at the code. However, it is clear that these methods are trying to do too much. They are almost certainly violating the SRP. I would suggest that you begin by looking at where the methods are different and why. Perhaps you could use method injection to inject different code into the method, based on who or what ...


6

If something is a pattern or an antipattern depends significantly on what language and environment you are writing in. For example, for loops are a pattern in assembly, C, and similar languages and an anti-pattern in lisp. Let me tell you a story of some code that I wrote long ago... It was in a language called LPC and implemented a framework for casting ...


5

For the use case you've described, I think it's a valid pattern. However, there is no name for this, AFAIK. In Java 8, there is a Map.getOrDefault method, which almost does that: String greetingKey = "Hello"; Map<String, String> i18n = new HashMap<>(); i18n.put(greetingKey, "Hola"); String greeting = i18n.getOrDefault(greetingKey, ...


5

My problem is that the code that I have inherited is, in my opinion, absurdly over-engineered. I have enormous problems following the program flow and finding any concrete implementations of anything. The amount of abstractions are totally mind-boggling and there is no documentation whatsoever. Its not you, I find that a lot of C# and Java (and ...


4

First of all, you're setting up Animal interface with generics in order to make the Animal instance passed to fight as the same as the one implementing Animal. In other words, Lion can only fight a Lion. A Tiger can only fight a Tiger. From the sounds of it, you want to be able to fight any animal. If you don't need to use A for any other reason in ...


3

It's just a class within Noda Time. It's not a specific design pattern that I could name. It's used to build up a text parsing/formatting pattern which consists of a number of steps. When formatting a value, we basically start off with a value (e.g. a date) and an empty StringBuilder, then apply each step in turn, supplying each step with the current ...


2

No, Chain of Responsibility doesn't make sense here, because it assumes all components have same interface. I don't think Java's type system is good enough to make this fully generic, so I would opt in to type erasure and some kind of "manager" that pipes output of one module into input of next one, while encapsulating the erasure. The module's interface ...


2

The strictest Object Oriented definition of a subclass relationship is known as «is-a». Using the traditional example of a square and a rectangle, a square «is-a» rectangle. By this, you should use subclassing for any situation where you feel a «is-a» is a meaningful relationship for your code. In your specific case of a subclass with nothing but a ...


2

The most common use of such subclasses that I can think of is exception hierarchies, although that's a degenerate case where we would typically define nothing at all in the class, as long as the language lets us inherit constructors. We use inheritance to express that ReadError is a special case of IOError, and so on, but ReadError need not override any ...


2

Too difficult to assess without having a code segment to analyse. Method consolidation should definitely be applied here, you're just going to need to keep a few principles in mind. Have a read through this article on Jeff Atwood's blog: Curly's Law: Do One Thing Pay attention to the three core principles of modern software development, namely: Don't ...


2

It's almost always what you want, and when it isn't, you can do the fill yourself. The main issue is with lazy semantics you don't know the length when you first start the zip, so you can't just throw an exception at the start. You would need to first return all the common elements, then throw an exception, which wouldn't be very useful. It's also a style ...


2

It's a guard or a guard function. The idea is that checks to make sure that some condition is true and then either continues or aborts the branching condition. It can also be regarded as a kind of filter function. This should not to be confused with guards as they are used in Haskell and other functional style programming languages, which operate more like ...


1

I can think of a few reasons to expose the class and not the singleton instance. Testability of the service Reusability of the class Life cycle management of service instances If you are purposefully hiding the class constructor, how can you ever write unit tests that exercise only its code and none of its dependencies (its constructor arguments). You ...


1

I think you're looking for 2 things, but I answered this question a while ago This is a very clear example of the Command Pattern Basically it goes like this: Look up the thing to do Do it If you use a defaulting map (there are implementations around) then, in the event the command was not found, you can do the default which can use the Null Object ...


1

If you are looking for a class for code that calls a winner of 2 animals fighting, then the solution is a third class that does that. Imagine you have to write a compare method that compares objects of 10 very different classes. Obviously you can't have code doing the comparing spread out all over those 10 classes. It's much cleaner to write a Comparator ...


1

As others have mentioned, these methods likely violate SRP. What I do in cases like this is I refactor out common code. Maybe a block of code is frobnicating widgets, but if you take a step back, it is really only transforming one data structure into another in a way that can be generalized. Maybe that is used in both methods: pull that out into a third ...


1

One common way to refactor code like this is through inheritance, using abstract or virtual methods for the behavior that may vary. Let's say you have a method to pay employees, and another one to pay the CEO: void PayNormalEmployee() { // do stuff // do more stuff // give meager bonus // do final stuff } void PayCeo() { // do stuff ...


1

Let me first note that as the code is written, the sub-class is not actually more specific that the parent, as the two fields initialized are both settable by client code. An instance of the subclass can only be distinguished using a downcast. Now, if you have a design in which the DatabaseEngine and ConnectionString properties were reimplemented by the ...


1

I use constructor only subclasses quite regularly to express different concepts. For example, consider the following class: public class Outputter { private readonly IOutputMethod outputMethod; private readonly IDataMassager dataMassager; public Outputter(IOutputMethod outputMethod, IDataMassager dataMassager) { this.outputMethod = ...


1

I think the key to answering this question is to look at this one, particular usage scenario. Inheritance is used to enforce database configuration options, like the connection string. This is an anti pattern because the class is violating the Single Responsibility Principal --- It is configuring itself with a specific source of that configuration and not ...


1

A subtype is never "wrong" as long as you can always replace an instance of its supertype with an instance of the subtype and have everything still work correctly. This checks out as long as the subclass never tries to weaken any of the guarantees the supertype makes. It can make stronger (more specific) guarantees, so in that sense your coworker's intuition ...


1

This doesn't sound like any Dictionary class I've heard of, but it could be trivially implemented by wrapping a dictionary in another class (or inheriting from it) and modifying the Get behavior.



Only top voted, non community-wiki answers of a minimum length are eligible