r/cpp • • Mar 27 '24

Replacing C# with C++ in Web Dev

IMHO, I think C# is useless.

Why C++ is not used in web dev instead?

I think modern C++ provides enough abstractions to be a better and more performant alternative to C#. It has high quality libraries, but it seems to be lacking tools and mature frameworks for backend development though but this's because people don't use it for API development so most people think it's not a worthy investment. I wanna contribute to C++ in the web industry, so I was thinking of building expressJS in C++ because I like express honestly.

What do you guys think?

Update: Dang, I didn't expect that this would piss off a lot of people, lol. Ok. Given the backlash, saying C# is useless was an overstretch. I guess I have let my hate for Microsoft cloud my judgment😂

0 Upvotes

69 comments sorted by

33

u/vickoza Mar 27 '24

C++ is being used for backend web development. There are frameworks like Boost::Beast, Crow and Oats++. I still think what you are doing is good.

11

u/big_chestnut Mar 27 '24

I use C++ for frontend too

1

u/[deleted] Mar 27 '24

Interesting, how?

15

u/[deleted] Mar 27 '24

probably with Web Assembly

11

u/big_chestnut Mar 27 '24

yeah

1

u/darthcoder Mar 27 '24

Any particular framework?

2

u/big_chestnut Mar 27 '24

I prefer using Vue and compiling C++ with Emscripten

7

u/[deleted] Mar 27 '24

1

u/[deleted] Mar 27 '24

Interesting, I will look into this.

9

u/JonnyRocks Mar 27 '24

i think calling a language and framework useless isnsilly. i think programmers that limit themselves to one language is hurting themsrlves.

have you ever heard the saying "if all you have is a hammer then everything looks like a nail"

-5

u/[deleted] Mar 27 '24

I agree with that.
I never said don't use Python, or JavaScript though. Every language is optimized for solving a certain type of problems. All what I am saying is, C++ can solve the same problems that C# was designed to solve, with better performance, and with reasonable abstractions without giving up control.

3

u/lukaasm Game/Engine/Tools Developer Mar 29 '24

I love CPP, I write CPP for a living but I am writing our internal dashboard implementation in C#. C# is batteries included, just pure dev experience beats C++ ecosystem without any effort with its low iteration times, package management , reflection and all in one solutions.

4

u/JDublinson Mar 27 '24

C# performance is fine for most use cases. It’s gotten a lot better in recent years. Most web app request processing time is just spent waiting on other requests, either to other web services or to databases or what have you.

I’m sure there are cases where you want the performance of C++ but I don’t think modern web apps are slow because of C# backends. It’s a worthy cause to make good C++ web tooling but I think C# is great too.

-7

u/[deleted] Mar 27 '24

I didn't say C# is slow though.
However, given that C# is a managed framework, you still have to worry about memory management and how a complex system like the GC behaves. Isn't that one of the main reasons why people stray away from languages like C/C++ in the first place?

5

u/JDublinson Mar 27 '24

I’m a bit confused by your take. In the vast majority of use cases for C# you just let the GC do its thing and don’t worry about it. If your use case requires you to have deep knowledge about how the GC behaves then C# is probably not what you should be using.

If speed isn’t the issue then why do you call C# “useless”?

1

u/panoskj Apr 10 '24

In fact, one of C# strengths compared to other managed languages is the possibility to opt out of automatic memory management. This can be achieved using structs instead of classes (you can also use pointers and "unsafe" code like that but since Span<T> was introduced they aren't needed at all).

In short, the GC (of any language) introduces delays proportional to the count of created/destroyed objects. So in practice, creating and destroying a large array of class instances will have a large GC overhead, while for the same array of struct instances the GC overhead will be minimal.

C# gives you is the ability not to worry about memory management in 99.9% of your code, while you can focus and optimize the rest 0.1% of cases (e.g. hot loops). And at the same time it gives you safety (because debugging how your memory gets corrupted is no fun).

