3
\$\begingroup\$

How can I improve this code?

public class Address {     
   public string house_no { get; set; }     
   public string street { get; set; }     
   public string zip { get; set; }
}
public class Contact {     
   public string email { get; set; }     
   public string ph_no { get; set; }     
   public Address address { get; set; } 
}

//Test program  
class Program {
    static void GetContacts(string input)   {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Dictionary<string, Contact> ContactList = serializer.Deserialize<Dictionary<string, Contact>>(input);        
    }

    static void Main(string[] args)     {  
        //jsonText is a string that I retrieve from a web service. 
        string jsonText = ws.GetContactList();
        GetContacts(jsonText);
    } 
} 
\$\endgroup\$
8
\$\begingroup\$

It is already short and to the point. One minor formatting change; I would make your class properties upper camel case, which is typical for C#. I also dropped the abbreviations.

public class Address {     
   public string HouseNumber { get; set; }     
   public string Street { get; set; }     
   public string Zip { get; set; }
}

public class Contact {     
   public string Email { get; set; }     
   public string PhoneNumber { get; set; }     
   public Address Address { get; set; } 
}
\$\endgroup\$
5
  • \$\begingroup\$ It is subjective point. Lower case is regular for javascript, that's probably why they are named in lower camel case. \$\endgroup\$
    – Snowbear
    Mar 17 '11 at 20:22
  • \$\begingroup\$ Snowbear is right. They are lowercase so that they are consistent with the JSON that is coming in. \$\endgroup\$
    – noobDotNet
    Mar 17 '11 at 20:31
  • \$\begingroup\$ That's PascalCase, but otherwise, yeah. :) \$\endgroup\$
    – Noldorin
    Mar 20 '11 at 19:12
  • \$\begingroup\$ @Noldorin PascalCase == UpperCamelCase \$\endgroup\$
    – Skeeterbug
    Mar 21 '11 at 2:45
  • \$\begingroup\$ Not in the Microsoft book. ;) \$\endgroup\$
    – Noldorin
    Mar 21 '11 at 17:41
5
\$\begingroup\$

There is nothing to actually review here so let's start nit-picking process:

1) I would use var for contacts. Anyway you already have Dictionary<...> at the same line. Var makes it little bit less noisy.
2) Lower camel case for variable Contacts name.

\$\endgroup\$
0
\$\begingroup\$

Since Skeeterburg and Snowbear have already covered the basics, the only change I'd reccomend is changing address and contact from classes to structs. You aren't doing anything by reference and you don't need mutable objects. It could clean up a bit since you don't need the getters and setters. Structs (IMO) are particularly useful for this type of JSON passing.

There was a good discussion on SO about this a while back: https://stackoverflow.com/questions/85553/when-should-i-use-a-struct-instead-of-a-class

Also: since the struct is created on the stack, you do get a bit of a performance gain. http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.