Hot answers tagged

203 votes
Accepted

Should we design our code from the beginning to enable unit testing?

Reluctance to modify code for the sake of testing shows that a developer hasn't understood the role of tests, and by implication, their own role in the organization. The software business revolves ...
Kilian Foth's user avatar
116 votes

Should I check if something exists in the db and fail fast or wait for db exception

Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this ...
Kilian Foth's user avatar
79 votes
Accepted

How to design a REST API that can "prompt" the client about long-running operations?

For long-running operations, it often helps to model the active job as a REST resource with its own structure and/or sub-resources. For example, starting a job may return a result such as 202 Accepted ...
Hans-Martin Mosner's user avatar
75 votes

Should we design our code from the beginning to enable unit testing?

It's not as simple as you might think. Let's break it down. Writing unit tests is definitely a good thing. BUT! Any change to your code can introduce a bug. So changing the code without a good ...
Ewan's user avatar
  • 71k
59 votes

Is it ok to have validation layer before access control layer

It depends on whether knowing the validity of some input for a task that you aren't permitted to do is a security leak. If it is, you really should to do it the other way round. The only safe ...
Caleth's user avatar
  • 10.5k
39 votes

Should I check if something exists in the db and fail fast or wait for db exception

I think what you call “fail fast” and what I call it is not the same. Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly ...
gnasher729's user avatar
  • 42.7k
36 votes
Accepted

Use empty string, null or remove empty property in API request/response

TLDR; Remove null properties The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if programming in that paradigm). The JSON that you receive is ...
Kasey Speakman's user avatar
26 votes

Why PATCH method is not idempotent?

PATCH requests describe a set of operations to be applied to a resource, if you apply the same set of operations twice to the same resource, the result may not be the same. This is because defining ...
Jbm's user avatar
  • 427
25 votes

Is it ok to have validation layer before access control layer

Well, there are multiple types of validation: Cheap basic sanity-checking, which verifies that the request is not obviously malformed. This is typically at least partially duplicated client-side, to ...
Deduplicator's user avatar
  • 8,601
23 votes
Accepted

Isn't CQRS overengineering?

Is CQRS a relatively complicated and costly pattern ? Yes. Is it over-engineering ? Absolutely not. In the original article where Martin Fowler talks about CQRS you can see a lot of warnings about not ...
Machado's user avatar
  • 4,090
22 votes
Accepted

Is it bad practice to use hyphens in JSON keys?

You can use anything as JSON keys, as long as it is valid UTF-8, doesn't contain zero code points, and it would be useful if you could represent the key as a string in the programming language of your ...
gnasher729's user avatar
  • 42.7k
21 votes
Accepted

Why do Microsoft's libraries depend on Newtonsoft.Json?

The most direct quote I've found is part of Scott Guthrie's announcement of the MVC 4 roadmap, back in 2012, (apparently offline but available via the Wayback Machine) which contains the following ...
Avner Shahar-Kashtan's user avatar
20 votes
Accepted

Is Open Data Protocol (odata) Being Widely Embraced by the Development Community?

Given that: The two "big name" implementors, Netflix and Ebay, abandoned it a couple of years ago and excitement for OData pretty much died with their departure, OData is a RESTful way of exposing a ...
David Arno's user avatar
20 votes

Use empty string, null or remove empty property in API request/response

Going with an empty string is a definitive no. Empty string still is a value, it is just empty. No value should be indicated using a construct which represents nothing, null. From API developer's ...
Andy's user avatar
  • 10.2k
19 votes
Accepted

Is it a bad idea to pass JSON objects on the query string for an API "search" operation?

