Consider you wanting to implement a simple game of checkers. There would be a rectangular game board and the player would able to move the pieces around according to a particular set of rules.

General good design principles suggest that we decouple the display of the game from it's data and logic.

That is, the display of the game board and the ability to click and move stuff around would be decoupled from the actual representation of a game board in memory. The data in memory doesn't know what displays it.

Now, let's talk about JavaScript specifically. JavaScript is a language designed mainly to be used as a lightweight client-side language. (I know, JS has grown far beyond it's original intended usage, but it is still generally regarded and used as what I described). All JavaScript does on the client side is manipulate Html and Css.

This means that, by it's nature, JavaScript is very coupled to the Html and Css; the GUI. That is it's nature.

So my question is: when designing a client-side JavaScript app, how loosely coupled should I make the business logic and data, and the user interface?

Should I make an effort to entirely separate the display and the logic, as I would have done in a e.g. a C# app? Or is it acceptable in JavaScript to allow a certain amount of coupling between logic and display?

  • 6
    Javascript, by its nature is NO more coupled to the HTML and CSS than you write it to be. Your Javascript code has no generic coupling to your HTML or CSS unless you make it be coupled. So, I think your supposition here is entirely flawed that by its nature things are coupled - they aren't. – jfriend00 Jan 8 '15 at 22:43
  • 4
    My preferred answer here is that you should assess your immediate goal and any likely future goals you might have with this code and find a design that is clear, concise, extensible in likely directions, yet efficient in implementation. This means you don't go way out of your way to decouple things that are unlikely to need to be decoupled, but at the same time, you pick an intelligent architecture that is easy to understand, implement, debug and produce solid maintainable code with while meeting the needs of your immediate project and any likely follow-on projects. – jfriend00 Jan 8 '15 at 22:44
  • 1
    JavaScript is not special here, you would perform as much separation between presentation and business logic as you would in any other programming language. Nothing is more tightly coupled in JS than it is in other languages... – Benjamin Gruenbaum Jan 8 '15 at 23:17

Your Answer

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

Browse other questions tagged or ask your own question.