c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I830 materiais10.236 seguidores
Pré-visualização50 páginas
///	Method	to	adjust	a	text	string	within	a	given	field	with	the	width	length.
/// If the length of the text is larger than length, the method simply
/// returns the text.
/// Otherwise the method returns a string of length length, where text
///	is	right-aligned,	and	where	the	field	is	filled	with	the	character	fill.
/// </summary>
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
182 
Comments
/// <param name=&quot;text&quot;>The text to be adjusted</param>
/// <param name=&quot;length&quot;>Field width</param>
///	<param	name=&quot;fill&quot;>Fill	character</param>
/// <returns>Justifyed text</returns>
public	static	string	FillLeft(string	text,	int	length,	char	fill)
{
if (text.Length >= length) return text;
//	here	are	created	a	StringBuilder	with	the	required	capacity	to	the	fill
// characters you could instead have used the method Insert(), but it has a
// worse complexity than Add()
length -= text.Length;
StringBuilder builder = new StringBuilder(length);
while	(builder.Length	<	length)	builder.Append(fill);
return builder.ToString() + text;
}
/// <summary>
///	Method	to	adjust	a	text	string	within	a	given	field	with	the	width	length.
/// If the length of the text is larger than length, the method simply
/// returns the text.
/// Otherwise the method returns a string of length length, where text
///	is	center-aligned,	and	where	the	field	is	filled	with	the	character	fill.
/// </summary>
/// <param name=&quot;text&quot;>The text to be adjusted</param>
/// <param name=&quot;length&quot;>Field width</param>
///	<param	name=&quot;fill&quot;>Fill	character</param>
/// <returns>Justifyed text</returns>
public	static	string	FillCenter(string	text,	int	length,	char	fill)
{
if (text.Length >= length) return text;
return 
FillRight(FillLeft(text,	(text.Length	+	length)	/	2,	fill),	length,	fill);
}
}
}
I have this time inserted comments in the code. Comments have no effect on the translated program, but 
will only have affect for us humans to read and understand the code. Much can be said about comments, 
so some words about it.
It is important with comments in the code, and more than that \u2013 it should actually be a permanent part 
of the programming task quite on pair with writing the code itself, and there are at least two reasons:
1. All program code must be maintained over time, and often by other than the one who wrote 
the code. Therefore it is extremely important that the code has comments that tell about 
important decisions, and why the code is written as it is. Comments are important not only 
for others but also because you do not remember your own code even if it is just a few 
months old.
2. The actual process of commenting the code is important because during this process, you 
think through the code and wonder why the code is written as it is. It is an extremely 
efficient method to find errors and discrepancies in the code \u2013 and get them corrected.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
183 
Comments
If you see my examples, you will in most cases not see any comments. This is partly because they are 
small examples and not actual applications that must solve practical everyday problems, and secondly, 
that the code is precisely explained in this book. It is true to say that a part of the books content could 
instead be comments in the examples.
In C# you have three kinds of comments. Above I have written a comment before the namespace:
/*
This namespace contains several classes with miscellaneous methods.
One can perceive the namespace as a custom class library.
*/
It\u2019s a little older kind of comment that has been inherited from the C programming language, but the 
characters /* start a comment and it will continue until you meet the characters */ and between these 
two markers can be all the text that you may have like on a single line or spread over several lines. This 
kind of comment is not used as often anymore, but there\u2019s nothing wrong with it, and if you need at 
the beginning of a source file to write a lengthy documentation, it is an excellent form of commentary.
The most common type of comment in C# is used in front of each method. This is partly due that Visual 
Studio auto generate a skeleton, which you must complete. If, for example you place the cursor in front 
of the method FillCenter() and press the / three times, Visual Studio generates the following skeleton 
to a comment:
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
www.discovermitas.com
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
184 
Comments
/// <summary>
/// 
/// </summary>
/// <param name=&quot;text&quot;></param>
/// <param name=&quot;length&quot;></param>
///	<param	name=&quot;fill&quot;></param>
/// <returns></returns>
Here, the programmer has to comment the following:
\u2022	 A description of the method and what it does and including generally what the user of the 
method needs to know.
\u2022	 An explanation of the method\u2019s parameters, including which rules (pre-conditions) that the 
parameters must satisfy.
\u2022	 An explanation of the method\u2019s return value.
The advantage of using this kind of documentation \u2013 not just to methods, but for all program elements \u2013 is
\u2022	 that the documentation has a standard form and that you remember to document all 
important elements as parameters and return value
\u2022	 that the documentation is used by IntelliSense in Visual Studio
\u2022	 that the documentation is in XML form, and therefore can use of a tool to form a complete 
documentation for an entire program, for example as HTML
The last type of comment is simpler and consists of everything after // and to the end of the line is a 
comment (see the method FillLeft() above). This comment is typically used to document the individual 
statements in for example a method, or the description of a variable or its equivalent.
It is easy to write comments, but quite another thing is what you should write, and there are many 
attitudes, and the following must then be mine. Generally, you should write what you believe that others 
and including even you self needs to know to read and understand the code and thus could maintain it. 
You should not write what is clear. You must assume that who must read the code knows the language, 
and you should not document the language itself, but the explanations for the