Thursday, July 26, 2012

Easiest way to find how good is your software team

The Joel Test
  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. Do you have a bug database?
  5. Do you fix bugs before writing new code?
  6. Do you have an up-to-date schedule?
  7. Do you have a spec?
  8. Do programmers have quiet working conditions?
  9. Do you use the best tools money can buy?
  10. Do you have testers?
  11. Do new candidates write code during their interview?
  12. Do you do hallway usability testing?


The neat thing about The Joel Test is that it's easy to get a quick yes or no to each question. You don't have to figure out lines-of-code-per-day or average-bugs-per-inflection-point. Give your team 1 point for each "yes" answer. The bummer about The Joel Test is that you really shouldn't use it to make sure that your nuclear power plant software is safe.

A score of 12 is perfect, 11 is tolerable, but 10 or lower and you've got serious problems. The truth is that most software organizations are running with a score of 2 or 3, and they need serious help, because companies like Microsoft run at 12 full-time.

Of course, these are not the only factors that determine success or failure: in particular, if you have a great software team working on a product that nobody wants, well, people aren't going to want it. And it's possible to imagine a team of "gunslingers" that doesn't do any of this stuff that still manages to produce incredible software that changes the world. But, all else being equal, if you get these 12 things right, you'll have a disciplined team that can consistently deliver.

Thursday, July 19, 2012

T-SQL RANK,Row_Number,NTile and Dense_Rank

Rank():
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
Usage: RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
Dense_Rank() :
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
Usage: DENSE_RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
Ntile():
Distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.
Usage: NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > ) 
Row_Number():
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> ) 
Example 

All Recordset :  
select * from finals 
RecordID Name Surname Course Point
1 Bugra Postaci Mathematics 89
2 Bugra Postaci Lecture 90
3 Dany Lowe Mathematics 75
4 Dany Lowe Lecture 85
5 Alice Marcel Mathematics 77
6 Alice Marcel Lecture 100
7 Simon Duru Mathematics 45
8 Simon Duru Lecture 58

select row_number() over(order by RecordID desc) as RowNumber ,* from finals 

RowNumber RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
2 7 Simon Duru Mathematics 45
3 6 Alice Marcel Lecture 100
4 5 Alice Marcel Mathematics 77
5 4 Dany Lowe Lecture 85
6 3 Dany Lowe Mathematics 75
7 2 Bugra Postaci Lecture 90
8 1 Bugra Postaci Mathematics 89

select ntile(2) over(order by RecordID desc) as [Ntile] ,* from finals 

Ntile RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
1 7 Simon Duru Mathematics 45
1 6 Alice Marcel Lecture 100
1 5 Alice Marcel Mathematics 77
2 4 Dany Lowe Lecture 85
2 3 Dany Lowe Mathematics 75
2 2 Bugra Postaci Lecture 90
2 1 Bugra Postaci Mathematics 89

as you see ntile function just divide the scope with given number 
select rank() over (order by Surname) as [Rank], * from finals 
Rank RecordID Name Surname Course Point
1 7 Simon Duru Mathematics 45
1 8 Simon Duru Lecture 58
3 3 Dany Lowe Mathematics 75
3 4 Dany Lowe Lecture 85
5 5 Alice Marcel Mathematics 77
5 6 Alice Marcel Lecture 100
7 1 Bugra Postaci Mathematics 89
7 2 Bugra Postaci Lecture 90

rank function is working like  match one ”Duru” as 1 match another “Duru” as 1 but total count as 2 , next surname matches “Lowe” as count +1 as 3 , another “Lowe” as 3  ; now total count is 4 matching next surname as “Marcel” is count + 1 as 5 and goes on …. 

select dense_rank() over (order by Surname) as [DenseRank], * from finals 

DenseRank RecordID Name Surname Course Point
1 7 Simon Duru Mathematics 45
1 8 Simon Duru Lecture 58
2 3 Dany Lowe Mathematics 75
2 4 Dany Lowe Lecture 85
3 5 Alice Marcel Mathematics 77
3 6 Alice Marcel Lecture 100
4 1 Bugra Postaci Mathematics 89
4 2 Bugra Postaci Lecture 90
 Dense rank its obvious like shown. 

if you want to make your denserank number reseting by groups there is a way use PARTITION BY
below example reseting ranks by using “partition by” by course column
select
dense_rank() over (Partition by course order by Surname) as [DenseRank], * from finals 
 

DenseRank RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
2 4 Dany Lowe Lecture 85
3 6 Alice Marcel Lecture 100
4 2 Bugra Postaci Lecture 90
1 7 Simon Duru Mathematics 45
2 3 Dany Lowe Mathematics 75
3 5 Alice Marcel Mathematics 77
4 1 Bugra Postaci Mathematics 89
Thats all folks…


Original Article: http://blog.bugrapostaci.com/2010/01/26/t-sql-rank-dense_rank-ntile-row_number/

Friday, July 6, 2012

Reading content of a locked File C# .Net

Sometimes it might get handy to read some file that may remain in use for very long time by other processes, one possible example of such process involves log files they are mostly locked by their parent processes and sometimes we need to read the content of file without stopping the main process. There could be two possible ways to read an locked file(locked means some other process have exclusive access to that file)

1. Create a Copy of that file using Snapshot copy of volume of course this option is not the optimal solution in case of large files and even snapshot copy is an easy task that can be managed through .net code easily.


2. Other option that i came across and works for me is by opening file in read/shared mode. You can use this method specially with log4net which don't acquire lock on file for a very long time.

FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);

while (!logFileReader.EndOfStream)
{
    string line = logFileReader.ReadLine();
    Console.WriteLine(line);
}

// Clean up
logFileReader.Close();
logFileStream.Close();
 
 

Creating And Setting Custom Principal

Authenticating a user internaly sets SecurityPrincipal Object at Thread level this patteren is used almost in every .net application e.g Web Application/Web Services/ Desktop applications and even Console Applications. What i needed was to set a custom defined object as security principal so that i can access additional parameters from it. This is what i came accross :

This is how i can get/set principal object using a Factory classs.

PrincipalFactory pf = new PrincipalFactory();
            pf.SetCurrentUser(new User() {UserID=1,Name="asasd" });
            var user = pf.GetCurrentUser();



Factory Class Code:

 public class PrincipalFactory
    {
        public MyCustomPrincipal GetCurrent()
        {
            return Thread.CurrentPrincipal as MyCustomPrincipal;
        }

        public User GetCurrentUser()
        {
            var principal = GetCurrent();
            return (principal != null ? principal.User : null);
        }

        public void SetCurrentUser(User user)
        {
            Thread.CurrentPrincipal = new MyCustomPrincipal(user);
        }
    }




My Custom Object that i want to be saved in Principle

 public class User
    {
        public int UserID { get; set; }
        public string Name { get; set; }
    }


Finally this is my Custom Principal that implements IPrincipal interface
 public class MyCustomPrincipal:IPrincipal
    {
        public MyCustomPrincipal(User user)
        {
            this.User = user;
        }
        public User User { get; set; }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get { return WindowsIdentity.GetCurrent(); }
        }

        public bool IsInRole(string role)
        {
            return true;
        }

        #endregion
    }