4

A conceptual question, I tried to google but couldn't find any definitive answers, so here it goes:

Is it a good practice to write Java methods to return HTML strings that will be returned to the display/presentation layer?

Example:

public string getTextField(){ return "<input type=\"text\"/>"; } 
4
  • 3
    Not if they break your syntax... ;P
    – deceze
    CommentedApr 24, 2013 at 4:02
  • it depends... Generally we shouldn'tCommentedApr 24, 2013 at 4:42
  • 1
    Like it's been said, it depends. Using a servlet to build HTML (to return a page to load an applet for example) is relatively standard practice. But in most other scenarios, templating (either by XSL, XHTML or just baking the HTML into a pseudo properties file) should be the default choice.
    – kolossus
    CommentedApr 24, 2013 at 5:04
  • 1
    I understand this if it's for applet development. However in general coding practice, especially when we are using a certain framework like Struts or Spring or even a plain old jsp-servlet structure, would it be better to leave the html part at the presentation layer and not coupling it into the logical part?
    – ipohfly
    CommentedApr 24, 2013 at 5:16

3 Answers 3

7

It's generally considered to be a bad idea in terms of 'modern' web development. Using a Javascript based templating framework such as angular.js allows you to cleanly separate your client-side code/logic from your server-side code/logic.

Having the HTML in your Java code makes it hard to validate, difficult to develop with rapidly (you have to recompile), difficult to debug etc.

    4

    For me this is a massive code smell, regardless of the server side language.

    It obfuscates how the pages are constructed and makes it much more difficult to understand when you have different concerns merged together.

    This can make extending, debugging, and fixing a system all much more complicated.

    I would be suspicious of the credentials of a colleague suggesting this as a way forward.

    I'd be especially suspicious of a company selling me a solution and code base ie. the solution being so complicated I have to keep them around to maintain/extend it.

    4
    • hmm any specific reason why you will consider it as a massive code smell? Just to find out more on the thoughts behind. :)
      – ipohfly
      CommentedApr 24, 2013 at 10:20
    • I suppose it's one of these feelings you get, it's just not the right way to do things. You can't just look at an HTML page and see how it flows.
      – ozz
      CommentedApr 24, 2013 at 10:56
    • -1. Could you please justify your opinion? Otherwise, this answer is really no more than a simple "It's bad. Trust me."
      – blubb
      CommentedApr 24, 2013 at 11:16
    • 1
      @blubb fair point, added some justification
      – ozz
      CommentedApr 24, 2013 at 11:32
    1

    No practice is totally good or totally evil, always depends on context. But some practices are more evil than others.

    For a quick .jsp where the Java code is embedded in the HTML template directly, Java methods returning bit of HTML is OK. But for anything bigger than quick hack it is evil, because it is difficult to maintain.

    The problem is that in larger application you want somebody to focus on the web design and other team members to do the backend. And for the designer it is easy to understand a decent templating system, but they will have big problems hunting down the snippets of HTML generated in methods buried deep within the backend.

    The other problem is that you want the templating system to ensure a sane template leads to sane result. I am not sure what would be it's Java equivalent, but e.g. Genshi (python) checks that valid XML goes in and ensures valid XML goes out by automatically escaping all strings it interpolates and only accepting bits of HTML/XML as XML objects that contain the tree representation and therefore are always well formed. Such template system goes a long way towards avoiding cross-site scripting and similar attack opportunities.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.