Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

Nathan Lilley
Nathan Lilley
6,085 Points

I crammed everything into two variables and only used three lines. Is this bad code?

It seems to work fine:

https://w.trhou.se/ppm7xfzj6r

Thank you!

2 Answers

rydavim
rydavim
18,814 Points

The short answer is no, it's not bad code. There's nothing wrong with it, and for simple things I would say this is fine.

The slightly longer answer is that I would not personally write it this way. I'd pull the calculation out into it's own variable, say difference, and use that to write to the document. Generally, I feel like this makes your code easier to read. However, much of this will come down to personal preference and there is no one right way to write code.

When it comes to formatting and commenting my code, I try to ask myself, "If I come back to this code some time in the future, will I be able to easily make sense of it? Can someone else easily make sense of it?" If the answer to those questions is yes, you're usually fine in my opinion.

Tangentially related to clarity, splitting the difference generally means taking the average of two things. It's a fun little program, but I might suggest changing the wording slightly.

Hopefully that helped, and maybe some others can weigh in with their thoughts on the matter. In any case, have fun and happy coding!

Nathan Lilley
Nathan Lilley
6,085 Points

This is what I needed to know, thank you! I guess I didn't know if it was preferable to use as few lines of code as possible, but now i see that clarity takes priority over that.

And yes, the wording doesn't quite work at the end there. I will revise.

Thank you for your response!