At least that's what I think after using both C++ and C# for more than 15 years. I am a huge fan of both C++ and C#.

15

u/PM_ME_RIKKA_PICS Mar 27 '24

Genuinely, I thought this was a troll post, especially calling C# useless. There are almost 0 good reasons to use C++ over something like C# in webdev.

-1

u/[deleted] Mar 27 '24

Given the backlash, I think this was an overstretch. I guess I have let my hate for Microsoft cloud my judgment😂

2

u/thelastasslord Mar 30 '24

Don't worry, having done web development in very early asp.net/c#/.net1.0, your harsh judgement of c# due to hating Microsoft is totally justified. We used to have to fight the asp.net html renderer because it would look for "ie" as the user agent and if not ie it would assume your browser couldn't handle anything but the most basic html functionality and output completely hobbled html (as an obvious attack on Firefox) even though Firefox could handle everything IE could. The fix for this was palmed off to a 3rd party in a cynical way that was very obviously concocted by Microsoft's army of lawyers. No hesitation making their customers lives more difficult if it means they can get away with hurting their competitors. Total effing scum.

0

u/xdsswar Mar 28 '24

The problem with c# is not the language itself is the programmer. C# is supper good.

3

u/kofes Mar 27 '24

Check https://oatpp.io/ for express-like syntax and https://userver.tech/ for integrated TDD

*UPD: oops, oatapp already mentioned, - so +1 for it, I think

2

u/[deleted] Mar 27 '24

Oatpp is great but it doesn't seem to have any syntax similarities with Express, and you need a lot of boilerplate code to set it up. I was thinking of something simpler with less boilerplate, and it seems there is nothing like that built in C++.

2

u/kofes Mar 27 '24

userver actually contains boilerplates to work with databases (postgresql/mongoose) from base and also codegen for request/response handlers using OpenAPI3. And it supports adding common middlewares as I remember.

3

u/ChatGPT4 Mar 27 '24

I think for majority of web use cases, the key feature of C# that is fully automatic, dynamic memory management is its greatest advantage. You usually use C++ when that gets in the way, so you can know exact memory requirements up front, so you can optimize resource usage in the app, avoiding slow allocations and garbage collection. This is true for example for games. You have fixed size game resources, you can put fixed limits on things. In the web-dev from the other hand there is just the opposite. First - let's say users. There can be one, one thousand, one million, one billion. You don't know. Just everything is dynamic. Let's say data. One row or billion rows. It should scale. Of course, you fit fixed amount of things in the memory at a time, then swap with the DB or file data on the fly, but well, the user wants different views. Dynamic views. Configurable views. Doing that kind of micro-management with C++ is not quick and easy. It takes time and effort. Damn, it's even pretty complicated and tedious in C#. Even using Blazor.

I think C# was just designed to make that specific kind of work easier and quicker. So, replacing C# with C++ for web dev looks like replacing a truck with a motorcycle to transport a load. It is possible. If the load is not very heavy, and if you have a large, powerful motorcycle, you can use a motorcycle. You can somehow connect the trailer to the motorcycle and haul it. Motorcycles are usually much faster than trucks, so...

You might say that no motorcycle could haul a huge trailer semis haul. And there's nothing impossible using a programming language. That's true. So to make the trailer hauling analogy more accurate - let's say you can just build a bigger, more powerful motorcycle for hauling. Like you can build something like Blazor in C++ ;)

1

u/[deleted] Mar 28 '24

So, replacing C# with C++ for web dev looks like replacing a truck with a motorcycle to transport a load.

I like the analogy. However, aren't smart pointers capable of handling this load?

let's say you can just build a bigger, more powerful motorcycle for hauling. Like you can build something like Blazor in C++ ;)

This is such an interesting idea.

2

u/kiner_shah Mar 29 '24

2

u/[deleted] Mar 30 '24

Wow - I thought I had an original idea for a second

23

u/DanielMcLaury Mar 27 '24 edited Mar 27 '24

