Personally, I look at programming as an art. In my opinion, there are good programmers, who know what they are coding and write beautiful code, and bad programmers that do not fully understand how coding works, and seem to rush and write ugly code.
This was written by me, this is personally how I have actually learned how to code.
1. Give descriptive names to any variables, methods, and classes.
An example of a descriptive name could be a control. I see this all the time where users will leave the names as Button1, Button 2, frankly, that is a horrible practice. What happens if you come up end up with 15 buttons all named Button#? That's a nightmare when it comes to debugging.
A good way to name controls is to give a prefix (find a list on this webpage), then give a descriptive name. For example, a login button, it can be btnLogin, instead of Button1.
Now with methods and variables, use a verb to describe what it is, or what it is for. Never name variables x, b, c, and so on, but rather variables so that it describes what it's used for. This goes with method naming as well, give a verb that describes what it does.
2. Use white space appropriately.
White space can easily help out when debugging when looking through code. Indention is an example of how white space is used correctly. Indentations should be used inside of scopes within other code, or when a line is continued.
Using line breaks help out when viewing long lines of code. If for example you have an if statement that checks multiple conditions and it goes past the end of the page (making you have to scroll horizontally), you should use line breaks.
Examples:
3. Use comments
Use comments to describe and explain what a method does or what a block of code does. Use comments as often as possible, however don't drown your code with comments every single line when it's not necessary.
4. Use exception handling
This is one area I didn't look at for awhile when I first began coding. Execption handling helps out when there is a block of code that may break, causing the application to crash. If you add exception handling, you can view the messages it "throws", and it will make your life a little easier to debug and fix the error.
Example:
5. Create simple code.
This should be a common sense one to every coder out there, if you can get a job done in 5 lines of code, why use 10 lines of code?
6. Create reusable methods and classes.
Many beginner coders will not understand how methods and classes work fully, but as you do, they are extremely helpful. Methods are simply used to group specific, reusable code, instead of re writing it several times.
7. Pascal and camel casing.
Pascal casing is when every word is capitalized.
Ex. ForeColor
Camel casing is when the first word starts with a lower case letter, and every other word after starts with a capital.
Ex. foreColor
Use Pascal casing for class names, public methods, and public variables.
Use Camel casing for private or protected methods, and any local scope variables.
Use a underscore followed by Camel casing when declaring private class scope variables. Ex. _forColor
8. Keep class scope variables private.
If these variables are to be accessed outside of the class, use public/protected properties.
This was written by me, this is personally how I have actually learned how to code.
1. Give descriptive names to any variables, methods, and classes.
An example of a descriptive name could be a control. I see this all the time where users will leave the names as Button1, Button 2, frankly, that is a horrible practice. What happens if you come up end up with 15 buttons all named Button#? That's a nightmare when it comes to debugging.
A good way to name controls is to give a prefix (find a list on this webpage), then give a descriptive name. For example, a login button, it can be btnLogin, instead of Button1.
Now with methods and variables, use a verb to describe what it is, or what it is for. Never name variables x, b, c, and so on, but rather variables so that it describes what it's used for. This goes with method naming as well, give a verb that describes what it does.
2. Use white space appropriately.
White space can easily help out when debugging when looking through code. Indention is an example of how white space is used correctly. Indentations should be used inside of scopes within other code, or when a line is continued.
Using line breaks help out when viewing long lines of code. If for example you have an if statement that checks multiple conditions and it goes past the end of the page (making you have to scroll horizontally), you should use line breaks.
Examples:
Inside other code segments or methods:
VB.Net
Line continuations:
VB.Net
C#
Line breaks:
VB.Net
C#
VB.Net
Code:
If loaded Then
MsgBox("Loaded")
End If
Code:
if (loaded)
{
Messagebox.Show("Loaded")
}
Line continuations:
VB.Net
Code:
'Some comment - This comment describes the use
of indentations when coding.
C#
Code:
//Some comment - This comment describes the use
of indentations when coding.
Line breaks:
VB.Net
Code:
Public Sub someLongMethodNameWithParameters(ByVal aRandomString As String, _
ByVal anObjectValue As Object)
'Code Here
End Sub
C#
Code:
public void someLongMethodNameWithParameters(string aRandomString,
Object anObjectValue)
{
//Code Here
}
3. Use comments
Use comments to describe and explain what a method does or what a block of code does. Use comments as often as possible, however don't drown your code with comments every single line when it's not necessary.
4. Use exception handling
This is one area I didn't look at for awhile when I first began coding. Execption handling helps out when there is a block of code that may break, causing the application to crash. If you add exception handling, you can view the messages it "throws", and it will make your life a little easier to debug and fix the error.
Example:
VB.Net
C#
Code:
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e ..
Try
loginToXBL(txtEmail.Text, txtPassword.Text)
Catch ex As Exception
lblError.Text = "Something went wrong!"
MsgBox(ex.Message)
End Try
End Sub
C#
Code:
protected void btnClear_Click(object sender, EventArgs e)
{
try
{
loginToXBL(txtEmail.Text, txtPassword.Text);
catch (Exception ex)
{
lblError.Text = "Something went wrong!";
MessageBox.Show(ex.Message);
}
}
5. Create simple code.
This should be a common sense one to every coder out there, if you can get a job done in 5 lines of code, why use 10 lines of code?
6. Create reusable methods and classes.
Many beginner coders will not understand how methods and classes work fully, but as you do, they are extremely helpful. Methods are simply used to group specific, reusable code, instead of re writing it several times.
7. Pascal and camel casing.
Pascal casing is when every word is capitalized.
Ex. ForeColor
Camel casing is when the first word starts with a lower case letter, and every other word after starts with a capital.
Ex. foreColor
Use Pascal casing for class names, public methods, and public variables.
Use Camel casing for private or protected methods, and any local scope variables.
Use a underscore followed by Camel casing when declaring private class scope variables. Ex. _forColor
8. Keep class scope variables private.
If these variables are to be accessed outside of the class, use public/protected properties.