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 trialMilo Dennison
Courses Plus Student 4,540 PointsWhy do so many examples use Hard Coding? I thought this was bad form across all programming
I don't think the question needs elaboration
2 Answers
Steven Parker
231,275 PointsThe page you linked this question to has no examples of hard coding on it! Did you mean another page?
Also, in case the instructor doesn't post a more definitive answer, for the purposes of illustrating a specific technique the use of hard-coded values can help keep the example simple and make the technique being shown a bit more clear. You can, of course, apply the technique later in actual code without using any hard-coded values.
Tim Stride
13,457 PointsJust to add that in some cases I think hard-coding a value might be acceptable if it is not going to vary. For example, if the number of rows and columns (in the Board class) and the number of tokens per player (in the Player class) would always be the same, it is simpler to hard code those.
Steven Parker
231,275 PointsEven in such a case, "best practice" would be to define a constant and then refer to it in the code.