codeasy .net
or Sign Up

Almost ready

After talking with Jessica and successfully solving all the tasks she'd given me, I felt enthusiastic. I wondered, What mission will I have tomorrow? Is it something dangerous? Or had they just talked about some non-existant mission to speed up my learning?
While thinking about it I was making coffee in the kitchen. Ritchie came in.
"Hi, how do you feel after Jessica’s lesson?" he asked.
"Fine, thanks! Now I know how to properly name variables," I boasted.
"I’m sure you do! You are almost ready for the mission. The last learning exercise for today awaits you in Tom’s room. Go and talk to him now, and after that make sure to have a good rest. Not less than 8 hours of sleep! See you tomorrow!"
I said goodbye to Ritchie and entered Tom’s room. Tom was a big guy with kind eyes and a big brown beard.
"Hi there! Ritchie told me about a smart new time traveler. Are you Teo? I’m Tom, as you might have already read on the door sign. I need to leave in 15 minutes, so let’s get started!"

Tom pointed at a computer screen and began: "Here is a code snippet. Try to guess what it's doing."

int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;

"Wow… This doesn’t look so easy. Some math operations?"
"Yes, some math operations. But what exactly is the result of this code? Take a look here, is this one easier?"

int x = 10;
int y = 5;
// Code to swap x and y
x = x + y;
y = x - y;
x = x - y;

"Does this swap x and y?" I asked, "It says so... but how? I don’t understand."
"I'm not surprised. I couldn’t understand this code either, the first time I saw it. Try again, with this slightly modified version:"

int x = 10;
int y = 5;
// Code to swap x and y
x = x + y;// x now becomes 15
y = x - y;// y becomes 10
x = x - y;// x becomes 5

Now, how the code worked was clear to me. The additions explained how the mathematical operations took the beginning values of x (10) and y (5) through a series of changes, and in the end - x is 5 and y is 10.
"I understand now," I said.
"Good. But why now, when you couldn’t understand at first?"
"Because of the explanations after the double slashes."
"Exactly. These are called one line comments. A one line comment starts with // and continues to the end of the line. Here are some examples:"

// Comment that starts from the new line
// Comments are ignored by the C# compiler
// string anyString = "Don't ignore!"; this entire line is ignored; nothing happens.
string s = "Tom is cool"; // Comment after a statement. String s has been set.
// Several comments, one
// by one
// In comments, you can use any character! !@#$%^&*~"+=- See?

"Comments are completely ignored by the C# compiler," Tom explained. "This means they have no influence on your program; C# 'sees' your program as if the comments aren't there at all. That code snippet is executed the same as this one:"

string s = "Tom is cool";

"I got it! The comments are for humans alone to see. Is that all?"
"No, Teo. Sometimes you want to write very long comments. Ve-e-e-ery long. For that, multiline comments were invented. We create a multiline comment by putting everything we want in it between /* and */. Here is an example:"

/* 
Once upon a time I decided to write a program
The weather was nice…
Birds were singing
*/
int singingBirds = 20;

"Unlike one-line comments, multi-line comments have a terminating sign, ‘*/’. You should never forget it! If you do, the compiler will keep looking for it and nothing else in your program will be carried out! Any questions?"
"Hmm… I’m not sure. I think not. It all seems pretty simple."
"Comments are easy, and very helpful. They are mostly used to clarify a complicated code. Sometimes they're used to disable part of the code, usually temporarily for testing and debugging purposes. Don’t go overboard and pollute your code with tons of comments. In most cases it is enough to have meaningful variable names. Try to feel this balance."
Tom smirked, and added, "Some programmers have used comments for warnings:"

// When I wrote this, only God and I understood what I was doing.
// Now, only God knows.
// Magic. Do not touch.
/*
You may think you know what the following code does.
But you don’t. Trust me.
Fiddle with it, and you’ll spend many a sleepless
night cursing the moment you thought you’d be clever
enough to "optimize" the code below.
Now close this file and go play with something else.
*/
// 
// Dear maintainer: 
// 
// Once you are done trying to 'optimize' this routine, 
// and have realized what a terrible mistake that was, 
// please increment the following counter as a warning 
// to the next guy: 
// 
// total_hours_wasted_here = 42 
// 

"Hah! Funny!" I said.
Tom arched an eyebrow and started the practice phase of our session: "I need to go, Teo. Try to solve this task, if you can…"
He actually said, “if you can.” I hate when people say things like that. "IF?!" I’ll show him what I can do!

Tom didn't seem to notice my anger, and calmly explained: Some virus has changed the output of this program, by adding its own lines of code! Use a double slash on each line of suspicious code. This is known as "commenting out." With the added lines changed to comments, they will be ignored and the program will work correctly, while documenting the unwanted changes made by the virus. The correct output will be: “Comments help to document the code. But you should try to write self-documenting code without comments!”


This site uses cookies. By continuing to browse this site you are agreeing to our use of cookies.

Got it!