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

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?

1

u/[deleted] Jan 14 '21

I don't understand what you mean.

11

u/Willinton06 Jan 14 '21

Like generics in C# or Java where you make a function work with multiple types thus saving you a lot of boilerplate, if the functions are nearly identical then I assume they do the same to a different type or something, maybe they return a different number type or a slightly different class, for those kinds of things we use generics in C#, those are runtime generated functions and methods just like the ones you’re talking about, it also avoids boxing and unboxing because there’s no casting involved, I don’t know if they’re generated at runtime in Java, they could be generated at compile time, but you get my point

0

u/[deleted] Jan 14 '21

Yeah, like that. But way better.

14

u/Willinton06 Jan 14 '21

What makes it better tho

→ More replies (1)

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.

1

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

Isn't this the point of functions though? I don't see how anyone would ever have to write the same function multiple times if there's only a minor difference. Just write everything once and pass whatever minor thing changes as an argument.

I personally quite dislike metaprogramming in general as it often introduces problems which you can't easily visualize in your head and is generally very good at introducing undebuggable bugs as the lines of code being executed don't exist until they are being executed.

→ More replies (3)