Tag Info

Hot answers tagged

53

In many cartoons or other media, the forces of good and evil are often illustrated by an angel and a demon sitting on the character's shoulders. In our story here, instead of good and evil, we have SOLID on one shoulder, and YAGNI (You ain't gonna need it!) sitting on the other. SOLID principles taken to the max are best suited for huge, complex, ...


8

In layman's words: Usually IDEs help you do that kind of refactoring, some even allow you to extract the interface out of an already implemented class It's almost impossible to get the design right the first time The normal workflow involves changing and rethinking the interfaces in the first stages of developement As development evolves it matures and you ...


7

Use dependency inversion where it makes sense. One extreme counterexample is the "string" class included in many languages. It represents a primitive concept, essentially an array of characters. Assuming you could change this core class, it makes no sense to use DI here because you will never need to swap out the internal state with something else. If you ...


6

It looks like your client wants to use those "secret codes" as a fine-grained authorization scheme: Only people who are authorized to access those entries should know the corresponding secret code. If that is the case, then those secret codes serve a similar purpose to passwords and should be subject to the same security standards.


5

The short answer is "almost never", but there are, in fact, a few places where the DIP doesn't make sense: Factories or builders, whose job it is to create objects. These are essentially the "leaf nodes" in a system that fully embraces IoC. At some point, something has to actually create your objects, and can't depend on anything else to do that. In many ...


4

It seems to me that the original question is missing part of the point of the DIP. The reason I am skeptical is because we are paying some price for following this principle. Say, I need to implement feature Z. After analysis, I conclude that feature Z consists of functionality A, B and C. I create a fascade class Z, that, through interfaces, uses ...


4

I had to tackle this recently in Java. My solution was to make the Pair class fairly painless to instantiate: Pair.of(..., ...). That's only 7 extra characters before the parentheses, and naming the factory method of seems to be a convention that value-based classes follow in Java 8. But in my use case there were usually no more than 3-4 pairs, so I also ...


3

In my opinion, comments should explain anything that cannot be made self-evident by the actual code, and any detailed external documentation you need should be generated from those comments (presumably as part of your automated build/deployment system). Clarifying what a class or function should be used for, what problems it was created to solve, and where ...


3

A very large reason for this is probably support: many languages and libraries are designed to support booleans or generic, dichotomous values over more-specific variants so that they have the widest applicability. For example: if supports booleans. If we wanted if to be able to support any dichotomy (ignoring equality comparisons on the values) then it'd ...


3

There is no point to making sure that every defect in your system trips exactly one test. A test suite has one job: verifying that there are no known defects. If there is a defect, it doesn't matter if one test fails or 10. If you get used to your test suite failing, if you try to gauge how "bad" your program is by counting the failing tests, you're not ...


3

Try keeping your unit test atomic. Remember that automated testing code is also part of your code base, but is itself not under test, so keep it as simple and obvious as possible. To answer your question more directly, there is no guarantee of the order of executing of the tests so there is also no way to guarantee that your utility test succeeds before ...


3

I read in "Effective C++" that APIs should always be easy to use correctly and hard to use incorrectly. Since you've stated that it can be used incorrectly, I would ditch it for a solution that can be safer.


2

It depends on your tooling, but you probably can't (and shouldn't) Some Unit-testing frameworks (take PHPUnit for example) allow you to 'chain' tests so that a failing test on one level does not execute other tests. However, I doubt that this will fix your problem for this situation. Not allowing guaranteed test order execution forces tests to be isolated ...


2

No, Chain of Responsibility doesn't make sense here, because it assumes all components have same interface. I don't think Java's type system is good enough to make this fully generic, so I would opt in to type erasure and some kind of "manager" that pipes output of one module into input of next one, while encapsulating the erasure. The module's interface ...


2

What is the advantage to returning a boolean from a function over returning something like a enumeration of named values? It let's programmers be lazy. Sometimes that is a virtue. Things like this, it is not. The main advantage is that at the call-site, using a bool is often more readable: if(foo.IsBar){ ... } Rather than: if(foo.Bar == enum.Good){ ...


2

