Tag Info

New answers tagged

0

1) For the sake of clarity, I decided to go with the public call to getConfig(). 2) The config object's responsibility is to get and store data from the db. Therefore, it would be better to have a separate class do the work of bootstrapping the config tables. My code now looks something like: protected override void OnStart(string[] args) { ...


0

Don't confuse OO classes and ontological classifications. You might naively want to classify a mermaid as both human and fish, but the description of the problem suggests that its swimming behaviour results in the composition of its two parts. OO classes are a means of combining state variables and behaviours which operate on those variables, they package up ...


2

A Mermaid is neither a human nor a fish, so I would not try to shoehorn these classes into an inheritance hierarchy: Class Mermaid{ /*some data members go here */ public: void swim() { /*functionality */ } }; Implement "swim" as you like. If you want to reuse some code in all 3 implementations of swim, you could easily do this by creating a fourth ...


2

I actually just solved this issue. I wanted to store configuration data in the database while having the program consume the data. I created a configuration retrieval class (non singleton) that when ever a part of the program needed a configuration value they instantiated the class and called the GetConfigValue(ConfigPropertyEnum item) method. Behind the ...


2

Contrary to popular belief, accessor methods do not provide any great advantage over direct field accesses, while in java they do tend to clutter the code with lots of gets and ()s. (Compare with C#, which has an awesome feature called properties, which look like fields but behave like accessors, saving you from all the mindless getSuchAndSuch()s and ...


2

You're going for something akin to (but not similar) Spring's ApplicationContextAware, where you pass ApplicationContext (which is a BeanFactory) via a setter, so it later can get beans it needs. Generally it is a bad idea, if you only need to inject dependencies: It is not explicit about dependencies it uses. So, it is actually harder to mock, not ...


3

Your dependency lookup looks very similar to service locator pattern, except that often, service locator is static. The benefits of such dependency lookup when compared to ordinary dependency injection are that: You won't find constructors which take dozens of (often mandatory) arguments if they need many dependencies. This makes your code shorter and ...


1

You usually don't need to. Scala, for example, just keeps the arguments to the primary constructor in scope throughout the object's lifetime. It turns out, the vast majority of the time that's all you need, especially if you support default values. However, you still have to support those other cases that the other answers enumerated. Most languages ...


1

From a language structure point of view: Because of scope. Parameters passed in to a function are only valid and accessible within that function. Instance variables are valid and accessible in every function. Furthermore, by definition function parameters are passed in to the function from a caller. So they do not reside within the object: they reside in ...


3

Because of inheritance. In a language without inheritance, the constructor could simply map its arguments to the object's fields in a one-to-one manner. If the mapping isn't one-to-one, you can always hide the constructor and provide a static method to do the work and pass the final values to the constructor. Case in point, this is how many functional ...


0

I understand the obvious -- that it is a requirement of the language -- but I was wondering why virtually all OO languages are implemented this way. Because there is a difference between the type (or type constructor) and the constructor (or data constructor). By mixing these concepts, you're making assumptions about what the programmer wants - ...


3

There are cases where a simple one-to-one mapping is not appropriate. Automatically assigning parameters to instance variables only works sometimes, not all of the time. Perhaps validation is needed, or an exception might be thrown. Maybe a parameter must be scaled, or another object created. Consider a Java BigDecimal. Internally it has a BigInteger and an ...


1

The purpose of a constructor method is to construct an object. Your constructor parameters are scoped to the constructor method, so unless you save them in instance variables, they are lost once your constructor method goes out of scope. This is how parameters work in any method; they are local to the method, not global to the class. Your constructor ...


0

From the comments we discussed the following: it could be a side effect that is not actually directly in the function called, but in a function called by that function? So function A is called. It calls function B and function B has a concrete side effect and function A is then sad to have a side effect, because it calls function B.


6

Heating the processor and losing time by useless computations is a side effect which is generally ignored, so could be considered as not very concrete. It is not concrete according to the quoted definition of Meyer. This is why compilers are permitted to optimize useless code like /// spend some time busy waiting in a useless computation for (int i=1; ...


4

Naming things is hard. But in my experience, ambiguity like this can almost always be solved by using a slightly more verbose name in the right place. The alternative method names you suggested are indeed ugly; in this example my first choice would be to rename the parameters: public interface Component { int Read(int portID, byte[] outputBuffer); ...


2

"disgusting if-blocks" can be avoided by using polymorphism to replace conditionals, or more specifically, by applying the strategy pattern. In your example, one could have objects representing a "window open" strategy, a "download strategy", a "download window opening strategy", and so on.


0

Consider this a blend of two basic MVCs (this assumes you know the basic MVC). First, deeper MVC pair is: M = model C = controller V = view model Second, upper MVC pair is: M = view model C = collapsed / minimal / interspersed (OnSuperButtonClicked is part of it) V = view. With this separation, of course view from one triad cannot access model, nor ...


0

Consider the model being just data. It doesn't make choices, and it doesn't know how to render. A product can be a model. A list of products can be a model. A bundle containing the list of products, the cart and the information about the current user is a model. The last model from the list corresponds to the data you need to render the page, but it ...


5

Inheritance is "is-a", composition is "has-a". Is it the case that "a B is-an A", or it is the case the "a B has-an A for some reason"? It is obvious than setting window is-not-a main window, but is an external actor. So it should have an A passed in. EDIT: Since you told what A and B is, that is, windows, you should separate domain from presentation; that ...


3

I wouldn't extend java.util.Observer with another interface unless you're actually adding new member definitions to it. Merely wrapping an interface in another one just to change its name is not enough of a justification, in my opinion. Your fellow programmers will believe that ClientHandler defines additional members, when it fact it does not. And the ...


3

Throw the most specific exception that already exists. It does no harm to the caller who doesn't care and will just catch the more general common superclass, but adds useful extra flexibility to a caller that distinguishes between them. Be eager to change existing code from general → specific. It shouldn't break code, after all. Be wary of changing ...


1

Your question is opinion-based and open-ended, so I'm not offering anything undoubtedly acceptable, but certainly, good naming is hard ← emphasis. I've found many times that coming up with good names or good system of names if they can't be easily changed with the use of refactoring tools later on, once the pilot was delivered out to the wild, is more ...


-3

Pass a whole object, but, select from databse only that fields that you eally need. You will get object with three filled properties, and others properties will get defaults. Such way it will be simplier to fill other properties with data just by adding field name to query, without changing method's signature.


9

I have an object that has about 50 properties, Stop! Go - right now - and fix this. Of course you're going to run into functions that only use part of your object, when your object is doing everything under the sun. No object needs 50 independent properties. Certainly some of them can be organized into sub-objects. Those three that your function takes ...


0

It sounds like you have a good grasp of the issues involved. I don't know specifically whether Dapper makes a difference in your strategy, but in general you only want to return the data from the database that you need. Will you have to alter your query if you need another property in the future? Of course, and that's the downside of returning only what ...


2

Having the language (eg, English, French, Japanese) so far up in your namespace is a smell. If I were you I'd keep the Language identifiers in the class names only. The recommended structure for a namespace is something like <company>.<project>.<namespace>.<subNamespace>.<andSoOn> So, in your case, you might consider ...


0

Probably you are not new and have the answer by now. The links from @KeesDijk and @Aaron should provide you a comprehensive answer already. Since I also had the same question before, this answer could be useful for those who start learning Angularjs. Should I have one module that contains all of the other objects in my application? This seems the ...



Top 50 recent answers are included