Skip to content

Birmingham | 2026-MAR-SDC | Joy Opachavalit | Sprint 3 | Implement shell tools#408

Open
enjoy15 wants to merge 7 commits intoCodeYourFuture:mainfrom
enjoy15:implement-shell-tools
Open

Birmingham | 2026-MAR-SDC | Joy Opachavalit | Sprint 3 | Implement shell tools#408
enjoy15 wants to merge 7 commits intoCodeYourFuture:mainfrom
enjoy15:implement-shell-tools

Conversation

@enjoy15
Copy link
Copy Markdown

@enjoy15 enjoy15 commented Mar 21, 2026

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Completed all exercises

@enjoy15 enjoy15 added 📅 Sprint 3 Assigned during Sprint 3 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 21, 2026
Copy link
Copy Markdown
Member

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looking good, but it looks like you're slightly over-fitting to the examples in the README.md - give some thought to other command lines a user may try to run, and how your implementations may be surprising to them.

Comment thread implement-shell-tools/cat/cat.js Outdated
let lineNumber = 1;

files.forEach((file) => {
const filePath = path.resolve(file);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this line doing? What would break if you removed it and just used file instead of filePath?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the unnecessary use of path.resolve and now directly use file. The code works as expected without it, simplifying the implementation.

Comment thread implement-shell-tools/cat/cat.js Outdated
Comment on lines +17 to +24
if (options.numberNonEmpty && line.trim()) {
console.log(`${lineNumber}\t${line}`);
lineNumber++;
} else if (options.numberLines) {
console.log(`${lineNumber}\t${line}`);
lineNumber++;
} else {
console.log(line);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three branches here look quite similar and repetitive. In general, if you have multiple similar branches, it's more clear to extract the differences into variables, and then run the same code, i.e. so you'd only have one call to console.log which looks more like console.log(`${prefix}${line}\n`) where prefix may be set differently based on options (including potentially an empty string).

This way it's easier for someone reading the code to see what's the same / different in each case, and also avoids the hazard that someone updates one of the branches but forgets to update the other ones.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have refactored the code to reduce repetition. Now, the differences are handled using a prefix variable, and there is only one console.log statement. This makes the code cleaner and easier to maintain.

Comment thread implement-shell-tools/cat/cat.js Outdated
}
});
} catch (err) {
console.error(`cat: ${file}: No such file or directory`);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exit code will your program have if something went wrong? What exit code should it have?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added proper exit codes. The program now exits with process.exit(1) when an error occurs, ensuring it signals failure appropriately.

Comment thread implement-shell-tools/cat/cat.js Outdated
}
});
} catch (err) {
console.error(`cat: ${file}: No such file or directory`);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the problem here is that the file didn't exist? What would happen e.g. if you didn't have permission to read the file?

In general specific error messages are good, but misleadingly specific error messages are a problem - if we're not sure what went wrong (or if we have more information about what went wrong), we should present that information, rather than guessing. And if we are guessing, we should make it clear we're not sure what the problem exactly was and that this is a guess.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have improved the error handling to differentiate between file-not-found (ENOENT) and permission errors (EACCES). The error messages now provide more accurate information about what went wrong.

Comment on lines +39 to +47
if (arg === '-l') {
options.lines = true;
} else if (arg === '-w') {
options.words = true;
} else if (arg === '-c') {
options.bytes = true;
} else {
files.push(arg);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if someone specifies more than one of these flags? What should happen?

Given the test cases we gave you in the README file, it's ok if your implementation doesn't do the same thing as the real wc does, though that would be ideal, but in general ignoring user input is bad - so if someone asks for both -l and -c and you ignore one of them, that can be confusing. Either showing both, or giving an error, is probably preferable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to handle multiple flags. If multiple flags are specified, the program now displays all the requested results together, ensuring user input is respected.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixed some problems, but introduced some new ones too :)

What happens if you wc /some/file with no flags? What does your program do?

Also, what happens if you wc -l /some/file /some/other/file? What does your program do? What's different between them?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out! Here's how the program currently behaves:

If no flags are provided (e.g., wc /some/file), the program defaults to displaying all metrics (lines, words, and bytes) for the file. This behavior is consistent with the standard wc command.

If multiple files are provided with a single flag (e.g., wc -l /some/file /some/other/file), the program outputs the line count for each file on separate lines. However, it does not provide a total count across all files, which I’ve made the updates to align with the standard wc behavior.

Comment thread implement-shell-tools/wc/wc.js Outdated
}