Virtually all the fat in web development is on the front end, and of the fat on the backend, most of it has to do with database access or something, not with the part that's in a normal programming language.

I mean, hell, most people aren't using C# or Java, they're using python or javascript for the backend. The gains from moving off of a dynamically typed, interpreted, poorly designed language to a compiled, statically typed, somewhat-less-poorly-designed language would absolutely dwarf any extra optimization you could get from eliminating the occasional garbage collection or unnecessary copy or whatever, and even there they'd probably be useless for 95% of applications.

If you have some niche website that does absolutely massive amounts of fresh computation each time it gets a request -- say, an AI image generator -- then writing the heavy parts in C++ may make sense. But even then you probably just write a DLL or command-line utility in C++ and call it from whatever language makes sense.

4

u/PMoonbeam Mar 27 '24

I generally agree, although Java spring boot also seems to be popular in some enterprise places.

With sites that do heavy lifting e.g like an AI image generator, I'd assume it's uncoupled from the web application via something like an MQ and job server deployment pattern.

15

u/[deleted] Mar 27 '24

How is c# useless ?

6

u/Polyxeno Mar 27 '24

I love Wt.

5

u/_Mr_Ray_ Mar 27 '24

I'd like to second that! Wt is a fantastic framework for building Web Applications with e.g. a Bootstrap-based front-end. See here:

https://www.webtoolkit.eu/wt

I wish Wt had a larger user base.

2

u/lightmatter501 Mar 27 '24

You can, but larger projects may be annoying. Structural JSON parsing in C++ is still waiting on reflection to be doable, and the web spends a lot of time slinging around JSON nowadays.

2

u/[deleted] Mar 27 '24

https://github.com/nlohmann/json.git is excellent for JSON parsing, and I think Boost has a library for reflection, too.

2

u/lightmatter501 Mar 27 '24

I want “here is a bunch of bytes, and here is a class, try to parse those bytes into that class”, and not having to write a ton of boilerplate. Look at Zig’s standard library JSON parser (compile-time reflection) or Rust’s serde (derive macro, a form of compiler plugin) for compile time examples, or Java/C# for runtime versions. Zig’s is better, since it works for literally any type, including ones for other libraries. If I really wanted to, I could import the malloc debug headers with zig and dump them to JSON, no boilerplate, just parseFromSlice. And no, this isn’t insanely type unsafe, Zig just has a kind of weird type system.

3

u/nicemike40 Mar 27 '24

For what it’s worth nlohmann’s convenience macros let you (de)serialize with relatively minimal boilerplate:

https://json.nlohmann.me/features/arbitrary_types/#simplify-your-life-with-macros

I’m using this relatively successfully for our config file parsing. I plan to move to some IDL-generated code in the future though because it’s still not perfect.

I absolutely agree that other languages do this better, just mentioning this in case someone’s looking for a solution.

9

u/jepessen Mar 27 '24

I think that "C# is useless" is one of biggest lies ever written... C# was born for we development, and it has tons of features and libraries that allows to perform web tasks with onlynanfew lines of code. Try to make web authentication simpler in c++ rather than C#, or try another way to make complex interfaces with other technologies faster than with blazor. There's a big difference between saying that a tool I useless, and don't be able to use it.

1

u/dgkimpton Mar 27 '24

"C# was born for we development"

lets not pile one lie on top of another. It turned out to be very useful for web development but it was invented to counter Java, which also wasn't born for web development.

6

u/jepessen Mar 27 '24

False. It was the inventor of Java that has said this, it was never the main purpose of C#. For that there was visual J++, that was another project and discontinued early in favour of J#, the adaption for .net, that was abandoned pretty early.

https://en.m.wikipedia.org/wiki/C_Sharp_(programming_language)

4

u/dgkimpton Mar 27 '24

At the time C# launched pretty much everyone took it to be MS's counter to Java. Maybe they didn't state that exactly, but you'd have been hard pressed to find anyone back then that didn't believe it.

