lundi 26 janvier 2015

All my applications end up becoming "hacky". Does it mean I'm not fit to be a programmer?


Everything starts nice in the beginning. The code is simple. Beautiful. Like haiku. It follows all the coding and design patterns. But then, as the application grows, it starts filling out with "hacky" code. In other words, code that I code not because it's the best solution but because, well, it's the only thing I can come up with. I try to improve it, of course. However, most of the time, I just become stuck there, and end up leaving the code as it was.


Here's an example:



contenteditable: function() {
return '<div class="content" contenteditable="true">' + this.content + '</div>'
}

Template.documentPage.events({
'input .content': function(e) {
var savedSel = rangy.saveSelection()
var document = $(e.target).html()

Documents.update(this._id, {$set: {content: document}}, function(){
rangy.restoreSelection(savedSel)
})
},

'input .title': function(e) {
var document = $(e.target).val()

Documents.update(this._id, {$set: {title: document}})
}
})


What the code does is to save the input of the user on keypress. Now the problem is that the contenteditable area rerenders the html inside each time the area is saved. So I had to rely on two hacks: one to return the contenteditable area before the rerendering mechanism takes place. And another to restore the position of the cursor after the html rerenders.


I'm not sure what kind of answer I want to get. I guess I just want to hear other people's experiences. Or whether I should just focus on front-end/design (my other job).





Aucun commentaire:

Enregistrer un commentaire