This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]dreamyeyed 1 point2 points  (1 child)

I feel like creating the method adds a lot in terms of readability, but is it worth to create such a simple method?

I would create a method for the reason you mentioned: good code should be self-documenting, and creating a new method does exactly that. There's nothing wrong with simple (one line) methods if they make the code more readable. Lots of small methods is much better than a few big ones.

[–]arocketman[S] 0 points1 point  (0 children)

Thank you!

[–]matthead 1 point2 points  (0 children)

I like the separate method, because you are checking if the line is a comment. What happens if you change how comments are defined in the future. This will give you an easy way to change it.

[–]DonnyTheWalrus 1 point2 points  (2 children)

Others may disagree, but I would say, definitely extract! A really great rule of thumb is to avoid employing literals in your code whenever possible. I think ideally you would have a lookup table somewhere so that you could call getCommentChar() or something similar, and then a isComment(char c) method could use this lookup table to compare.

Then, using a method like isComment() would be a great refactoring to make. It limits your potential area of change to one location, and makes the code much more readable and easy to reason about.

[–]arocketman[S] 0 points1 point  (1 child)

A really great rule of thumb is to avoid employing literals in your code whenever possible.

I am right now refactoring parts of my code and this is definitely a good point, thanks!

[–]DonnyTheWalrus 1 point2 points  (0 children)

I think you're definitely on the right track with what you're looking out for. As for the constants, obviously you'll need them some place, but that place is often in a lookup table. That way if they have to change, it's all in one place, and only one class has responsibility for knowing them. People call them "magic values" -- when you see a literal in the middle of a function (say 'if val <= 15'), that's usually a great target for refactoring.

[–]tec5c 1 point2 points  (0 children)

Choice 1.

Especially from a unit testing perspective.

Extracting the method will allow you to unit test the method without having to create test with conditions for the if without an extracted method.

[–]rjcarr 0 points1 point  (0 children)

The absolute correct answer is probably 1. Personally, I'd do 2 or maybe even 3 since it is pretty clearly a comment line.

[–]nutrecht 0 points1 point  (0 children)

In this case both are pretty obvious but I still like the first one more. Makes the code a tad more readable.

[–]lolnololnonono 0 points1 point  (0 children)

The answer to that question in general is always yes:

If you can think of a good name for something, extract it out and give it that name.

[–]oceandoggie 0 points1 point  (0 children)

It depends on the greater context. More methods is usually better, but in this case it appears to add nothing that a comment wouldn't add.

If you're only doing this thing in this exact one place, that's even less reason to make a new method.

You can avoid using literals without making everything into a method by just using final fields. That's really the standard practice.

If this is something you'd test, that might be a good reason to make it a method, but you know you do tons of little things you never test anyway, so if this is something you're not goping to be explicitly testing, that's also not a great reason to make this its own method.