Everyone needs an identity

Everyone needs an identity, that is how we identity them. Well, some need more than one…

identity_2002_20_thumb_thumb

The above still is from the movie Identity (the movie and this blog post title.. get it Secret telling smile). Well that sucked! Anways, its among my favorite movies. It is such a common thing that any one just probably just takes it for granted. But let me tell you, when 2 different “things” do not have their individual identites, well they are not any different to the naked, eye are they? Enough theatretical stuff,  here is a simple code snippet in C#:

public class Person
{
    public string Name{get;set;}
}

var p1 = new Person({Name = 'Frank Underwood'});
var p2 = new Person({Name = 'Frank Underwood'});

Now I understand the value equality vs identity equality case here, but is there any “identifier” I can use during debugging to let me know if I am dealing with p1 or p2? In C#, all objects are gifted with a method called “GetHashCode()”. This method returns a unique value for each object across all classes in your application. This number is the identifier of an object that shouts his identity, and it is mighty useful during debugging when you have bunch of objects flying around in callbacks and event handlers. It would be nice to have something like this in Javascript where things are already so “simple”. (Really Javascript is so simple and has so much less to learn, but after working with it for some time makes us realize that is the only thing that is simple.) So a similar snippet in Javascript:

var Person = function(){
    this.name = null;
};

var p1 = new Person();
p1.name = 'Frank Underwood';
var p2 = new Person();
p2.name = 'Frank Underwood';

function whoAreYou(person){
    // How do we know if here we have a p1 or p2???
}

Recently while working on a project I had issues where I was using an event library (EventEmitter of node precisely). I had a constructor function Subject that was acting as an event source. There was another constructor function Observer, that listened to those events. The scene was to have a single subject and a single observer specific to it. When I had multiple of these objects interacting with each other, shit started happening when multiple instance of Observer were listening to a single Subject. When there Javascript and shit close to one another in a paragraph more often that not its got to do with the “this” pointer. What helped me come to that conclusion was a way to identify an instance of Subject I am debugging is indeed the one I intend to be. This line of code did the trick for me:

var Person = function(){
    this.name = null;
    this.hashCode = Math.random(); // Identity!
};

Now when I debug, I tracked the objects I needed with this unique hashCode number. Sure those numbers are floats and are hard to remember but they get the job done as I see it.

Abhang Rane


9 comments :

Reader's Comments

  1. Good work…unique site and interesting too… keep it up…looking forward for more updates.Good luck to all of you and thanks so much for your hard-work.
    Language Interpretation Services, Voice Over Services India

    ReplyDelete
  2. Boost your data skills with a tableau course online
    designed for beginners and professionals alike. Learn to create interactive dashboards and insightful visualizations from anywhere, at your own pace.

    ReplyDelete
  3. salesforce administrator classes
    provide comprehensive training to manage and optimize Salesforce platforms efficiently. These classes equip learners with practical skills in user management, automation, and reporting.

    ReplyDelete
  4. Enroll in power bi classes online
    to master data visualization, reporting, and analytics from anywhere at your convenience. These Power BI classes online provide hands-on exercises and expert guidance to boost your business intelligence skills.

    ReplyDelete
  5. "Great post! Our ui ux design course
    teaches practical design principles, user research, and hands-on projects to create engaging digital experiences."

    ReplyDelete
  6. Android app development training provides strong practical exposure to building mobile applications. It explains Android components and lifecycle clearly. This android app development training improves coding confidence. Students create real-world mobile apps. Firebase and SQLite integration are included. Web service integration is covered. App deployment guidance is provided. It prepares skilled Android developers.

    ReplyDelete
  7. ⭐ Workday HCM Course
    A comprehensive workday hcm course covers end-to-end HR lifecycle management.
    You learn staffing models, compensation, and benefits configuration.
    Live sessions ensure interactive and practical learning.
    Real-time case studies enhance hands-on experience.
    Mentor support helps clarify complex modules.
    Recorded sessions allow easy revision anytime.
    This course builds strong confidence for Workday interviews.

    ReplyDelete
  8. Django online classes provide interactive instructor-led sessions. They simplify backend development concepts. These django online classes improve coding confidence. Students complete practical assignments. Project implementation is included. Database handling is covered. Deployment support is explained. They prepare professional developers.

    ReplyDelete
  9. "Very informative! Master training boomi
    with hands-on labs covering data transformation, error handling, and cloud integration strategies designed for career growth."

    ReplyDelete