Tag Info

Hot answers tagged

21

I'm seeing a lot of instantiable classes in the C++ and Java world that don't have any state. Some possibile reasons to create classes without ivars of their own: State is or could be contained in a superclass. Class implements some interface and needs to be instantiable so that instances can be passed to other objects. Class is intended to be ...


19

The case for any change of practice is made by identifying the pain points created by the existing design. Specifically, you need to identify what is harder than it should be because of the existing design, what is fragile, what is breaking now, what behaviors can't be implemented in a simple manner as a direct (or even somewhat indirect) result of the ...


15

No, it is not right that an "object" is always an instance of a class. Just for example, the standard for C (which doesn't have classes at all) defines an object as (ยง3.14/1): "region of data storage in the execution environment, the contents of which can represent values." Now, it is true that using "object" to refer to an instance of a class is quite ...


12

I have "inherited" a lot of legacy code using your first variant, and also written lots of code by myself using Point2D and Point3D classes instead (essentially the same what your Vector<int> is about). The first variant leads always to functions with too many parameters and too many repeated constructs where simple vector addition or scalar ...


12

It sure looks like a Mock. While often used for testing, it's also sensible in a Duck-typed language to mock other class definitions. You've got two classes which are both implementations of a common interface. This is polymorphism in action. There's not much of "standard formal" name for it because it's just OO programming. In Python, because there's ...


9

using is not a "bloat", it's absolutely necessary to free resources wrapped into IDisposable objects. using is compiled into try...finally, with a call to the Dispose method in the finally section. For example, when StreamReader and StreamWriter are used to read and write files, respectively - their Dispose method automatically closes the file. Another ...


9

First, you need to present that any measurable organisation need to adopt industry best practices. Saying that "it just works for us!" cannot be measured, neither in time or in resource as it is simply unpredictable. Software engineering is a science as much as any other fields of science, and these concepts have been studied, researched, tested, and ...


6

The Translator Pattern is what you're asking for. But I suspect what you're looking for is a framework, more than a pattern. I believe Dozer is popular in the Java world.


6

Constructors are not just "methods that are called when object is created", they are conceptually different. Constructor's purpose is to constrain what states objects can be in initially. A freshly created object is zeroed on all fields (null/0/false) and that may be an invalid state in your program. For example, a Customer object in your application needs ...


5

The term object can refer to (at least) three different independent concepts: An instance of a class. This is the case for object-oriented, strongly-typed, statically-typed languages such as Java and C#. In these languages, the "class" is the definition and "object" is a single manifestation ("instance") of that. An untyped area in memory containing ...


4

Caleb and Robert Harvey have already pointed out what utility classes are, and some legitimate reasons for having "data-less" classes. Those descriptions are spot-on, but deal with the positive aspects. I would like to just mention that the abuse of utility classes can definitely be an OO anti-pattern (see c2wiki's description). This quote sums it up ...


4

The analogy is a bad one, because it can convey the essence of classes and objects if you apply the right mental mapping, but that mapping is not obvious to arrive at, and perhaps not even the most convincing one. A class is like a cookie-cutter in that it has the power to create multiple items that are alike in some ways: they all have the same number and ...


4

fooObj is an object and it is also an instance of the foo prototype. Though javascript doesn't technically have classes, many would also call fooObj an object of the foo class because it works somewhat similarly to classes in other languages.


3

To give a more general answer, variables should be grouped into an object when it makes logical sense to do so. Are the variables different aspects or properties of one "thing"? Do they always belong together? Will they be used enough to justify any extra work that may be involved in creating an object? In your case, I would say that your code clearly ...


3

Yes, the prototype property on a given object refers to the parent object, which allows the given object to inherit all its properties and methods. However, the definition of parent probably should be defined here. In a prototype-based language, that means that a single object can be the parent of many other objects. A side effect of this is that a property ...


3

try/finally and using are almost totally orthogonal concerns to memory leaks. You should use using or try/finally when you have non-memory resources, like a lock, a handle, or a connection, and you need to make sure those are cleaned up. There are very few or no common cases in .NET where you need to pay explicit attention to avoiding memory leaks. The ...


3

According to Martin Fowler, fake is pretty accurate: Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example). http://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs


2

The best solution would be to make several configuration interfaces and implement them as you wish. This both limits accessibility and keeps things localized. However, it's far too much effort to be worth it over simply chucking all the config in a single class and moving on to a problem with a lot more gravitas. This is the configuration, not the ...


2

An object instance would be something which corresponds to that object in the same way that a class instance corresponds to a class. I am not aware of any mainstream concepts that match that definition... Also, while it's true that a class instance is always an object, objects are not always class instances : some languages support the creation of objects ...


2

Nested levels of try catch must be avoided if all they do is nothing but log the exceptions. If object supports IDisposable then it must be wrapped in using block. Exceptions must be caught mostly on root entry points, instead catching and rethrowing at various levels. It just makes code little more complex to analyze stack traces. Try-finally must be used ...


2

You could always ask Uncle Bob himself. The thing is, it's so obvious to people with any sense that once it's spelled out, it doesn't need to be re-referenced by a multitude of authors. There need only be one source. Other terms you might want to start with when looking for sources are: SOLID Law of Demeter Big Ball of Mud All of these express related ...


2

A lot of those classes do interact with data, even if they don't explicitly contain it, and need to do so in ways that require multiple simultaneous instances to be active. E.g. a stateless session bean that interacts with a database stored procedure, passing incoming data to some PL/SQL package and passing the results back to the calling application. That ...


2

Utility classes in Java and C++ are used to maintain a library of methods that take one or more input parameters, and return a result. They have no need to hold state, if they simply return a value. In that respect, these methods can be regarded as a crude form of functional programming, with all the advantages thereof (no problems with concurrency, for ...


1

It's only a shim for the standard Object.create function which isn't available in some browsers (yes, you guessed which one). Note that it's not complete : it doesn't do everything that is done by Object.create but it's probably enough for the author. As for why the author wanted to use Object.create instead of a explicit prototype based OOP, it's probably ...


1

Actually, I think the best way is to do this: Vector<int> offset(10, 40); element.moveBy(offset); This code abstracts away the dimensionality of your vectors, and the same line of code (element.moveBy(offset)) will work unchanged if you change your vectors to 3D, or doubles, or even polar coordinates, as long as your types implement the required ...


1

You may want to see guru of the week #84. It's all about god objects (monoliths) and how they are bad. Extract: (Major) It isolates potentially independent functionality inside a class [...] (Minor) It can discourage extending the class with new functionality [...]


1

The reason is that the Java Language Specification says so. It could probably have been written to allow what you describe, but not for free: enforcing the order makes things simpler. Think about inheritance and calls to super(). If it wouldn't be the first statement in a constructor, then the subclass could do something with a superclass which is not yet ...


1

Every exception gets handled. You just get to choose if it kills your program or not, and if not, what you want to do after that. I regularly write code that throws exceptions all up and down the stack, catches the ones that it wants to recover from, and has a simple main method: public static int main(string args[]) { try { ...


1

To address the part of your question about what exceptions you should catch, have a look at this article: How to Design Exception Hierarchies Despite the title, it's very useful in understanding the different types of exceptions. There are three types, and they should be handled in three different ways: Usage error. This is due to an error in coding - ...


1

using is only allowed by the language for situations where you should almost certainly be doing it - that is, classes which implement IDisposable. You're free to write code that implements IDisposable needlessly if you really want to, but almost all libraries will only do it for a reason - because the object needs to be cleaned up. You can write the dispose ...



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