Kgn-web
Kgn-web

Reputation: 7555

Looking for better understanding on the coding standards

I installed CodeCracker

This is my original method.

//Add
public bool AddItemToMenu(MenuMapper mapperObj)
{
    using (fb_databaseContext entities = new fb_databaseContext())
    {
        try
        {
            FoodItem newItem = new FoodItem();
            newItem.ItemCategoryID = mapperObj.ItemCategory;
            newItem.ItemName = mapperObj.ItemName;
            newItem.ItemNameInHindi = mapperObj.ItemNameinHindi;
            entities.FoodItems.Add(newItem);
            entities.SaveChanges();

            return true;
        }
        catch (Exception ex)
        {
            //handle exception
            return false;
        }
    }
}

This is the recommended method by CodeCracker.

public static bool AddItemToMenu(MenuMapper mapperObj)
{
    using (fb_databaseContext entities = new fb_databaseContext())
    {
        try
        {
            var newItem = new FoodItem
            {
                ItemCategoryID = mapperObj.ItemCategory,
                ItemName = mapperObj.ItemName,
                ItemNameInHindi = mapperObj.ItemNameinHindi,
            };

            entities.FoodItems.Add(newItem);
            entities.SaveChanges();

            return true;
        }
        catch (Exception ex)
        {
            //handle exception
            return false;
        }
    }
}

I am very curios to get these answer, as it can guide me in a long way.

Adding one more method:-

private string GeneratePaymentHash(OrderDetailMapper order)
{
    var payuBizzString = string.Empty;

    payuBizzString = "hello|" + order.OrderID + "|" + order.TotalAmount + "|FoodToken|" + order.CustomerName + "|[email protected]|||||||||||10000";
    var sha1 = System.Security.Cryptography.SHA512Managed.Create();

    var inputBytes = Encoding.ASCII.GetBytes(payuBizzString);
    var hash = sha1.ComputeHash(inputBytes);

    var sb = new StringBuilder();
    for (var i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString().ToLower();
}

Upvotes: 0

Views: 60

Answers (5)

Petter Hesselberg
Petter Hesselberg

Reputation: 5498

As far as I know Static methods occupy memory when the application intialize irrespective if they are called or not.

Methods that are never called may or may not be optimized away, depending on the compiler, debug vs. release and such. Static vs. non-static does not matter.

A method that doesn't need a this reference can (and IMO should) be static.

When I already know the return type then why should I use var keyword

No reason. There's no difference; do whatever you prefer.

Why this way of Object intializer is better.

The object initializer syntax generates the same code for most practical purposes (see answer @SonerGönül for the details). Mostly it's a matter of preference -- personally I find the object initializer syntax easier to read and maintain.

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

As far as I know Static methods occupy memory when the application initialize irrespective if they are called or not.

This is true for all kind of methods, so that's irrelevant.

When I already know the return type then why should I use var keyword.

var is a personal preference (which is a syntactic sugar). This analyzer might think since the return type is already known, there is no need to use type explicitly, so, I recommend to use var instead. Personaly, I use var as much as possible. For this issue, you might wanna read Use of var keyword in C#

Why this way of Object intializer is better.

I can't say object initializer is always better but object initialize supplies that either your newItem will be null or it's fully initialized since your;

var newItem = new FoodItem
{
    ItemCategoryID = mapperObj.ItemCategory,
    ItemName = mapperObj.ItemName,
    ItemNameInHindi = mapperObj.ItemNameinHindi,
};

is actually equal to

var temp = new FoodItem();
newItem.ItemCategoryID = mapperObj.ItemCategory;
newItem.ItemName = mapperObj.ItemName;
newItem.ItemNameInHindi = mapperObj.ItemNameinHindi;
var newItem = temp;

so, this is not the same as your first one. There is a nice answer on Code Review about this subject. https://codereview.stackexchange.com/a/4330/6136 Also you might wanna check: http://community.bartdesmet.net/blogs/bart/archive/2007/11/22/c-3-0-object-initializers-revisited.aspx

Upvotes: 1

Soviut
Soviut

Reputation: 91555

  1. Static methods don't occupy any more memory than instance methods. Additionally, your method should be static because it doesn't rely in any way on accessing itself (this) as an instance.

  2. Using var is most likely for readability. var is always only 3 letters while many types are much longer and can force the name of the variable much further along the line.

  3. The object initializer is, again, most likely for readability by not having the variable name prefix all the attributes. It also means all your assignments are done at once.

In most cases, this tool you're using seems to be about making code more readable and clean. There may be certain cases where changes will boost performance by hinting to the compiler about your intentions, but generally, this is about being able to understand the code at a glance.

Only concern yourself with performance if you're actually experiencing performance issues. If you are experiencing performance issues then use some profiling tools to measure your application performance and find out which parts of your code are running slowly.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

As far as I know Static methods occupy memory when the application intialize irrespective if they are called or not.

All methods do that. You are probably confusing this with static fields, which occupy memory even when no instances of the class are created. Generally, if a method can be made static, it should be made static, except when it is an implementation of an interface.

When I already know the return type then why should I use var keyword.

To avoid specifying the type twice on the same line of code.

Why this way of Object intializer is better?

Because it groups the assignments visually, and reduces the clutter around them, making it easier to read.

Upvotes: 4

Kevin Smith
Kevin Smith

Reputation: 14436

A lot of these are personal preferences but most coding standards allow other programmers to read your code easier.

Changing the static method to an instance takes more advantage of OO concepts, it limits the amount of mixed state and also allows you to add interfaces so you can mock out the class for testing.

The var keyword is still statically typed but because we should concentrate on naming and giving our objects more meaningful so explicitly declaring the type becomes redundant.

As for the object initialisation this just groups everything that is required to setup the object. Just makes it a little easier to read.

Upvotes: 0

Related Questions