Hot answers tagged

6

If it is the case that the name of the car isn't publicly available in any way at all, then it makes no sense to test it, because obviously it doesn't matter to any client code. If the name of the car matters, even if you can't actually see it by inspecting the field, then exercise some method that relies on the name being correct and assert against some ...


6

Are there any general-purpose programming languages with classes, objects, methods, interfaces, and so on, which disallow class-based inheritance? This reads very much like a description of VBA - Visual Basic for Applications, embedded in Microsoft Office and other VBA-enabled hosts (e.g. AutoCAD, Sage 300 ERP, etc.), or even VB6. The "A" of "Basic" stands ...


6

The notion that static methods are impossible to unit test is a myth that has proven difficult to kill. What makes a method hard to test in isolation is stuff like hidden dependencies and accessing static state. There is no difference whatsoever between a static method and an instance method, except for the fact that invocations of instance methods get a ...


5

Basically my question is ErrorAccumulator a pattern or anti-pattern? With a few exceptions, a design can be good or bad depending on context (some designs are awful no matter how you use them). A pattern may make one design really good, but can be misapplied in another design to make it worse. Context matters. In general, it is a good idea for functions ...


5

Why not just store all state in a context object? So instead of passing state around in the object itself, where it's conveniently encapsulated, you would prefer to use a global variable? Or better yet just log errors inside of the function? Now your functions have taken on a logger dependency. Gathering errors in the object does not require this, ...


5

No it is not really a decorator pattern But it is a good way to compose instead of just inheriting. A decorator is an object you can wrap around another object, that alters the behavior of the object, but the resulting object is still used as the original object would be. You can actually wrap as many decorators around the object you want. In your example ...


5

You're responsible for enhancing and maintaining the codebase. Is that the only responsibility that you have ? If it is, then the game is pretty simple. Start small. DO NOT TOUCH THIS GOD. It's a God for a reason. It is used everywhere, it is too massive and too powerful, you won't be able to test it anyway and any change could break ANY part of your ...


5

The smaller the objects the more difficult to visualise program flow is. But the problem here is not that you have objects, its that you're trying to create a program from objects in a procedural manner. Your objects should be completely self-contained black boxes to the calling code, if you can make them like that then you can start to use them without ...


4

According to the Java Language Specification s. 8.1.3, "An inner class is a nested class that is not explicitly or implicitly declared static" As this is the most authoritative document on the Java language, the answer to your question is no, it is not incorrect to use this term.


4

I'd take Robert C. Martin's advice on this one and make your code read like a newspaper article as he explains in his book Clean Code. Think of a well-written newspaper article. You read it vertically. At the top you expect a headline that will tell you what the story is about and allows you to decide whether it is something you want to read. The first ...


4

Optional parameters is not a bad practice, definitely not, but sometimes the optional parameters may be optional solely because of what the class does. You arrived at a problem which is a result of a bad design. You are trying to figure out how to ignore certain properties of a model if you don't use them. The problem is, you are mixing responsibilities of ...


3

There is nothing wrong with inheritance, and in in your case it is clearly the simplest solution. The second solution introduces complexity for no benefit. "Composition over inheritance" is just a rule-of-thumb to avoid over-reliance in inheritance - it does not say you shouldn't use inheritance when it is appropriate. Furthermore, you are not even reducing ...


3

To make it simple: Static state is bad because it is effectively global state since everyone has access to it. Wrapping it in a singleton doesn't change anything. In general it is good to avoid global state which is hard to test. Static functions that are pure functions or that only mutate their arguments are perfectly fine. If they don't do any ...


3

The first question that comes to my mind when reading your question is not how one could refactor such a beast (»1,100 files and 120,000 lines of code.«), it is more »why on earth would anybody do that?« I know the strive for the perfect design and I too love clean code. But ont the other hand, I have only 8h a working day and there are funnier things to do ...


3

It's very typical to use the domain model as the resources in the web API layer, but it's usually not the right thing to do. The domain layer has clients, including the web API. The web API has clients, including your UI. The needs and wants of domain clients are not the same as the needs and wants of web API clients. Write your web API for your clients. If ...


