Far Away Developer

Sebastien Lachance

Archive for October, 2007

Law of Demeter

Posted by Sebastien Lachance on October 26, 2007

Talk only to your close friends. This is a object-oriented design guidelines. This law state that your object should know as little as possible of others objects. You may also know this by the name of “Principle of Least Knowledge”.

I have found that this guidelines has helped me a lot in my development. The more decoupled your object is, the easier it will be to make changes in your design.

I strongly suggest that you apply this law. You’ll thank me later.

Posted in Agile, Design | No Comments »

Bookmarks of the week 2

Posted by Sebastien Lachance on October 26, 2007

Posted in Fun | 1 Comment »

Book review : Head First Design Patterns

Posted by Sebastien Lachance on October 23, 2007

My first of 12 books I’ve committed myself to read : Head First Design Patterns.

This is the second time I’ve read that book, and quite enjoyed it again. As the title say, it’s a book about design patterns. The patterns explored are :

  • Strategy
  • Observer
  • Decorator
  • Simple Factory
  • Factory Method
  • Abstract Factory
  • Singleton
  • Command
  • Adapter
  • Template Method
  • Iterator
  • Composite
  • State
  • Proxy
  • Model-View-Controller

Every pattern listed here is explained with an example of the real world. Not exactly as real as ours but it’s different from what we’ve seen on other books.

If you’ve never read a Head First book, you will be surprised. The book is focused on interaction with the reader. At the end of some chapters, there is crosswords for us to do, forcing us to re-read the entire chapter trying to find the answers. There is also some place where you need to answers questions or writing class diagrams. You need to think a lot and it’s a very good way to learn fast. The paragraphs are short and jokes are scattered all over the place. A very nice feature is that you have handwritten text everywhere, pointing out important stuff. If you happen to see incoherence in the text, be prepared to have a section clarifying all that (with a conversation between two patterns or a reader’s question).

But it’s not all about design patterns. They go through the basics and principles of Object-Oriented programming such as the Dependency Inversion Principle.

Well the book is about learning differently. And ca be a quick-start for beginners.

Have I learned something off this book? Of course! I have a lot more knowledge about design patterns now, even if I had previously read the Gang of Four pattern book. I can explain each one of the design patterns explored and recognize it in other’s code. I recommend it seriously to anyone who wants to start applying design patterns and learning about Object-Oriented principles.

Here is a nice preview of the book : Chapter 3 : Decorator.

Posted in Book review | 1 Comment »

Visual Studio 2005 : Reference Path

Posted by Sebastien Lachance on October 15, 2007

When I came across this feature, my world changed forever. When you are in a multi-user scenario and your assemblies are stored in a different folder from your peer, then you have this solution. Suppose you get the latest version from the project you are working on and the reference to AssemblyA is broken because the path of the assembly is C:\dev\lib. But you know that you have it in a folder somewhere else and you don’t want to duplicate that assembly. Then the reference path will come to the rescue. Adding a reference path will add a place that will be looked when checking for references. This is how you do it :

Step 1. In the solution explorer, right-click on the project with a broken reference.

Step 2. Click Reference Paths tab

Step 3. Enter the folder in which your assembly is located.

Step 4. Click the Add Folder button.

This is what it should look like :

Reference

Notice that you can also Update and Delete reference in this page.

Posted in .NET, Visual Studio | No Comments »

Visual Studio 2005 : Adding projects to your solution

Posted by Sebastien Lachance on October 11, 2007

There is 2 ways :

1. Begin by opening the New Project dialog box : File/New/Project… (or Ctrl-Shift-N). Navigate to the type of project you want. In the solution dropdown, choose Add to Solution. Failure to do that will result in the creation of a new solution. Enter the name of our project and click the OK button.

AddToSolution

2. In the Solution Explorer (Ctrl-Alt-L), right-click on the solution node and choose Add/New Project…

AddNewProject2

This time you don’t need to choose anything in the dropdown. Enter the name of the project and click the OK button.

Each one of those 2 methods will a a new folder with the name you entered. Inside the folder you will find a project file (.csproj or .vbproj) and a .cs (or .vb). By default Visual Studio will generate a default file for the type of project you choose).

Let’s look at the project file.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.50727</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>c50693b9-547b-49f0-8850-e3c0a3b0325c</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ClassLibrary1</RootNamespace>
    <AssemblyName>ClassLibrary1</AssemblyName>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

As you can see, it’s an xml file. It contains informations about the assembly name, the output type, namespace, version of Visual Studio used to create the project, etc. And it also contains the information used to compile. This is a whole topic so we will not cover it now. Right under we have the reference used by the project and the files it contains . You do not need to remember all that, the important thing is to know what is the purpose of the file. And finally, if you decide to use SourceSafe, all binding information will be added to this file.

Posted in .NET, Visual Studio | 3 Comments »

Visual Studio 2005 : Starting a solution correctly

Posted by Sebastien Lachance on October 10, 2007

I’m always wondering about what should I be blogging today. But not today. I want to give back some of the stuff I’ve learned in the past years. The question was : I’m starting with what? After some time, it struck me right in the face. I know that some people would prefer using notepad over Visual Studio, but I’m not there to discuss that. I found that mastering an editor is a great to get started and will allow you more time to code and less time to wonder where is the option that will highlight the file you are in. So the first posts will be about Visual Studio 2005. And the very first is about how to start a solution correctly.

Why?

Given the number of people I helped with that, I think this is something you should know. You should not start be creating a project. Doing that will lead you to a messy folder hierarchy and a wrong solution name.

When?

Everytime you start a solution.

How?

You start by clicking File\New\Project… or Ctrl-Shift-N.

FileNewProject

In tree on the left you need to go to the Other Project Types node and select Visual Studio Solutions. You now have the choice of selecting the Blank Solution item. You should notice that the checkbox for the Create directory for solution is grayed out, this means that a directory will be created for the solution. In our case, it will be : C:\dev\dotnet\Solution1. This folder will also contains a Solution1.sln and Solution1.suo. This is exactly what we want, every project you will add will be placed in this folder. The hierarchy you see in the Solution Explorer will be the same as your solution.

BlankSolution

.sln file

This is the solution file that Visual Studio will use to load all project associated with the solution. As well as other options that does not need to be covered now.

.suo file

This binary file contains information about what files were currently in edition, collapsed nodes, etc. When you close and reopen Visual Studio 2005, it will read this file to replace it the way it was before.

Posted in .NET, Visual Studio | No Comments »