Tag Info

Hot answers tagged

79

Because there are no functions shared by all objects. There's nothing to put in this interface that would make sense for all classes.


52

Would there ever be a something_else Bob = new Person()? Yes, because of inheritance. If: public class StackExchangeMember : Person {} Then: Person bob = new StackExchangeMember(); Person sam = new Person(); Bob is a person too, and by golly, he doesn't want to be treated differently than anyone else. Further, we could endow Bob with super ...


51

Because what would that object have for functionality? In java all the Base class has is a toString, a hashCode equality and a monitor+condition variable. ToString is only useful for debugging. hashCode is only useful if you want to store it in a hash-based collection (the preference in C++ is for std::vector and plain unordered lists). equality without a ...


51

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 ...


34

Let's take your first line of code and examine it. Person Bob = new Person(); The first Person is a type specification. In C#, we can dispense with this by simply saying var Bob = new Person(); and the compiler will infer the type of the variable Bob from the constructor call Person(). But you might want to write something like this: IPerson Bob = ...


22

Whenever you build tall inheritance hierarchies of objects you tend to run into the problem of the Fragile Base Class (Wikipedia.). Having many small separate (distinct, isolated) inheritance hierarchies reduces the chances of running into this problem. Making all of your objects part of a single humongous inheritance hierarchy practically guarantees ...


21

This syntax is pretty much a legacy from C++, which, by the way, has both: Person Bob; and Person *bob = new Bob(); The first to create an object within the current scope, the second to create a pointer to a dynamic object. You definitely can have something_else Bob = new Person() IEnumerable<int> nums = new List<int>(){1,2,3,4} You are ...


20

Because: You shouldn't pay for what you don't use. These functions make less sense in a value-based type system than in a reference-based type system. Implementing any sort of virtual function introduces a virtual-table, which requires per-object space overhead that is neither necessary nor desired in many (most?) situations. Implementing toString ...


15

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 ...


14

Algorithms and OOP are two disparate terms, which have only in common, that they are CS-terms. Simply - An algorithm is like a cooking recipe: to do x you need the following ingredients and do step 1,2,3,4,5,6... then you have your meal prepared. That said, it seems a natural fit for algortihms to be described in a procedural way. Procedural means nothing ...


11

Having one root object limits what you can do and what the compiler can do, without much payoff. A common root class makes it possible to create containers-of-anything and extract what they are with a dynamic_cast, but if you need containers-of-anything then something akin to boost::any can do it without a common root class. And boost::any also supports ...


10

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 ...


9

First, lets define what we mean by OOP. By OOP I mean primarily : Encapsulation and hiding of details in class. Polymorphism of behavior through inheritance and virtual methods. Now, to answer your question: What is the relation between algorithms and OOP? Are they two independent topics? Yes. Are there some problems which only can be presented ...


9

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 ...


8

There are many good answers above, and the clear fact that anything you would do with a base-class-of-all-objects can be done better in other ways as shown by @ratchetfreak's answer and the comments on it is very important, but there is another reason, which is to avoid creating inheritance diamonds when multiple inheritance is used. If you had any ...


6

Pass an already established connection to Table's constructor (as opossed to the other answer suggestion of passing the connection string). That's the approach I've used for years. Objects that access the database don't open or close database connections. A connection is made by some other component, usually the main class, and passed to the constructors of ...


5

I'm going to suggest another reason that comes from Java. Because you can't create a base-class for everything at least not without a bunch of boiler-plate. You may be able get away with it for your own classes - but you'll probably find that you end up duplicating a lot of code. E.g. "I can't use std::vector here since it doesn't implement IObject - I'd ...


5

In fact Microsofts early C++ compilers and libraries (I know about Visual C++, 16 bit) had such a class named CObject. However you have to know that at that time "templates" were not supported by this simple C++ compiler so classes like std::vector<class T> were not possible. Instead a "vector" implementation could only handle one type of class so ...


4

you have a problem. The business domain model describes your problem, and the concepts from the problem domain you are going do be dealing with. Algorithms describe the way you are going to solve your problems, conceptually; what will your implementation look like; and how do you deal with your problem after you translated it into "Computer Science" ...


4

Every loop in Java (and most, but not all programming languages) has a loop conditional which is checked every iteration. It works just like an if statement: the conditional is a boolean expression. If it evaluates to true, the loop executes. If it evaluates to false, the loop stops and control flows to the next statement after the loop. In this code, ...


4

If you have an object oriented language, then it should be normal and expected that every value is an object; you need special justification if that is not the case. Update: it seems you're uncertain what the statement means, probably due to lack of contrast. So let me provide contrast: in other languages, even those which are considered OO languages, some ...


4

I had to tackle this recently in Java. My solution was to make the Pair class fairly painless to instantiate: Pair.of(..., ...). That's only 7 extra characters before the parentheses, and naming the factory method of seems to be a convention that value-based classes follow in Java 8. But in my use case there were usually no more than 3-4 pairs, so I also ...


3

There is a huge difference between int x and Person bob. An int is an int is an int and it must always be an int and can never be anything other than an int. Even if you don't initialize the int when you declare it (int x;), it is still an int set to the default value. When you declare Person bob, however, there's a great deal of flexibility as to what the ...


3

This work flow does seem rather simplistic and in some ways unhelpful to me: It assumes that all requirements of your application can be captured as use cases, but use cases are abstract descriptions of operations that need to be performed on stored data that (a) do not describe user interface and (b) do not describe storage mechanisms. I would, along with ...


3

There's really two things to understand: prototypal inheritance has nothing to do with performance at all. The performance issues come from runtime changes to the inheritance structure. prototypal inheritance (and flexible object structures) are not so much inherently slower, as they are harder to optimize. To illustrate the first claim, Ruby would be a ...


3

You are handling errors across the boundary between two systems, effectively, so its best to think of the http handler as a system and the internal logic that throws these internal exceptions as a library. Given you want to catch these at the boundary point, and you do not want to map internal to external exceptions, then you're left with a simple catch-all ...


3

The connecting table that you speak of is called a junction table. Using the Wikipedia example, they have 3 tables defined. User ---- User ID User Login User Password User Name Permission ---------- Permission ID Permission Description UserPermission -------------- User ID Permission ID Now, you can map these 3 tables to PHP classes. It would probably ...


3

I read in "Effective C++" that APIs should always be easy to use correctly and hard to use incorrectly. Since you've stated that it can be used incorrectly, I would ditch it for a solution that can be safer.


3

I believe that you should either expose object TextBlock from Page or have delegate methods in Page to provide access to the methods of TextBlock but not both. I guess it depends how many properties and methods of TextBlock you want to expose, so if they are many, that just provide TextBlock and do not have all those simple delegations. You can also check ...


3

what is a good guideline to ensure that it is in fact testing the proper test case This falls back to the old "who tests the tests?" line of thinking. And in general, nobody can test the tests. Unit tests have proven themselves so successful in part because they are small enough that they can be implemented without error. A good guideline is that ...



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