Skip to main content
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
115 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
80 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
  • 77k
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
  • 11.4k
50 votes
Accepted

Purpose of async/await in web servers

As far as I can tell, every request is already being run on a thread pool (as empirically tested by logging the thread ID during each request), so making all calls use async/await within your ...
Ccm's user avatar
  • 2,084
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
  • 45.8k
37 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
27 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
  • 447
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
  • 9,071
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,120
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
  • 45.8k
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
21 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.3k
19 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
  • 11.6k
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,103
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

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,488
17 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
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.2k
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
15 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.8k
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
  • 209k
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
12 votes

Purpose of async/await in web servers

A threadpool does not have infinite threads. Each time you synchronously wait, you are holding onto a thread and doing nothing with it. If you instead await, the suspension will bubble up to the ...
Caleth's user avatar
  • 11.4k
11 votes

Is creating ViewModels in Web API a bad practice?

You’ve asked two questions here and I think things will make more sense with a deeper dive into API architecture. The first thing I will say is than an API should ALWAYS have an independent ViewModel ...
Hope's user avatar
  • 111

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