Friday, May 25, 2012

Primitive Obsession:I will design all from scratch!

This is one of the primary tendency of every fresher developer or programmer, I my self is not out of the boundary, After starting the programming I was also kind of had the same tendency, I didn’t have much idea about the vast class library and functionality the c++, java or c# has to offer, rather I used the basic and primitive data structure and ideas and techniques that I learned while coding c.

This simple ignorance is some time called “Primitive Obsession”.

Here is what “codinghorror” has to say about “Primitive Obsession”,

“Don't use a gaggle of primitive data type variables as a poor man's substitute for a class. If your data type is sufficiently complex, write a class to represent it. ”

Code that has primitive obsession has the following phenomena 

  • use of primitives data type (like integers or strings) for solving complex problem.
  • use of low-level methods to perform operation on data.

Eventually we loose a higher level of abstraction.

One simple example in c# could be build a list of objects, we could build a class to keep a specific type of data and then expose different methods and properties to support the class,

Or

We can use a List<T> to keep the object, where what ever we need from a list is there. I think you got the idea.

References

Friday, May 18, 2012

Oddball Solution: some one does the same thing in different way.

In this section we would take a look at oddball solution code smell. So what is a odd ball solution, if one problem is solved in one way throughout a system and the same problem is solved in another way in the same system in some cases, one of the solutions is oddball solution.

This particular smell is also known as Inconsistent Solution.

This happens specially in case of algorithms, different algorithm or different version of the same algorithm is been used several places which creates inconsistency and duplicate code.

How odd ball solution get in to the system?

There are two obvious reason for this,

  • Ignorance of how a solution is implemented elsewhere in a system.
  • Not spending enough time refactoring code to use a consistent solution.

 

How to get rid of odd ball solution?

The Simple process to get rid of oddball solution is to use extract method and use same method and use same algorithm all over the system.

While picking the right solutions for a problem we must consider which solution is been used majority of time then decide if this solution is better than the one is been used minority times. Compare and then keep the best one and eliminate the other one.

Thursday, May 10, 2012

Lazy Class : Does not have any purpose in the Matrix.

There is a very nice saying in the movie matrix, every thing is the matrix has purpose, other wise its been deleted by the agents. Believe we also create purpose less classes in our projects. as we all can guess the purpose of this discussion is to take a small note on “Lazy Class”

Here is what Wikipedia has to say about Lazy Class.

“A class that does too little”

This particular code smell is also known as freeloader. A Lazy class does not start from the beginning, And had definite purpose, but after some move method and refactor the class gets so smaller in size and the minor functionality that it has can be offered by another more meaning full class, or perhaps it already offered by some one other class already. So it end up with doing nothing at all.

So we don’t feel pity about it and can delete the lazy class.

How to eliminate Lazy Class?

Use “Collapse Hierarchy” or “Inline Class” to eliminate this code smell.

Thursday, May 3, 2012

Large Class : The making of code smell history.

In this particular short discussion we are going to take a good look at “Large Class” code smell, and some obvious reason for creating one in any project. And then we would discuss some easy way to eliminate the large class code smells.

“Large class: a class that has grown too large”

This is what Wikipedia has to say about large class. And the statement is so true, as no class is a large class at the beginning, but as time passes some how it started to grow big and big and eventually end up with a unmanageable situation.

Who a large class?

  1. A class do too many work
  2. A class has too many methods and members both private and public and they used for versatile purpose.

In a large class we tend to add more functionally, when we are unsure where to put them, in this way more and more unsure stuff gets added and eventually got spoiled. Just like large method large class is very hard to manage and understand, a class has too many responsibility and do too many work all by it self, where as it could be separated in few smaller classes or perhaps could be refactored in such a way so that they do violate “Single responsibility principle”.

How to eliminate large class?

First of all we have to separate the responsibility and identify the set which belong to one group, and create another class to which has one single responsibility and using “move method” refactor technique move the related field and method to another class.

Follow the above process until all the responsibility is been distributed to other class.

Sunday, April 29, 2012

How to disable Yes/No security message of visual studio 2010?

I was not sure about what the heading will be so just put down a rubbish title, if you still not sure about what is going on header the bellow bold paragraphs,

“The current project settings specify that the project will be debugged with specific security permissions.  In this mode, command line arguments will not be passed to the executable.  Do you want to continue debugging anyway?”

Still not sure, ever seen a message like bellow?

image

Ah whey the hell you saw a message like this? simple you have put something in the command line argument, And where its been added, its added on the project properties.

image

Now then, how to get rid of that, the solution is also simple, you got to disable the click once security, how a screen shot is given bellow for your help. If the Check box is enabled by default, just disable it, you wont see that message again.

image 

Note & Disclaimer: I have no idea what will happen after that, I have tested works fine, but in deployment you may see some problem.

Until next time.

Thursday, April 26, 2012

How to make any object selectable in User interface so that it can be bind to Treeview, checkbox list or radio button list?

Today I am going to share a simple trick to make any object selectable in User interface so that it can be bind to Treeview, checkbox list or radio button list. The trick is simple, we have to warp the user custom class with another class named “SelectableData”.

Bellow the code segment is given

public class SelectableData<T>:ANotifyPropertyChanged
{
public SelectableData()
{
_isEnabled = true;
}

private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}

private T _data;
protected bool _isEnabled;

public T Data
{
get { return _data; }
set
{
_data = value;
OnPropertyChanged("Data");
}
}

public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
OnPropertyChanged("IsEnabled");
}
}
}


Bellow the uses is given.


private void GenerateSelectableOption(string[] selectedOptions)
{
string[] options = new string[] {"Data", "Data1"};

var selectableOptions = new List<SelectableData<string>>();
foreach (var option in options)
{
selectableOptions.Add(new SelectableData<string>
{
Data = option,
IsSelected = selectedOptions.Any(o => o.Equals(option))
});
}

SelectableOptions = selectableOptions;
}

public List<SelectableData<string>> SelectableOptions
{
get { return (List<SelectableData<string>>)GetValue(SelectableOptionsProperty); }
set { SetValue(SelectableOptionsProperty, value); }
}

public static readonly DependencyProperty SelectableOptionsProperty =
DependencyProperty.Register("SelectableOptions", typeof(List<SelectableData<string>>),
typeof(OptionsEditorPresenter), new UIPropertyMetadata(null));


And the Xaml Binding Code is given bellow


<UserControl x:Class="OptionsEditorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="200">
<UserControl.Resources>
<DataTemplate x:Key="OptionsCheckboxTemplate" >
<CheckBox Content="{Binding Data}" IsChecked="{Binding IsSelected}" Margin="0 0 5 0"/>
</DataTemplate>
<DataTemplate x:Key="OptionsRadioButtonTemplate" >
<RadioButton GroupName="options" Content="{Binding Data}" IsChecked="{Binding IsSelected}" Margin="0 0 5 0"/>
</DataTemplate>
<Style x:Key="ItemControlStyle" TargetType="{x:Type ItemsControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Multiselect}" Value="False">
<Setter Property="ItemTemplate" Value="{StaticResource OptionsRadioButtonTemplate}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Multiselect}" Value="True">
<Setter Property="ItemTemplate" Value="{StaticResource OptionsCheckboxTemplate}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding SelectableOptions}" Style="{StaticResource ItemControlStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>


Hope this helps, until next time.