Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Friends
I have a problem with summarizing this statement in 4 lines.
could anyone help me please?


private static string GetSize(long length)
        {
            if (length >= 1024 * 1024 * 1024)
                return string.Format("{0:f2} GB", (double)length / (1024.0 * 1024.0 * 1024.0));
            else if (length >= 1024 * 1024)
                return string.Format("{0:f2} MB", (double)length / (1024.0 * 1024.0));
            else if (length >= 1024)
                return string.Format("{0:f2} KB", (double)length / (1024.0));

            return string.Format("{0} Bytes", length);
        }



[Edited] Wraped code in pre tags[/Edited]
Posted
Updated 18-Dec-10 21:30pm
v2
Comments
Abhinav S 19-Dec-10 3:28am    
What exactly do you mean by 'summarizing' here?
MCY 19-Dec-10 12:35pm    
it is in a good balance of readability and length. why to disturb?

1 solution

I'm not sure what you mean by summarizing this, you could reduce the lines count by using the "?" operator, but at the expense of readablility / maintainability. For example:
return length >= 1024 * 1024 * 1024 ? string.Format("{0:f2} GB", (double)length / (1024.0 * 1024.0 * 1024.0)) : length >= 1024 * 1024 ? ...


Personally, I would leave it pretty much alone.
 
Share this answer
 
Comments
fjdiewornncalwe 19-Dec-10 11:50am    
Valid, but I must admit I would want to smack any dev in a code review if I saw something like this... I agree with you that the OP should leave the statement as is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900