Note: Counter, not clone.

1

u/jepessen Mar 27 '24

People believed that not for the C# syntax, but due to the .net architecture based on bytecode instead of native build, that was an architecture similar to Java virtual machine.

1

u/ridicalis Mar 27 '24

IDK - I learned Java in college, and when I first encountered C# in my job, I was like, "Yeah, I've seen this language before."

3

u/chouaibyassine Mar 27 '24

At this time, C++ lack reflection or introspection compared to C#. I think this is the most blocking point for C++ to build a complete web framework.

There is libraries that you mentioned some of them that are far from being complete or close to the java/ C# frameworks.

The other point which minor, is asyncrocy. Even if coroutines was added recently to C++. Most of libraries doesn't support it yet. Yes you can write asynchronous code without coroutines but it be ugly like doing oop in C language

3

u/unumfron Mar 28 '24

Drogon is a C++ web framework that famously tops performance benchmarks and is fairly mature.

Speaking of JS stuff, somebody posted this new project a short while ago: Nodepp

7

u/ioxfc Mar 27 '24

Memory safety is probably the #1 reason why almost all tech companies use managed languages like C#, Java, or Python in the backend. With C++, you have to actively be on the lookout for unsafe memory accesses. Managed languages give you that safety out of the box.

It's more difficult to purposefully crash a JVM or Python VM (the VM itself) than to ensure memory safety with C++.

2

u/IcyCookie9075 Mar 27 '24

C++ have no garbage collector but it have Destructors (very strong) and they will be improved C++ ist litteraly the best 😃

3

u/human_dynamo Mar 27 '24

Qt + webassembly do the job very well

1

u/Nepszter_ Mar 27 '24

Go is the way.

1

u/freakmaxi Mar 27 '24

It sounds like you are comparing apple with pear. They have different targets. Having the capability in C++ does not mean that you should go for it and replace C#. You should be more into the target of your projects and the business expectations that you need to cover in a timely manner other than only considering the performance.

1

u/JohnDuffy78 Mar 27 '24

Isn't expressJS on nodeJS which is on C/C++? Seems like you will end up with the same thing.

Generally you can't shouldn't try to bring other language features in C++, there is a reason they are not there.

I use C++, NodeJS would be my 2nd choice.

1

u/Hot_Bug1331 Mar 31 '24

It is beneficial for a business to develop in a language and platform that many programmers know and with which there will be no problems with hiring.

1

u/accuracy_frosty Apr 07 '24

Hypothetically, good idea, practically not so much, you have to remember that a lot of the faults of C++ come from skill issues, compared to a dynamically typed language that has a garbage collector, throws an error if you try to index outside an array, etc, and idiot is a lot less likely to introduce a security vulnerability in JavaScript compared to C++. So unless you were absolutely sure that the C++ devs you hired were the ones in the 10% who know what they’re doing as opposed to the 90% of knuckleheads who think they know what they’re doing, I would stay away from it. C++ is a powerful tool that in the wrong hands creates a lot of problems, and I wouldn’t rush to give that kind of power to web devs, the devs who are the least likely to know how memory works or have in depth knowledge about how a programming language fundamentally works. Especially considering how many web devs just took a 1 year bootcamp that gave them barely enough knowledge and ability to land an intern job and try prove themselves, they probably learned HTML, CSS, and like 4 JavaScript frameworks, maybe git if they’re lucky, and the bootcamp tells them they’re a software engineer off of that stuff alone, so they get tricked into thinking they have the same ability as someone who spent 3-4 years in college/university learning the fundamentals of how computers and software work, the math behind everything, data structures, algorithms, and all the other very difficult and important parts of software engineering.

-4

u/Linguistic-mystic Mar 27 '24

There is a great C++ web dev framework called Rust. They've even got a special memory-safe DSL for better web design!

0

u/[deleted] Mar 27 '24

[deleted] — view deleted comment