Where we should put search action? In GET /search/:text. This will return a JSON array containing the matches, every match containing the album it belongs to. This makes sense, because the client may be interested not in the track itself, but the entire album (imagine that you are searching for a song which, you believe, was in the same album as the one ...


2

Any data type can be thought of as able to hold the answer to a question. What is two plus two? The integer four. What is pi? The floating point with a certain bit pattern. Is the sky blue? A yes/no, true/false answer. These are primitives, meaning data elements are reusable across a broad spectrum of uses and generally are simple, often fitting into a ...


2

Your class is called RVDBuilder. The point of the builder pattern is to avoid ridiculously complex constructors and instead allow you to gradually collect all necessary information via ordinary method calls. Therefore, the best solution would be to add a fluent interface to add pairs of information: class RVDBuilder { public RVDBuilder Add(string key, ...


2

Extending the ORM Context is a good approach, but when I am in system with lots of predefined queries, I like to take another approach, using Extensions methods in order to group the related queries. Let me give an example: The usage is kept simple like this: var data = Context.Financial() // This returns a FinancialContextExtensions instance with all ...


2

Your main bottle neck will always be IO unless you are doing some seriously intensive computations. Your best bet for squeezing the most performance out of your target machine to read all files serially (one at a time) and process each file concurrently (multiple calculation pipelines). Since you are using C#, I would suggest you look into using the Task ...


2

No. If people cannot read the code and understand what the code is doing, that is a sign that your code is poorly designed, overly complex, or poorly named (or some combination thereof). Comments don't fix those problems - they only exacerbate them. Use comments to describe why code is doing weird things. More conventional documentation is better for ...


1

Comments should not explain how the code works. The code is there to explain how it works. If the implementation is changed, the comment becomes incorrect. Mostly the comments should explain What the purpose of the code is. This allows you to change the implementation over time without invalidating the comment. Changing the purpose of the code is ...


1

Because the domain answer is just another boolean. LetterSent, BillPaid, AccountIsPastDue, SubscribedToAutoPay, etc... When you talk to the domain experts, that is how they think of the situation -- the main difference is that they typically think in terms of yes/no and not true/false, but creating an object for that would be silly -- basically a lot of ...


1

Sounds like you are organizing your code by what the code is and not the domain it applies to. As a developer working with the entity framework, I would expect you to have a derived DataContext class with all your general CRUD operations. Since this DataContext is your gateway to the database, it would make sense to store the queries and methods that use ...


1

You should throw as many I/O jobs as possible and let the OS does its job, use async I/O to do that or mmap(). Don't assume you know how to it better than the OS, instead you can give hint. That's why there's call like madvise(), to let the OS know if you only need the data to be read once or sequentially, etc. Yes, even with magnetic disks you want to use ...


1

Sometimes you just need to know whether an operation succeeded or not. The most straightforward example that I can think of is the TryParse metaphor in the .NET Framework. TryParse exists for a number of reasons: You can control the lifetime of the variable you're putting the parse result into, Exceptions are too expensive to be throwing if you're in the ...


1

First things first: configuration is complexity, and should be avoided unless necessary. Far too often I've seen people add configurable settings when they ain't going to need it. For example getting the settings from XML files vs GUI Whatever. You usually have some data transfer object/entity/POCO/dictionary/whatever to represent your settings. XML ...


1

Use a Property Grid. This greatly reduces the effort of standing up a settings dialog, because you can just define the setting items you want in the prototype class, and let the PropertyGrid do all the work of creating the UI. Example of a Property Grid: In C#, the PropertyGrid responds to things like class attributes that will cause the PropertyGrid to ...


1

It depends. What is the benefit of not using encryption? Under some circumstances, it can be cheaper to implement and debug. Easier for admins and others to figure out a password / secret access code. What are the drawbacks of not using encryption? Potential legal liability, depending on the context. Easier for admins and others to figure out a ...


1

Maybe it's force of habit from C coding 20+ years ago, but, no matter what teh language, I usually let my small helper functions float to the top. At the very least, I like to have every function declared before it is called, in order to obviate the need for forward declaration/function prototypes. I guess that makes me the exact opposite of @IAE (+1 to ...



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