Blending data and logic#
Two different programming styles#
Imagine you want to write an application that checks if the entered food is one of the following vegetables or not:
carrot
beetroot
cabbage
Take 1
Console.Write(
"""
I can help you with your diet.
Enter the food:
""");
var line = Console.ReadLine();
if (line == "carrot"
|| line == "beetroot"
|| line == "cabbage")
Console.WriteLine("✅ this is a vegetable.");
else
Console.WriteLine("❌ I don't know what this is.");
Take 2
const string WelcomeMessage =
"""
I can help you with your diet.
Enter the food:
""";
const string YesMessage = "✅ this is a vegetable.";
const string NoMessage = "❌ I don't know what this is.";
string[] knownVegetables =
[
"carrot",
"beetroot",
"cabbage"
];
Console.Write(WelcomeMessage);
var line = Console.ReadLine();
if (knownVegetables.Contains(line))
Console.WriteLine(YesMessage);
else
Console.WriteLine(NoMessage);
string[]
means a collection of manystring
s. We will introduce this.
Activity 17
Which differences do you notice regarding:
readability
structure, e.g., where which components of code is located
How could this difference affect the programming experience?
Steps:
1 min think individually.
4 min discuss with your partner.