0

Which of the two you guys recommend for my code to be more convenient for those who will read my code in the future.

int grade = 75; if (grade < 75) { Console.WriteLine("Failed"); } or int grade = 75; const int fail = 75; if (grade < fail) { Console.WriteLine("Failed"); }

19th Nov 2022, 2:38 PM
Mozzie
Mozzie - avatar
5 Réponses
+ 2
Second one is more reusable. First one is more readable and easy to understand. I would pick the second one because reusability is more important.
19th Nov 2022, 3:13 PM
Black Winter
+ 2
it depends. If you got a huge project and you do the 2nd for everything (if-else, for, while etc...) the code will became really huge and probably will be a mess with so many variables. But if do the 1st always and later want to change something you maybe need to edit a lot lines of code that takes time and maybe forgot to change something. So choose wisely.
19th Nov 2022, 3:47 PM
Giannis
Giannis - avatar
+ 2
The second code is better because the reader will not be wondering what the magic number 75 is.
21st Nov 2022, 11:29 AM
Bob_Li
Bob_Li - avatar
+ 1
In the first code , if you want to change the value of fail variable you must get inside if statement and change it. This sounds easy but in programming its not a good tactic to change variables into statements. Variables must be declared outside of statements in order to change them easily.
19th Nov 2022, 3:23 PM
Black Winter
0
What do you mean by reusable?
19th Nov 2022, 3:16 PM
Mozzie
Mozzie - avatar