files.forEach((file) => {
const filePath = path.resolve(file);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, why do you need the path.resolve here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the unnecessary use of path.resolve and now directly use the file path provided by the user. This simplifies the code without affecting functionality.

Comment thread implement-shell-tools/ls/ls.js Outdated
} else if (arg === '-a') {
options.all = true;
} else {
directories = [arg];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This meets the requirements for the examples listed in the README.md, but feels like it risks being confusing to users. If someone specified ls /some/path /some/other/path what do you think they would expect to happen? What does your code actually do? (See also the comment about wc options for a similar question)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to handle multiple directories. The program now lists files for all specified directories, making it more intuitive for users.

@illicitonion illicitonion added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 27, 2026
@enjoy15 enjoy15 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 5, 2026
Copy link
Copy Markdown
Member

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking much better! Just a couple of things to look at still :)

Comment thread implement-shell-tools/cat/cat.js Outdated
const lines = data.split('\n');

lines.forEach((line) => {
const prefix = options.numberNonEmpty && line.trim() ? `${lineNumber}\t` : options.numberLines ? `${lineNumber}\t` : '';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested ternary operators are generally quite hard to read (and also, two of the conditions here lead to the same result, which is duplication) - I'd recommend rewriting this without a ternary.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out! I have refactored the code to replace the nested ternary operator with a more readable if-else structure. This change eliminates the duplication and improves clarity by explicitly handling the conditions for setting the prefix.

Comment thread implement-shell-tools/cat/cat.js
Comment thread implement-shell-tools/cat/cat.js
Comment on lines +39 to +47
if (arg === '-l') {
options.lines = true;
} else if (arg === '-w') {
options.words = true;
} else if (arg === '-c') {
options.bytes = true;
} else {
files.push(arg);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixed some problems, but introduced some new ones too :)

What happens if you wc /some/file with no flags? What does your program do?

Also, what happens if you wc -l /some/file /some/other/file? What does your program do? What's different between them?

@illicitonion illicitonion removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 7, 2026
@enjoy15 enjoy15 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 10, 2026
Comment thread implement-shell-tools/cat/cat.js Outdated
Comment on lines +16 to +22
if (options.numberNonEmpty && line.trim()) {
prefix = `${lineNumber}\t`;
} else if (options.numberLines) {
prefix = `${lineNumber}\t`;
}
console.log(`${prefix}${line}`);
if (prefix) lineNumber++;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works and is clear to read, but I may consider rewriting it as something like:

Suggested change
if (options.numberNonEmpty && line.trim()) {
prefix = `${lineNumber}\t`;
} else if (options.numberLines) {
prefix = `${lineNumber}\t`;
}
console.log(`${prefix}${line}`);
if (prefix) lineNumber++;
const shouldNumberLine = options.numberLines || (options.numberNonEmpty && line.trim());
if (shouldNumberLine) {
prefix = `${lineNumber}\t`;
lineNumber++;
}
console.log(`${prefix}${line}`);

Two reasons for this:

  1. It shows that both of the "should number line" cases are handled the same.
  2. The "should you increment the line count" is really based on "whether you should number the line", not "whether there's a prefix". I can imagine in the future we may add prefixes for different reasons not just line numbering, at which point if (prefix) lineNumber++; would now be incorrect. We generally want our conditions to be based on the underlying reason something should be done, not something else we decided to do for the same reason.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve applied this refactor now. I introduced a shouldNumberLine condition and based both prefix assignment and lineNumber increment on that condition, so the logic is clearer and tied to the underlying behavior rather than checking prefix.

Comment thread implement-shell-tools/wc/wc.js Outdated
const bytes = Buffer.byteLength(data, 'utf8');

const results = [];
if (options.lines || (!options.lines && !options.words && !options.bytes)) results.push(lines);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but it doesn't read very well and is quite brittle - imagine if in the future you added a new option, you'd need to update all of these places where you're checking if no other options are specified.

Right now your options.lines perfectly mirrors the exact flags passed. But this doesn't have to be the case. Flags are just ways of configuring our program, but our program doesn't have to be tightly tied to the flags it exposes to users. Try making this line just be if (options.lines) { and see if you can initialise options.lines when parsing the flags to always have the correct answer for "should I show lines".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — updated now. I simplified flag handling as suggested, fixed -l counting logic, and aligned output formatting. Verified against all README wc commands.

@illicitonion illicitonion removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 13, 2026
@enjoy15 enjoy15 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 3 Assigned during Sprint 3 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants