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.

15 Replies
doesnt make sense when he defines the string message = "";

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
Nope
That's why it is called a variable
the variable is defined (declared) once on line 10, it's value then changes based on the if condition it enters
He declares a variable named message as a string type set initially to Hello.
could he have simply just done
string message;
and defined it that way
i think that is why im thrown off
yes
i see
a default value isn't technically required
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
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
gotcha
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
reduce is actually known as refactoring.