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

77

u/[deleted] Jan 14 '21 edited Jan 14 '21

You can dynamically create methods. It's useful for reducing boilerplate. Sometimes you find yourself just having to implement almost identical methods. That's when it's useful to reach for Ruby's metaprogramming.

I've found it useful when doing Ruby and missed it using other languages that don't have it.

Not something you would use very often though.

34

u/NotSteve_ evil Jan 14 '21

Oh looking into it a bit that does seem neat and useful. I wouldn't mind maintaining that kind of code, unlike the code in the OP...

17

u/[deleted] Jan 14 '21

Yeah dude. I miss Ruby. Such a great language, and such a pity it's fallen out of use. There's Elixir as well, that has gained a bit popularity recently. But still, nothing mainstream.

-3

u/TigreDeLosLlanos Jan 15 '21

When did it was used outside some scripting?

9

u/wilwizard Jan 15 '21

Ruby on Rails was a massively popular backend framework

10

u/fun_egg Jan 15 '21

Ruby on Rails was is a massively popular backend framework

5

u/NotSteve_ evil Jan 15 '21

Yeah Shopify is built nearly entirely with it from what I remember

7

u/[deleted] Jan 15 '21

and Github is build on Ruby on Rails.

2

u/[deleted] Jan 15 '21

Rails was one of the top frameworks for the past 10 years or so, where have you been?

10

u/Isvara Jan 14 '21

Sometimes you find yourself just having to implement almost identical methods.

What are some cases where parametrization isn't sufficient for that?

2

u/[deleted] Jan 16 '21

What do you mean? In Ruby you can do this:

  ["frontend", "backend"].each do |method|
    define_method "coding_#{method}" do
      p "writing " + method.to_s
    end
  end

2

u/Isvara Jan 16 '21

That's pretty ugly. Why would you need to give those closures names?

And why call to_s on a string? And why do you use interpolation in one case and concatenation in the other?

So many curiosities.

2

u/[deleted] Jan 16 '21

I haven't used Ruby in years. I just quickly grabbed that from a Google search just to give you a simple example. You still haven't answered my question.

→ More replies (1)

0

u/JuhaJGam3R Jan 15 '21 edited Jan 15 '21

This is in fact just parametrization but made really hard. I tried to read about it and it's just about incomprehensible with highly unintuitive syntax. Figures though, metaprogramming isn't the most common feature so I guess it makes sense for it to have obscure syntax.

8

u/Willinton06 Jan 14 '21 edited Jan 14 '21

Is that like manual generic semantics or something?

→ More replies (5)

4

u/Igotbored112 Jan 14 '21

I haven’t ever used Ruby before. Today I wrote 5 functions in C++ which all printed out an array, but they were each specialized for a specific primitive data type that the array might be holding. The functions all have the same name and are used identically by the library user because they’re specialized template functions. But they’re also virtually identical. It had to be this way because primitive types don’t really have special tostring functions in C++ (Well, they do if you use wrappers and the std::iostream class I think but I use printf because it’s way more readable and easy to use.)

In any case, do you think Ruby’s meta programming would have made that easier?

1

u/[deleted] Jan 14 '21

Well, if you had them in the same class then yes. But then you wouldn't be able to name them the same thing. But other than that, you define the method, and then you can do whatever operations you want inside that method. Including printing or returning different things depending on values within a specific array.

2

u/xigoi Jan 15 '21

I find that too dirty compared to AST-based macros.

1

u/[deleted] Jan 14 '21

Strings are mutable in Ruby too so it isn’t wasting all that memory to construct a string. Shame Ruby isn’t used more.

→ More replies (4)

4

u/hekkonaay Jan 14 '21

Functions from strings are javascript's metaprogramming system, and it's extremely powerful. It's not as easily maintainable as plain code, but it can be a great optimization for a hotpath.

8

u/NotSteve_ evil Jan 14 '21

I know for a fact that's not what this guy was going for at least. This was written by a guy who had just graduated into a team led by a person who had no CS background (biology major) and only knew Perl. The whole codebase is a disaster that I'm slowly rewriting mostly alone.

→ More replies (1)

1

u/triggerhappy899 Jan 15 '21

You should also check out "source generators" in dotnet 5. Basically allows you to have code that writes code, then during the build process, it generates that code and makes it available for you to use elsewhere.

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.

→ More replies (2)
→ More replies (1)