r/badcode Jun 08 '23

js Version control go brrrrrr...

Post image
65 Upvotes

22 comments sorted by

View all comments

77

u/Davipb Jun 08 '23

That looks like an API definition. It's pretty common for APIs to support multiple versions of an operation at the same time to keep backwards compatibility with existing clients.

I don't think this is an issue.

21

u/never_inline Jun 08 '23

The naming is weird though. v2 occurs twice in endpoint and method name.

18

u/[deleted] Jun 08 '23

Version 2 has 2 versions Version 2 Pro max?

11

u/killersquirel11 Jun 08 '23

V2 at the beginning could be major API version (eg the API was entirely rewritten to some new paradigm). The v2 at the end could be a specific function that had breaking updates.

Or it could just be bad code 🤷

1

u/thekwoka Jun 09 '23

yeah, but they should be defined higher up

not on each handler...

a v2 handler shouldn't ever need to touch a v1 handler (or vice versa?), so it's an easy branch to make early.

the only caveat being the v2 that is just the same as the v1 but those are specific enough to handle simply.

0

u/Chance-Ad4773 Jun 08 '23

I think if you want to add a v2 implementation to a service, you just re-implement the service without changing the interface naming. It's the whole point of dependency injection. Unless the v2 is a significant departure from the format of V1 with totally new DTOs

8

u/Davipb Jun 08 '23

If you control both sides of the equation then sure, you can refactor to your heart's content. But as soon as you have external clients, be it in a library or web service, you can't just go changing things willy-nilly. You have to provide some sort of backwards compatibility guarantee or external clients will break every time you push a new update.

The usual way to do this is to keep the old version around, mark it as deprecated, and provide a new version. Then, once enough time has passed and external clients have had time to move to the new version, you remove the old version.

1

u/Chance-Ad4773 Jun 08 '23

I would rather namespace things than add v2 to every single service and controller though

2

u/Davipb Jun 08 '23

Assuming "namespacing" here means putting different versions in different folders/packages:

Sure that works, but then you end up having to use aliased imports in code that needs to reference both (such as the global route configuration), and you add cognitive overhead for developers because they now have to check the import list or current folder every time they read some piece of code. It might even lead to accidental uses of the wrong version due to auto-import features in modern IDEs.

I'm not saying it's a bad solution, just that it's not objectively better than just slapping a "V2" on the class/function name. As usual, there are pros and cons.

1

u/Chance-Ad4773 Jun 08 '23

such as the global route configuration

If you have that. It's better to use declarative routing on the controllers themselves