Tag Info

Hot answers tagged

52

If a language inherently supports exceptions, then it is preferred to throw exceptions and the clients can catch the exception if they do not want it to result in a failure. In fact, the clients of your code expect exceptions and will run into many bugs because they will not be checking the return values. There are quite a few advantages to using exceptions ...


18

Error variables are a relict from languages like C where exceptions were not available. Today, you should avoid them except you are writing a library which is potentially used from a C program (or a similar language without exception handling). Of course, if you have a type of error which could be better classified as "warning" (= your library can deliver a ...


14

This sounds suspiciously like an inner platform. Here are the potential problems: You'll be writing queries that query the database for what information to query (metaqueries) first, rather than queries that simply retrieve the needed information. You'll be subverting the role of the database, which already provides metadata capabilities such as rows, ...


11

There are multiple ways to signal an error: an error variable to check: C, Go, ... an exception: Java, C#, ... a "condition" handler: Lisp (only ?), ... a polymorphic return: Haskell, ML, Rust, ... The problem of the error variable is that it is easy to forget to check. The problem of exceptions is that is creates hidden paths of executions and although ...


11

I think it's awful. I'm currently refactoring a Java app that uses return values instead of exceptions. Although you may not at all be working with Java, I think this applies nonetheless. You end up with code like this: String result = x.doActionA(); if (result != null) { throw new Exception(result); } result = x.doActionB(); if (result != null) { ...


10

Mutable state is easily avoidable using immutable objects. In the same way, global variables are usually the choice of the developer (or a poorly implemented framework). This being said, you may also want to use additional functional paradigms in non-functional languages. It's all about the expressiveness of your code. If you see that a list comprehension ...


10

A Helper class is a lesser known code smell where a coder has identified some miscellaneous, commonly used operations and attempted to make them reusable by lumping them together in an unnatural grouping. Successive developers have then come onto the project and not realised that the helper class exists, and have consequently rewritten the same common ...


9

This is an example of a facade pattern, which may provide some starting points for your research. I've also seen it called something like "dehorriblizing layer," but I can't find the exact term at the moment. Basically, you create your dream API, the one you would create if starting from scratch today, but implement it by making calls to the current ...


7

I'm going to start flat out by saying after more than 30 years experience writing software and working in I.T in general, I have NEVER, EVER yet found a good reason for concatenating data coming out of a database. You could be putting 90% of your application code in the DB infrastructure, and doing all sorts of magic things with it, and I still would not be ...


6

It is really the flyweight pattern which is a specialized sort of object pool, where objects get shared to save memory.


6

Looks like the purpose of the model is missing from the picture. DDD is not about modelling reality: it's about discovering the model which is the best fit for the specific purpose. Design patterns will eventually follow.


5

There is often nothing wrong with using this pattern or that pattern, as long as you use the pattern that everyone else uses. In Objective-C development, the much preferred pattern is to pass a pointer where the method that is called can deposit an NSError object. Exceptions are reserved for programming errors and lead to a crash (unless you have Java or ...


5

If you are working on a script where you know (or suspect) that it will later be used as a library in a larger project, then it is easier to start out with writing the functionality in a library and then tagging on a driver script to use the library from the command line. There are no specific design patterns to help in creating good API's. It requires ...


4

Below is an example using an interface and two implementations in a console application: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var processes = new List<IProcessable>(); processes.Add(new Process1()); ...


4

There are use cases were error codes are preferable to exceptions. If your code can continue despite the error, but it needs reporting, then an exception is a poor choice because exceptions terminate the flow. For example, if you're reading in a data file and discover it contains some non-terminal piece of bad data it might be better to read in the rest of ...


4

The question is already answered, but I can't help myself. You can't really expect Exception to provide a solution for all use cases. Hammer anyone? There are cases where Exceptions are not the end all and be all, for example, if a method receives a request and is responsible for validating all the fields passed, and not only the first one then you have to ...


4

"In order to handle several possible errors happening, that shouldn't halt execution," If you mean that the errors should not halt execution of the current function, but should be reported to the caller in some way - then you have a few options that have not really been mentioned. This case is really more a warning than an error. Throwing/Returning is not ...


4

"I'm trying to do X, which design pattern should I use?" is usually the wrong question to ask. A design pattern is a proven solution to a recurring problem, but its value as a pattern lies largely in its name: once you've solved similar requirements in similar ways several times, you can more easily remember the combination of classes and relationships ...


3

Fragment is a modular section of an Activity that has it's own lifecycle, receives its own input events, which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities) Apart from the obvious advantage of using fragments, UI optimization across different screens, it lets you manage ...


3

You can't really decide on a specific design pattern at this stage. Your description is too high level. To be able to get to deciding on a design pattern you should first construct a doman model which shows the main entities in your system, and associated cardinalities. From reading your description there are three main entities that jump out : User, ...


3

Bags and suitcases are both types of containers. You can also put bags inside suitcases, and vice versa. Containment doesn't care about inheritance, and inheritance doesn't care about containment. The two concepts are entirely orthogonal, and as such there is no 'best practice' here.


2

As usual, there is no definite answer for how you must model something. Everything that is valid according to whatever modeling language you apply is available and can potentially be used that way. That being said, intermediate states are nothing out of the ordinary. I even have to correct the comment made by @rwong, that a state remains unchanged in the ...


2

Why do people use it? I have seen quite a number of singletons in the PHP world. I don't remember any use case where I found the pattern to be justified. But I think I got an idea about the motivation why people did use it. Single instance. "Use a single instance of class C throughout the application." This is a reasonable requirement e.g. for the ...


2

Every pattern attempts to solve one or more challenges in software design, and has a set of disadvantages. You will need to study the patterns that you can utilize in your situation and determine what suits your needs the best. The link you shared in your question is by Mark Seeman whose book that he references in the article is primarily focused on ...


2

If something is tedious, it often can be automated - that is true for test data generation as well as for many other tasks. Instead of constructing each test case manually, write a test case generator, which takes some simple textual description of the tree (maybe some kind of "mini DSL") and constructs it. Write a second function which takes the ...


2

Entity component systems (ECSs) can be programmed in an OOP or functional manner depending on how the system is defined. OOP way: I have worked on games where an entity was an object composed of various components. The entity has an update function which modifies the object in place by calling update on all its components in turn. This is clearly OOP in ...


2

There's definitely nothing wrong with not using exceptions when exceptions are not a good fit. When the code execution should not be interrupted (e.g. acting on user input that may contain multiple errors, like a program to compile or a form to process), I find that collecting errors in error variables like has_errors and error_messages is indeed far more ...


2

Looks like a variation on Strategy, where the twist is you're looking up the strategy for a type in a hash keyed by the type. It's also "dynamic dispatch", which is fundamental mechanism in OO. That's where you decide which method to execute at runtime based on the type of an object. In an OO language like Java, it's usually an anti-pattern to base logic ...


2

How can I write solid functional code that does not allow side effects even in languages that have mutable state and global variables? The simple answer is, do not use global or mutable variables, or just because you can mutate them does not mean you have to. Consider a class like this: class ImmutableClass { private int myImmutableField; ...


2

I can try to answer your question on a generic level. You can create a centralized repository object which contains all currently loaded matrix objects and also records accesses to them. It loads the matrix objects on first access and stores them internally. All requests for these matrix objects must go through this repository object to record them ...



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