Its not a brilliant idea. The URI is not really a good place for data of unpredictable length, and although there is no 'official' maximum length, many webservers apply their own limit (IIS is 2083 ...
richzilla's user avatar
  • 1,093
18 votes

Why PATCH method is not idempotent?

I think clear answer when PATCH in not idempotent is this paragraph from RFC 5789: There are also cases where patch formats do not need to operate from a known base-point (e.g., appending text ...
Ivan's user avatar
  • 281
18 votes
Accepted

Use composition and inheritance for DTOs

As a best practice, try and make your DTOs as concise as possible. Only return what you need to return. Only use what you need to use. If that means a few extra DTOs, so be it. In your example, a ...
Jon Raynor's user avatar
  • 10.9k
18 votes

Should we design our code from the beginning to enable unit testing?

Designing code to be inherently testable is not a code smell; on the contrary, it is the sign of a good design. There are several well-known and widely-used design patterns based on this (e.g., Model-...
mmathis's user avatar
  • 5,408
17 votes

Best practice for REST API call with many parameters

I have a REST API with GETs operations which receive a (long) list of > parameters. Which is the best practice to manage this scenario? AFAIK, there is no firmly established best practice (sorry). ...
sleske's user avatar
  • 10.1k
16 votes

Why do people do REST API's instead of DBAL's?

Warning: big post, some opinions, vague 'do what works best for you' conclusion Generally, this is done as a means of implementing 'hexagonal architecture' around your database. You can have web ...
Dogs's user avatar
  • 1,176
16 votes

Should I check if something exists in the db and fail fast or wait for db exception

This started as a comment but grew too large. No, as the other answers have stated, this pattern should not be used.* When dealing with systems that use asynchronous components, there will always ...
Mr.Mindor's user avatar
  • 309
14 votes

Is it ok to have validation layer before access control layer

There must be some validation before access control. Let's say SO's API has an endpoint "edit answer", then whether the user can edit a particular answer can depend on the answer (below a certain ...
Sebastian Redl's user avatar
13 votes

Is it bad practice to use hyphens in JSON keys?

There are plenty of JSON serialization systems that are more than capable of handling mapping between field names that aren't suitable for use in the language they integrate with. In most cases, they ...
Jules's user avatar
  • 17.6k
13 votes
Accepted

How to design a REST API to handle non-CRUD operations?

You need to watch this talk by Jim Webber. When I need to update the state of the subscription, I cannot simply send a POST request to the server, because I don't have direct access to the resources, ...
VoiceOfUnreason's user avatar
13 votes

Should we design our code from the beginning to enable unit testing?

It is IMHO very simple to understand that for creating unit tests, the code to be tested must have at least certain properties. For example, if the code does not consist of individual units which can ...
Doc Brown's user avatar
  • 201k
13 votes

How to map "mv" operation to HTTP verbs?

Is there a RESTful way to implement this whilst maintaining atomicity of the operation? Short Answer Just use POST Medium Answer Seriously; it is okay to use POST. POST serves many useful purposes ...
VoiceOfUnreason's user avatar
12 votes
Accepted

Repository pattern, call another API that updates a SOR's from service or repository class?

The calls to other APIs should go in the repository layer. The service layer shouldn't care how it gets its data, no matter where that data comes from. The job of a repository isn't communicating ...
Greg Bair's user avatar
  • 345
12 votes

Should we design our code from the beginning to enable unit testing?

I take issue with the (unsubstantiated) assertion you make: to unit test the Web API service we will need to mock this factory That's not necessarily true. There are lots of ways to write tests, ...
Daniel Pryden's user avatar
10 votes
Accepted

What are the consequences of using verbs instead of nouns in REST API URI?

Standard URL mapping for REST has the resource mapped to the URL and what you do to it in the HTTP method. It works well when interacting programmatically with your REST endpoint. It's also very ...
ptyx's user avatar
  • 5,851
10 votes

Should we design our code from the beginning to enable unit testing?

You are in luck as this is a new project. I've found that Test Driven Design works very well for writing good code (which is why we do it in the first place). By figuring out up front how to invoke ...
Thorbjørn Ravn Andersen's user avatar

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