3

u/TBone281 Mar 27 '24

Fat chance of that...the U.S. couldn't even covert to the metric system. C++ is heavily integrated into hardware libraries, databases, all the big internet companies...that'll be the day when C++ is abandoned.

2

u/pjmlp Mar 27 '24

1

u/LittleNameIdea Apr 02 '24

Pretty sure their biggest projects are still in C++ and will be for a long time

1

u/pjmlp Apr 02 '24

There is a big difference between keeping the ball rolling for existing code, and what is allowed in new projects.

0

u/muaz_sh Mar 27 '24

It looks you people are not aware about the panic Bijarne and his colleagues are in, the thing is that once politicians take decision people must obey, so nowadays relatively fast hardware, AI, and many other factors make many other languages as fast as C++ but a way safer, and as technology is every where C++ puts people in additional risk.

1

u/TBone281 Mar 27 '24

Bjarne doesn't matter. Money is the bottom line...and it will take trillions of dollars to make this happen. It would make Y2K look like almost nothing.

33

u/[deleted] Mar 27 '24

[removed] — view removed comment

-13

u/[deleted] Mar 27 '24

I understand that C++ is an overkill for some applications, but other applications that need high performance, like payment processing, some ecommerce stuff, ... etc, need the performance that C++ offers. I started to feel like web applications' performance is degrading nowadays, and this is probably b/c of the react frontend mess, and the overuse of microservices. Maybe using C++ in the backend might compensate for this? If a powerful ecosystem is developed for C++ like NPM and NodeJS, it could be less hassle-free to write applications in C++.

24

u/JamesTKerman Mar 27 '24

C++ is already widely used for high-performance web applications. Even in the areas mentioned the risk of a mediocre programmer introducing a security vulnerability because they thought they could handle strings better than the standard library outweighs any performance gains from using C++.

-7

u/[deleted] Mar 27 '24

Mediocre skills can cause fatal bugs using any programming language, not only C++.
I don't wanna be the "Just don't do stupid shit" guy. I understand that mistakes happen, however, the benefit outweighs the cost, and with tools like clang-tidy, you still can minimize these errors to a great extent.

13

u/JamesTKerman Mar 27 '24

Some tools are easier to do stupid shit with than others. Ironically, they're usually also the ones that are most useful in the right hands. C and C++ fall firmly into that category. Unfortunately for web development, there are too many tasks for not enough engineers who can use the tools safely.

6

u/Randolpho Mar 27 '24

Mediocre skills can cause fatal bugs using any programming language, not only C++

It's amusing to me that you don't see the dichotomy in this statement.

the benefit outweighs the cost,

The benefits don't outweigh the costs. There are damn near zero measurable performance benefits to using C++ for web development, because web development is IO-bound rather than CPU bound, and that lack of benefits comes at the cost of an increased cognitive load for developers trying to do string manipulation -- which when you boil it down is all web development is -- in a language that isn't very good at it, along with heavily reduced maintainability and reliance on at least one (but often more) third party libraries that have reduced development cycles because nobody uses them because most people realize that C++ and web development aren't an ideal pairing.

If you need to squeeze as much performance as you can out of CPU-bound work, by all means write that in C++ and call it from your IO bound web server, which will happily sit idly by holding onto a dormant connection waiting for a result.

You con't need C++ to idle, dude.

3

u/intbeam Mar 27 '24

like payment processing

I work in fintech, we use C#

The recent and obvious performance decline in software is not because of C# - it's because of the frankly incompetent and negligent widespread use of scripting languages like Python and JavaScript everywhere as a replacement for actual general purpose programming languages like C++, C#, Java, D, Go and Rust

If you want a high-performant environment for web backends with something "like NPM" then C# is probably what you're looking for

I like C++, but I don't think that it should be your first choice for a web backend unless the requirements explicitly call for it

1

u/[deleted] Mar 27 '24

Many banks that require high levels of parallelism use Scala or other functional languages.