19 November 2010

Curly braces
Curly braces are an important part of our life code. Although some languages managed to avoid the usage of brackets, I am a fan. You can have too few curly braces, but never too many.
Adding the opening curly brace on the same line as the condition or on the next one is usually dictate by some code convention specific to the language you work with. For example Java likes to open the brace on the same line as a condition, C# on the next line. Java guys might argue that C# is wasting precious lines on your monitor, C# guys that Java makes the code less eye-friendly.
So who likes curly braces? for, do, while, foreach (C#), switch, all love it.
The rule of thumb would be to encapsulate all the statements in curly braces, no matter if your if or loop has just one statement.
if (clients > 10) { pages = clients / 10; } //versus: if (clients >; 10) pages = clients / 10; Besides the visual part it can also avoid some bugs, like when you want to add a new statement and you put the cursor after the ";", hit enter and type fast the new statement, forgetting to add the braces. Don't smile, it does happen!
Same with loops. Even if you have one statement encapsulate it into curly braces.
[csharp]
for (int i = 0; i < clientListBox.Items.Count; i++)
{
clientListBox.Items[i].Visible = true;
}
[/csharp]
The single exception I agree with is when you have a simple condition and need to set a property (one short line):
[csharp]
if (clients > 10) clientsPanel.PagingEnabled = true;
[/csharp]
There is one more way to use the braces, to separate the scope of a variable:
[csharp]
public void ShowClientPage(int pageNumber)
{
//here is the code for the left column
{
int items = 0;
//some processing goes here
leftColumn.Height = items * ITEM_HEIGHT_CONSTANT;
}

//here is the code for the right column
{
int items = 0;
//some processin goes here
RightColumn.Height = items * ITEM_HEIGHT_CONSTANT;
}
}
[/csharp]
In this example items is used both for the right and left columns calculations, and is separated into 2 scopes. There is no reason for doing that in this example, it is just for explaining the idea. And the idea is, it's bad practice. If you are thinking about writing that, you most probably have to extract the code into two methods.

Line spacing
One more important aspect is the spacing between your lines of code. It helps a lot when the code is separated into small logical blocks, and most of the time a look at the first line of this blocks is enough to give you an idea what this block is and if it is the one you're looking for.
[csharp]
public void Foo()
{
int visibleElementsCount = 0;
int selectedIndex = 0;
foreach (Element element in Elements)
{
if (element.Age > 0) visibleElementCount++;
if (element.IsDefault) selectedIndex
}
contractsListBox.SelectedIndex = selectedIndex;
}
[/csharp]
Now that is kind of heavy to comprehend with one look. Compare it to:
[csharp]
public void Foo()
{
int visibleElementsCount = 0;
int selectedIndex = 0;

foreach (Element element in Elements)
{
if (element.Age > 0) visibleElementCount++;
if (element.IsDefault) selectedIndex
}

contractsListBox.SelectedIndex = selectedIndex;
}
[/csharp]
I hope you got my point. Till next post ;)



blog comments powered by Disqus