2
\$\begingroup\$

Recently I have been busy with nodejs and I thought to create something like this Java input library, so I started with some rough code like below:

'use strict';
const readline = require('readline');
const fs = require('fs');

class StdIn {
  constructor(opt_name) {
    this.name = opt_name;
    this.rs = this.name ? fs.createReadStream(this.name) : process.stdin
  }

  readInt(callback) {
    this.rs.on('data', (chunk) => {
      let line = chunk.toString();
      let numbers = line.trim().split(/\s/);
      numbers.forEach(num => {
        callback.call(this, num);
      });
    });
    this.rs.on('end', () => {
      process.exit(0);
    });
  }

  readLine(callback) {
    this.rs.on('data', (chunk) => {
      let line = chunk.toString();
      callback.call(this, line);
    });
    this.rs.on('end', () => {
      process.exit(0);
    });
  }
}

const stdIn = new StdIn(process.argv[2]);

//stdIn.readInt(num => {
//  console.log(num);
//});

stdIn.readLine(line => {
  console.log(line);
});

Its working fine till now and buts its still incomplete and the code is too messy, the problem clearly visible here that I am trying to push event oriented code into my object oriented world, I am confused basically for two things:

  1. It is practical to do so?
  2. If yes then what would be the best way?
\$\endgroup\$
1
\$\begingroup\$

... the problem clearly visible here that I am trying to push event oriented code into my object oriented world

There is nothing wrong with object oriented and event oriented code being intermingled. It's like saying "toast and jam should not be used in the same breakfast." Not only can you mix the two, depending on your needs you should mix the two. The "problem" I think you're referring to is mixing object oriented with functional programming.

Since this is JavaScript, and it handles both programming paradigms good enough, feel free to mix the two. The only thing to consider is "callback Hell" where you have callbacks nested inside callbacks. For this, the Promise Pattern was created.

On to my favorite subject, naming things.

  • In the StdIn constructor, the argument is called opt_name, yet everything else appears to be written in camelCase. Pick either snake_case or camelCase and be consistent. To be consistent with other API's I would recommend camelCase

  • The opt_name argument could just be called name since there is not ambiguous reference between the argument and the property on the StdIn object

  • Rename the rs property to something meaningful, like:

    • readStream
    • input
    • inputStream
    • stream

Otherwise I don't see any glaring issues.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ But there is are issue, like I have to listen for the events in both of my methods which looks ugly, I am not well known to node world yet I am feeling that there must be some pragmatic approach. \$\endgroup\$ – CodeYogi Jul 20 '16 at 2:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.