r/badcode • evil • Jan 14 '21

js Dynamically created functions are the future!

Post image
795 Upvotes

102 comments sorted by

View all comments

Show parent comments

60

u/NotSteve_ evil Jan 14 '21

Haha seriously? What possible use could writing functions in strings have

1

u/cajuncoding Jan 15 '21

In many enterprise platforms that offer developers capability to integrate code by uploading plugins via assemblies, etc its common to see them wrap the plug-in up dynamically like this and then compile it for performance....but Roslyn for C# has made it much easier and more elegant.

In my own experience we needed a dynamic rules engine that could be used with C# syntax where Roslyn compiled expressions from strings was awesome.

In other languages like XSLT where you can extend it with powerful C# functions right there in the source of the XSLT they are using capabilities such as dynamic compilation to enable this....

Sure it’s rare for the average developer to need this at all, and should really question it if they think they do....but there are some very valid use cases for having this capability, so much so that Roslyn was developed ;-) ...

And as someone else mentioned Microsoft is now moving into source generators which will be powerful tools that many underlying frameworks will take advantage of.

1

u/MustachioedMan Jan 15 '21

Roslyn sounds really neat. I implemented a rules engine a while ago in c# using expression trees and it was a real pain

1

u/cajuncoding Jan 16 '21

Yeah with Roslyn it was really a breeze (comparatively speaking as dynamic rules engines go)...here’s an article that helps juxtapose the two approaches

https://www.strathweb.com/2018/01/easy-way-to-create-a-c-lambda-expression-from-a-string-with-roslyn/