(The framework used in question is AngularJS so the question can be translated as "Whether to introduce Directives for small repeating code segments in Views")

As a developer who focuses on Java backend development, I am very used to the DRY principle -- even the smallest repetition (long object access paths, logic used only twice, etc.) are often extracted.

However, in front-end HTML templating, the cost of reusing code is unbelievably high. In the case of AngularJS, the Directives syntax is extremely cumbersome (requires defining a JS object with nonsensical fields and a separate HTML file for the contents). For a small amount of code repeated a limited number of times, abstraction seems to harm maintainability.

Question is should I still abstract and hope for the frameworks to get better (e.g. Angular 2.0 have a much better syntax or the project could switch to React), or just repeat the code for readability?

  • Are you using JSPs for rendering the HTML? If so, the standard way to reuse code is to encapsulate it in a tag. I'm not sure if Angular makes this approach difficult, but we have had no problems with JQuery. – kiwiron Apr 27 '15 at 9:13
  • @kiwiron (sorry not logged in at work) The page is just static HTML with all dynamic stuff handled by Angular. – billc.cn Apr 27 '15 at 22:07
  • 1
    Are you just hoping a web UI dev like myself with 10ish years of experience and more knowledge of CSS/JS than probably the top 95th percentile-conservatively in his field will come out and tell you that Angular sucks giant mega-balls? Cuz I'll totally say that. It's a modern-times framework. A framework for people so turned off by knowing things they'd rather just crank those wrenches over and over in the same spots until they lose their freaking minds. It reinvents hooks to the HTML, its own scope scheme, and gives us directives in a language with first-class functions. There is no DRY here. – Erik Reppen May 19 '15 at 8:02
  • @ErikReppen Points taken... I am still counting on the possibility to switch to React or maybe native Web Components + some light-weight framework when things matures... – billc.cn May 19 '15 at 9:15

If you abstract/DRY and hope to switch to Angular 2 or React you will have to touch your code when that happens. If you repeat the code you might not have to touch it much, maybe a simple search/replace on the whole project with a pinch of regex.

You really need to know the specifics (project size, cost of changing code, testing methods) and guess how each instance of that copy-pasted code might evolve in the future (remain the same?, add lots of parameters to your abstraction?).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.