C
C#2mo ago
Staynk

Not understanding this lesson

Im following the Microsoft fundamentals course and in this video he started with a code game that all made sense and then he started to condense it and reduce the amount of code. Well now I am confused.
No description
15 Replies
Staynk
StaynkOP2mo ago
doesnt make sense when he defines the string message = "";
Staynk
StaynkOP2mo ago
No description
Staynk
StaynkOP2mo ago
it made sense here then he started to reduce and im not understanding how the string message can have multiple definitions i thought you could only define a string once
glhays
glhays2mo ago
Nope That's why it is called a variable
Anu6is
Anu6is2mo ago
the variable is defined (declared) once on line 10, it's value then changes based on the if condition it enters
glhays
glhays2mo ago
He declares a variable named message as a string type set initially to Hello.
Staynk
StaynkOP2mo ago
could he have simply just done string message; and defined it that way i think that is why im thrown off
Anu6is
Anu6is2mo ago
yes
Staynk
StaynkOP2mo ago
i see
Anu6is
Anu6is2mo ago
a default value isn't technically required
Staynk
StaynkOP2mo ago
okay okay makes sense I mean why condense it though It worked and it made sense It's almost more work to go back and remove stuff
Anu6is
Anu6is2mo ago
u generally don't want redundant code ideally you'd do it the condensed way first, not write it one way and go back and change it although refactoring is a thing
Staynk
StaynkOP2mo ago
gotcha
Anu6is
Anu6is2mo ago
generally if you have repeated code it's better to reduce it as it helps make the code easier to maintain while this is a simple case, consider where you wanted to change how the message is output in the long version you'd have to go update all the console.writeline to whatever the new thing is in the condensed on, there's only the final console.writeline to change again it a simplified case here but duplicated code could also lead to bugs where you update one instance of the duplication and forget to update the other instance, then you have differing logic in 2 places when it should be the same logic in both those places
glhays
glhays2mo ago
reduce is actually known as refactoring.

Did you find this page helpful?