O
optimistx
It is fun to add a piece of short code to a project, eg. a method of
a nice object/constructor without worrying, which other possibly
numerous locations in the same project have to be changed.
But as the code grows, everything starts to depend on everything
else, the code feels like spaghetti, entangled, and the fun ends.
Which practical principles would keep the fun as long as possible?
How to arrange code so that more or less unknown future changes
would occur in as few separate locations as possible and reasonable?
with still other words:
How to organize js, html, css to achieve good encapsulation and
modularity of all written code?
Books about programming recommend writing classes/objects, which
have clear interfaces, follow certain design patterns and rules:
Separate interface and implementation.
Use encapsulation.
Define classes carefully, avoid monster classes.
Put one task to a method, not many.
Put css to a separate file.
Put js to separet file(s).
Etc, those generally make sense and feel good.
So I do that and the problem is solved, uh?
Between css and html there seems to be no serious problems:
selectors and tags are their common ground, nothing else
to worry about.
css and js seem to live in different worlds in my programs (so far),
no entanglement, no problems.
Between js and html there is a chicken and egg problem. Who is
the boss?
If I write a nice js-'class'/object e.g. to update some program
settings given by the user, the program soon ends up in manipulating
complex forms 'far away on an html-page', breaking encapsulation.
There are at least two code locations to worry about: html-form and
js-methods of the object ( with events).
The obvious idea to generate html-code inside js-object sounded
fascinating at first, but in tests the code started to feel
artificial, long and clumsy especially, if avoiding usage of
innerHTML. The generated html-code would be appended to the
existing htmlelement(s) using the class or id.(compare css).
Is this way of 'having js-constructor as a boss', and generating
html-code with commands document.createElement, element.appendChild
etc the way to go, anyhow? What have you used?
If the input form is written in html and js-code is added to its
elements, the code feels oldfashioned <a href="#" onclick=
"do wonderful things here>...</a> type
of code, but it has the advantage of being in one place only.
There is the other extreme: put everything into js, leave only
minimal html, generate html dynamically with js.
The balance between the extremes has to be found, but how?
Example pages?
a nice object/constructor without worrying, which other possibly
numerous locations in the same project have to be changed.
But as the code grows, everything starts to depend on everything
else, the code feels like spaghetti, entangled, and the fun ends.
Which practical principles would keep the fun as long as possible?
How to arrange code so that more or less unknown future changes
would occur in as few separate locations as possible and reasonable?
with still other words:
How to organize js, html, css to achieve good encapsulation and
modularity of all written code?
Books about programming recommend writing classes/objects, which
have clear interfaces, follow certain design patterns and rules:
Separate interface and implementation.
Use encapsulation.
Define classes carefully, avoid monster classes.
Put one task to a method, not many.
Put css to a separate file.
Put js to separet file(s).
Etc, those generally make sense and feel good.
So I do that and the problem is solved, uh?
Between css and html there seems to be no serious problems:
selectors and tags are their common ground, nothing else
to worry about.
css and js seem to live in different worlds in my programs (so far),
no entanglement, no problems.
Between js and html there is a chicken and egg problem. Who is
the boss?
If I write a nice js-'class'/object e.g. to update some program
settings given by the user, the program soon ends up in manipulating
complex forms 'far away on an html-page', breaking encapsulation.
There are at least two code locations to worry about: html-form and
js-methods of the object ( with events).
The obvious idea to generate html-code inside js-object sounded
fascinating at first, but in tests the code started to feel
artificial, long and clumsy especially, if avoiding usage of
innerHTML. The generated html-code would be appended to the
existing htmlelement(s) using the class or id.(compare css).
Is this way of 'having js-constructor as a boss', and generating
html-code with commands document.createElement, element.appendChild
etc the way to go, anyhow? What have you used?
If the input form is written in html and js-code is added to its
elements, the code feels oldfashioned <a href="#" onclick=
"do wonderful things here>...</a> type
of code, but it has the advantage of being in one place only.
There is the other extreme: put everything into js, leave only
minimal html, generate html dynamically with js.
The balance between the extremes has to be found, but how?
Example pages?