2

I think the worst part of the interface is the fact that there is temporal coupling. Clients of the interface are expected to call methods in the right order, and this order isn't enforced by the type system, so it can only be infered by trial-and-error or by pre-existing domain knowledge. My initial thought is to keep a clean separate interface for the ...


2

Virtual inheritance Virtual inheritance is a technique to solve the problem of the diamond of death that can arise when via multiple inheritance you can inherit the same base class several times. For example, suppose you have a class Person, and two derived classes of it: Student and Employee. Now you can have a class StudentWorker that inherits from ...


2

According to the Open/Close Principe (OCP): Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Let's examine your static factory architecture under this perspective: Imagine that we want to extend our design horizontally with a subclass ProductThree. How could the makeProduct() ...


2

The reason you don't see static factory methods listed in the GoF book is because this pattern doesn't use polymorphism in any interesting way. Your diagram suggests this, but most languages do not support the structure it shows. Specifically, a static method cannot also be virtual. There is no instance object to dispatch on. It is not possible to override a ...


1

the classic way is to use a Factory class with your switch Factory.Create(data) { switch data.class case : combo return new Combo(data) .... } this can be made more generic using reflection until you end up with a dependency injection container container.Register<FormElement,Combo>().Named("combo") container.Resolve<...


1

With a little guidance from @RobertHarvey I found a StackOverflow answer about Presentation Models. A presentation model is a utility class that is used to render data on a screen or report. Presentation models are typically used to model complex data structures that are composed from data from multiple DTO’s. Presentation models often represent a ...


1

If the language permits it, I would place it on the object it operates on. For example, you asks an Integer to convert itself into a Bound, so the conversion function takes place in the integer object. The same goes for Bound to Integer but this time, on the Bound object.


1

Is returning a private pointer is a bad idea ? By making a member private, you express that this is the class internal business, and that you want to ave the freedom to manage such interals as you want. The problem when returning a private pointer, you give your control away, breaking your own design intention: What happens if the caller deletes the ...


1

This is a matter of convention and not an absolute rule, but in my programming experience, helper functions should be at the top of the file, because they will be used later and so they are a prerequisite to understand the body. If a programmer wants to skip directly at a function that uses a helper, but didn't read the body of your helper, he will also be ...


1

Validation is a pretty tricky thing to handle but I've seen most frameworks handle it in a similar way. In the frameworks I've worked with, data validation works by annotating your model classes. This 'technique' of validation is good because it follows DRY or "Don't Repeat Yourself". The best form of suggestion is through example: .NET Data validation ...


1

That's a very confusing specification. "The composition of the item class is a shield,a weapon, an engine, and a ship" : what does it means? An item is composed by a shield, a weapon, an engine and a ship or an item can be a shield, a weapon, an engine and a ship? A ship is an item (that's counterintuitive but it could be, explain how)? Moreover the most ...


1

Yes and no. The important thing is that the 'decorator' needs to act as much as possible as the original, except for where you want it to differ. In typed languages, this is often done by making both Book and MyBookDecorator implementing the same interface (commenly called IBook. All the methods the interface declares are then implemented as follows. ...


1

As a general, largely language agnostic approach, any time you're concerned that the behavior of a third party library might change, the providing of that behavior (be it your own code in the future, or another third party library) might change, or you simply want to decouple the third party library, it's perfectly reasonable to build a more abstracted ...


1

Rather than think of this as a state of the system I'm inclined to think of it as a few capabilities you either do or don't have access to as a player. Some basic abilities: Copy objects (prototype pattern) Move objects (could be as simple as select object, move player & object, release object) Save objects (as fancy as serialization or as simple as ...


1

Many years ago I read about this topic in a JavaScript book, I cannot remember which one it is. But I can still remember the discussion about whether JavaScript should be considered as an OOP language, the author's answer is yes, coz it meets the four criteria for a language to be object oriented: Encapsulation, which allows you to combine data and ...



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