7

(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?

4
  • 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
    Commented Apr 27, 2015 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
    Commented Apr 27, 2015 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. Commented May 19, 2015 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
    Commented May 19, 2015 at 9:15

1 Answer 1

0

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?).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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