DEV Community

Cover image for Ideal Code, I can't today
Austin French
Austin French

Posted on

Ideal Code, I can't today

The real world, is rarely ideal

Today, I had to hack it. And I don't mean hammer on the keyboard to get a feature done. Nor do I mean "debug it until I make it", I mean, work against everything I think good, ideal code is. I mean, make something that I feel like it shouldn't be in a code base, part of a library until legacy code is updated.

Backstory

We have an angularJS application, running with jQuery, MVC, and various libraries written for that stack. I've been architecting for months now a path to Angular using ngUpgrade. Most of it has been going great:

  1. Make a thing
  2. Set up the components, services, and classes to make an Angular native and well designed pattern.
  3. Do a little hackery with window variables from the MVC views.
  4. Looks good, functions well, and doesn't break anything the Angular creators would warn against in a style guide.

Hacking it

This though, isn't something I ever thought would be a thing.
Todays goal:

  1. Create a parent Angular component
  2. Define some meta data to pass to a directive component
  3. Have the directive convert the parent components meta data to HTML
  4. Insert the HTML into a rendered element from a library
  5. Have the child component attach the parent component's functions to the window as a function for the onclick attribute of the library rendered angularJs / HTML to call.

It worked!

And it works!

I can create an instance of a class, and assign it something like:

myFunction(myArgument: type){
  // the stuff we need to do in the parent component
  // from the 3rd party angularJs/ Jquery library inserted to the 
  // DOM in a child component rendered HTML
}

element.Function = this.myCallbackFunction;
Enter fullscreen mode Exit fullscreen mode

I am both stoked that I was able to get the basic functionality, but also heartbroken that I had to write something this janky.

Conclusion

The real world is rarely as ideal as the code standard we push. I don't know how long I will have to support this code, but until the called library is fixed / modernized we are stuck. I pity all of our developers just learning Angular, and who might have to grok this concept. And I especially fear for those supporting it years or a decade down the road if the library isn't ever modernized properly.

Top comments (0)