New answers tagged

0

There are a few unknowns here to be able to provide a more detalied response, so I will try to suggest a generic solution. First of all, you would have to handle different types of data that the SQL query could return. This can be done by creating a universal type holder class. Next, you could create a struct containing a two-dimensional dynamic array of ...


2

Is it better to expose or hide dependency in OOP? It's best to do both. Before I explain that let me explore the problems with your proposals. Let's say I have an object A, which is too big(having too many methods and variables). So, I break it down to smaller objects. This is good. Always break objects down until they have only one ...


0

What you said make me think why Object A is that big. Instead of thinking about dependencies and injection I'd concentrate about composition over inheritance which allows late just-in-time object creation only if needed. If you're worried about dependency exposition it could be managed by exposing interfaces instead of internal classes.


5

I agree that it is totally okay if a class has only one method, but recently I have read many blogs saying this is bad OOP and rather procedural coding. If a class has only one method, it's usually Execute(), or something equivalent. The question you have to ask yourself is, what are you encapsulating by using a class, if you only have one method? That's ...


1

The code you describe shows two objects that update each other based on an event. The risk of such a setup is that they may get into an endless loop where they continually update each other. However you seem to have addressed this issue by checking if the value is changed before setting it. Object A: value is different, set it. New event fires because of ...


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


0

When you add a new column to a database table, you will usually either give the column a default or update the current rows in the same script. You're sort of doing the same thing here, just with the extra indirection of having a serialized string instead of a table, it's a little trickier. Many SQL implementations have tools for parsing and writing XML, so ...


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

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


0

Instead of checking and invoking specific makeComboBox vs. makeNumericTextField, consider invoking a generic constructUI, which would be a virtual method on the base class FormElement that is overridden by the various subclasses, each of which does the equivalent. The idea behind OOP is to instruct the object to do something useful on its own and return to ...


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

If they came up with 20 classes/interfaces for an example solution on a trivial coding excersise I would stay within an arms length away from that company. They obviously like to bloat code and you'll have a living hell trying to manage their backlog projects. But yes; apps tends to get bloated over time and a lot of systems are complex. But who would tell ...


-1

Reality is complex. Projects, good projects, are hard. Because they must take care of many little things to be actually working. Of course, many projects in real life are not "good project": they work, but the code is more "grown by accretion" rather than designed with all the nice features that we learn. Tests are missing, logic is intertwined with ...


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


0

Sounds like a state machine to me. Create a set of events and have the end of one start the beginning of the next one. The line growing would be a state (with state of its own representing the current length), then the full length line (maybe pulsating or something) would be another state. Then, maybe an event would then trigger a "close" even where the ...


0

why not iterate through the tiers? that should solve your problem without any overhead: protected function getPath() { $classname = get_class($this); do { $path = BASEPATH. '/'. $classname .'/'; if (file_exists($path)) { return $path; } $classname = get_parent_class($classname); } while ($classname); return NULL; } or, in your code, ...


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


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


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


0

This is a Java-specific design. For example in D, sort is an instance method on arrays, not a static method as in Java. But in Java arrays are special. They are defined as objects, but they are not instances of classes. The only instance methods they have are the ones inherited directly from Object. There are no Array-specific instance methods since there ...


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


0

It is a bad design pattern. Why a function called sum(int x, int y) should take a third argument and became sum(ErrorAccumulator ea, int x, int y)? It's very counterintuitive. Moreover suppose that there is a function foo() that does not have such parameter and does not log errors, than the need to log errors in that function arises: I have to modify that ...


-1

To me this feels like bad OO design because you are modifying the state of an object inside of a function as a side effect. Why is that bad? The term "side effect" comes from functional programming, which is afraid of state because tries to pretend that programming is a math problem, and math problems don't have state, so anything that doesn't fit the "...


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


-1

The biggest disadvantage of a Data-table is no fixed structure and zero help from IDE. A fellow developer will never know what all columns are present in your datatable unless they apply a break point or have access to the database. Always assume someone else will have to maintain your code.


-1

From an OO perspective ellipse does extend circle, it specializes on it by adding some properties. The existing properties of circle still hold in ellipse, it just gets more complex and more specific. I do not see any issues with behavior in this case like Cormac does, shapes have no behavior. The only trouble is that in a liguistic or mathematical sense it ...


0

As stated by others, private variables are good to avoid miss-usages leading the object into an inconsistent status and hard to track bugs and unforeseen exceptions. But in the other hand, what has been mostly ignored by the others is about protected fields. An extended sub-class will have full access to protected fields, making the object as fragile as if ...


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


0

Yes and No. It Depends. Scenario 1: You are the sole developer on the project or its your personal project. Practices do not matter. What matters is that it works and its of good quality. Scenario 2: There are muliple people working on this project Yes it is a bad practice. Only required fields must be part of constructor parameters. Either use ...


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


0

Having optional parameter constructors eliminates the need for multiple constructor overloads. In Java, there's an entire software pattern for this called the Builder Pattern, which essentially replaces multiple constructors with a fluent interface. It's an elaborate, overly complex and ultimately unsatisfying pattern; optional constructor parameters is a ...


0

Change the scope of the field. If the name matters and that behavior should be tested, then make that field public. In this case, make it public read only. Then one will be able to test the state of the object after the constructor is fired.


0

Trusting the dependent class that it works properly, hoping there would fail some unit tests when something there does not work, then it should this be tested very vell. Maybe there are missing some important unit tests? There can be an untested case that would create an error, that will be produced in my originary testing class, and would not be catched in ...


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


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

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.


0

You can think in terms of composable behaviours using DI. You already have all the exising pieces in hand, just tweak it a little bit: // Your FoodFactory interface public Food createFood(); // Factories, each of them returns a specific kind of food meat = new MeatFactory(); fish = new FishFactory(); poop = new PoopFactory(); // Inject the wanted factory ...


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


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


0

In general, code should be written as it is intended to be read. Put your functions in the order that makes the code easiest for another programmer to understand them. That often means that the functions that get invoked first, such as main() and other high-level functions, should be early in the source file.


0

Instantiating a class is when you create an instance of that class at runtime. Though I think what you're referring to could be class initialization? Just in case, class initialization occurs when any of the following happens: 1) an Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ...


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


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


0

If I understood your question correctly, you're looking for abstract static. This is not possible in Java, but here are some workarounds: http://stackoverflow.com/questions/1916019/java-abstract-static-workaround . As an alternative, you could use other languages with allow this, for example Scala


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


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



Top 50 recent answers are included