2
\$\begingroup\$

Recently I have been busy with nodejs and I thought to create something likethis 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?
200_success's user avatar
200_success
146k22 gold badges191 silver badges481 bronze badges
askedJul 19, 2016 at 17:07
CodeYogi's user avatar
\$\endgroup\$

1 Answer1

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 onlycan you mix the two, depending on your needs youshould 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, thePromise Pattern was created.

On to my favorite subject, naming things.

  • In the StdIn constructor, the argument is calledopt_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

  • Theopt_name argument could just be calledname since there is not ambiguous reference between the argument and the property on the StdIn object

  • Rename thers property to something meaningful, like:

    • readStream
    • input
    • inputStream
    • stream

Otherwise I don't see any glaring issues.

answeredJul 19, 2016 at 18:15
Greg Burghardt's user avatar
\$\endgroup\$
1
  • \$\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\$CommentedJul 20, 2016 at 2:17

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.