Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jannesiera/717d891f1d8ba2b30778589b881f62c8 to your computer and use it in GitHub Desktop.
Save jannesiera/717d891f1d8ba2b30778589b881f62c8 to your computer and use it in GitHub Desktop.
/* DESCRIPTION
In this file are two code samples. They both implement the exact same functional requirements but in a different way.
Your challenge? Write a description of what this piece of software does.
Why? I would like to know which piece of code you find easier to grasp. What can you tell me about each snippet?
*/
/* SNIPPET 1 */
// VIEW
var view = {
render: function(stateRepresentation) {
document.querySelector('#counterA').innerText = stateRepresentation.counterA_value;
document.querySelector('#counterB').innerText = stateRepresentation.counterB_value;
document.querySelector('#counterC').innerText = stateRepresentation.counterC_value;
document.querySelector('#buttonA').onclick = stateRepresentation.intents.incrementA;
document.querySelector('#buttonADuplicate').onclick = stateRepresentation.intents.incrementA;
document.querySelector('#buttonAB').onclick = stateRepresentation.intents.incrementBoth;
}
};
// VIEW MODEL
var viewModel = {
project: function(appState) {
var stateRepresentation = {
counterA_value: appState.counterA_value,
counterB_value: appState.counterB_value,
counterC_value: appState.counterA_value + appState.counterB_value,
intents: {
incrementA: actions.incrementA,
incrementBoth: actions.incrementBoth
}
};
view.render(stateRepresentation);
}
}
// MODEL
var model = (function(){
var appState = {
counterA_value: 2,
counterB_value: 12
};
return {
consider: function(data) {
if(appState.counterA_value >= 50) {
return;
}
if(data.incrementCounterA) {
appState.counterA_value = appState.counterA_value + 1;
}
if(data.incrementCounterB) {
appState.counterB_value = appState.counterB_value + 1;
}
viewModel.project(appState);
}
}
}());
// ACTIONS
var actions = (function() {
var action = function(callback) {
return function() {
model.consider(callback());
}
};
return {
incrementA: action(function() {
return {
incrementCounterA: true
};
}),
incrementBoth: action(function() {
return {
incrementCounterA: true,
incrementCounterB: true
};
})
};
}());
/* SNIPPET 2 */
var counterA_value = 2;
var counterB_value = 12;
function preventIncrement() {
return counterA_value >= 50
}
function updateCounterC() {
document.querySelector('#counterC').innerText = counterA_value + counterB_value;
}
function updateCounterA() {
counterA_value = counterA_value + 1;
document.querySelector('#counterA').innerText = counterA_value;
}
function updateCounterB() {
counterB_value = counterB_value + 1;
document.querySelector('#counterB').innerText = counterB_value;
}
document.querySelector('#buttonA').addEventListener('click', function(e) {
if(preventIncrement()) {
return;
}
updateCounterA();
updateCounterC();
});
document.querySelector('#buttonADuplicate').addEventListener('click', function(e) {
if(preventIncrement()) {
return;
}
updateCounterA();
updateCounterC();
});
document.querySelector('#buttonAB').addEventListener('click', function(e) {
if(preventIncrement()) {
return;
}
updateCounterA();
updateCounterB();
updateCounterC();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment