Tag Info

Hot answers tagged

24

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


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

I was then asked how many strings this program would generate, assuming garbage collection does not happen. My thoughts for n=3 was (7) Strings 1 ("") and 2 ("a") are the constants in the program, these are not created as part of things but are 'interned' because they are constants the compiler knows about. Read more about this at String interning on ...


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


14

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


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


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


11

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.


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

On each iteration, a new String is created by the + operator and assigned to s. After return, all of them but the last are garbage-collected. String constants like "" and "a" are not created every time, these are interned strings. Since strings are immutable, they can freely be shared; this happens to string constants. To efficiently concatenate strings, ...


9

The question asks "which process determines which method should execute?" This is a bad question. But, we can immediately eliminate three of the choices: Is-A, Has-A, and Parent Class, since those are object-oriented, but not certainly not processes. Even if Is-A and Has-A were processes, they would be processes regarding class and composition, as you ...


7

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


7

An object, often called an instance, is a specific instantiation of a class. If you instantiate (make an instance of) a class ten times, you get ten objects, but there's still just the one class.


6

We have to decouple two concepts here. As of Java 7, the language does have RAII. It's called the try-with-resources statement. static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } } This guarantees that the reader will be closed no ...


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


5

Doing this is the best (and verbose) option right now. return {'x': x, 'y': y} The caller would have to do this to use the values var c = get_coords(); alert(c.x + ' ' + c.y); // c.x and c.y holds the coordinates If you care to use only single axis you could do - alert(get_coords().x); With the ECMAScript 6 you would be able to do ...


5

None of the above. There is no universal term, it depends on the language and the community, but the correct answer is one of message dispatch (virtual) method dispatch (virtual) method resolution (virtual) method lookup vtable lookup or a similar term.


5

I have often pondered the same question. I was a C++ programmer for many years and an Objective C programmer for many years before that. That experience taught me the discipline to track every object creation and achieve the nirvana of code - to logically assure the ultimate destruction of every created object and buffer. And what a discipline it was - I ...


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.


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


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


3

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


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

Going off of here, which lists the design goals of the Java programming language (emphasis mine): 1.2.2 Robust and Secure The Java programming language is designed for creating highly reliable software. It provides extensive compile-time checking, followed by a second level of run-time checking. Language features guide programmers towards reliable ...


3

The GC attempts to emulate a world where we never run out of memory, and that's where the advantages in terms of speed and maintainability come from. In theory, the garbage collector should know more about whether now is a good time to destroy objects than you will while programming it. Often, programs in languages with less abstraction free memory as soon ...


3

Disclaimer - I don't want to discuss if a cyclic reference is good or bad - IMHO there are situations where this kind of design is 100% appropriate, and for the sake of not starting a holy war, let's assume this is such a case here. Your 3 alternatives don't differ much - at least, not in languages like C# or Java. What you get is two objects a and b, each ...


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



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