HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Algorithm generating a unique ID based on client name

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
uniquealgorithmgeneratingclientnamebased

Problem

Following code is supposed generate a new ID (PID) for a new client based on the client name's first letter and the existing IDs in the range which are stored in a database.

For ex lets say if the new client's name is "Abc" and if last ID in the database starting from letter A, is A200. Then the new ID should be A201.

This code compiles and runs giving the desired out put but I hope this could be optimized further.

Expecting your suggestions on improving this...

```
private void GeneratePIDforNewPoint(string pointName)
{
string firstLetterOfPointName; // variable to hold first letter of "point name" being passed to this method
string lastExistingPID; // variable to hold the last PID (most recent hence largest) from the list of PIDs retrieved from the database
int lastDigitOfExistingPID; // variable to hold last digit of the "lastPID" (ex- digit '2' from PID 'F002')
string finalPID; // variable to hold out put (the generated new PID for the new point)...

firstLetterOfPointName = pointName[0]; // Get the fist letter of the point name...

List pidList=_ds.GetPIDs(firstLetterOfPointName); // Calling method to get the pid list from database

if (pidList.Count >=1) // At the end of this if block, numeric part of the new PID will be desided
{
pidList.Sort();
lastExistingPID = pidList[pidList.Count - 1];
lastDigitOfExistingPID = int.Parse(lastExistingPID[lastExistingPID.Length - 1].ToString());
}
else
{
lastDigitOfExistingPID = 0;
}

lastDigitOfExistingPID += 1;

// Found digit will be converted to the pid format by attaching the starting letter and zeros to make the length.(format --> A001/ B099 / C100 etc.)

finalPID= firstLetterOfPointName + String.Format("{0:000}", lastDigitOfExistingPID );
_view.PID = finalPID;
}

public List GetPIDs(string firstLetterOfPointName)
{
string selectStatement = @"SELECT PID FROM point WHERE PID like @PID";

Solution

First, I would expect from a function named GeneratePID to return the generated ID and not to set it. (Nobody likes side-effects)

private string GeneratePIDforNewPoint(string pointName)
{
    if(pointName == null)
        return "error";         

    var storedPIDs = _ds.GetPIDs(pointName[0]);
    var newPID = 0;        
    if(storedPIDs.Count > 0) {
        var maximumStoredPID = _ds.GetPIDs(pointName[0]).Max();
        newPID = Int32.Parse(maximumStoredPID.substring(1)) + 1;
    }
    return pointName[0] + String.Format("{0:000}", newPID); 
}

Code Snippets

private string GeneratePIDforNewPoint(string pointName)
{
    if(pointName == null)
        return "error";         

    var storedPIDs = _ds.GetPIDs(pointName[0]);
    var newPID = 0;        
    if(storedPIDs.Count > 0) {
        var maximumStoredPID = _ds.GetPIDs(pointName[0]).Max();
        newPID = Int32.Parse(maximumStoredPID.substring(1)) + 1;
    }
    return pointName[0] + String.Format("{0:000}", newPID); 
}

Context

StackExchange Code Review Q#48505, answer score: 4

Revisions (0)

No revisions yet.