This site uses 3rd-party cookies for targeted advertising. If you do not agree to this, then please leave this site now. Otherwise please click on "Ok" to continue.
Ok
 
 

Let's start with some code-cleanup

06-Jan-2015

In my endeavour to make the code for my blog-engine better, I will start with the main file, blogforum.js. Everything starts here. So I may as well start here too. :)

Let's begin with some code-cleanup. And by that I mean eliminating some console.log() statements which were used for debugging and have since then been commented-out. No need to keep those.

Next step is cleaning up all the require() statements at the beginning of blogforum.js. This is how they looked before I changed anything:

"use strict";
var http = require('http');
var qs = require('querystring');
var fs = require('fs');
var path = require('path');
var tools = require('./tools.js');
var te = require('./template-engine.js');
var postComment = require('./post-comment.js');
var config = require('./config.js');
var blog = require('./blog.js');
var notFound = require('./not-found.js');
var handleGetBlogpost = require('./handle-get-blogpost.js');

The first require() should be the one including tools.js as that file extends String.prototype with some utility functions. We will use those in other files, so by putting require('./tools.js') first, we ensure that String.prototype really has those new functions defined, before we start using them.

The next step has to be to put require('./config.js') next. We MUST have that configuration-data. Without it we would be lost and not know what to do or where our files are.

Well, what else do we have in all those require() calls? Hmm... require('fs') and require('./blog.js')... We don't even use those here... Be gone with 'em! :)

Wow, how much can you do wrong with a few require() statements? Quite a lot actually. If you don't remember to clean-up after yourself. :)

And this is how the beginning of blogforum.js looks after the above changes:

"use strict";

// This extends String.prototype with some utility-functions.
// Keep this at the front to ensure that this happens first.
var tools = require('./tools.js');


// Make sure to load the configuration-data. We REALLY need that! :)
var config = require('./config.js');


var http = require('http');
var qs = require('querystring');
var path = require('path');
var te = require('./template-engine.js');
var postComment = require('./post-comment.js');
var notFound = require('./not-found.js');
var handleGetBlogpost = require('./handle-get-blogpost.js');

If you want to take a look at the full source-code as it was at the end of this blogpost, you can get it from GitHub:

git clone https://github.com/MichaelSchoebel/blogforum
cd blogforum
git checkout blog-2015-01-06

See you soon with more code-cleanup.

Comments:

You want to comment on this blog-post? If yes, then simply enter your comment in the field below and click on "Submit".

Comments are moderated at the moment thanks to some $%&# who thought it would be funny to post total nonsense here.

 


Back to the Homepage

A Programmer's Diary