Click here to Skip to main content
15,891,423 members
Home / Discussions / C#
   

C#

 
QuestionWhat's special with message box? Pin
teknolog1237-Oct-09 6:56
teknolog1237-Oct-09 6:56 
AnswerRe: What's special with message box? Pin
PIEBALDconsult7-Oct-09 7:27
mvePIEBALDconsult7-Oct-09 7:27 
GeneralRe: What's special with message box? [modified] Pin
teknolog1237-Oct-09 7:37
teknolog1237-Oct-09 7:37 
GeneralRe: What's special with message box? Pin
LimitedAtonement7-Oct-09 7:42
LimitedAtonement7-Oct-09 7:42 
GeneralRe: What's special with message box? Pin
teknolog1237-Oct-09 8:09
teknolog1237-Oct-09 8:09 
GeneralRe: What's special with message box? Pin
PIEBALDconsult7-Oct-09 7:42
mvePIEBALDconsult7-Oct-09 7:42 
QuestionAnother method safer than StreamWriter and how can I save the file on another server (asp.net c sharp) Pin
aspkiddy7-Oct-09 6:32
aspkiddy7-Oct-09 6:32 
QuestionCode Readability Poll Pin
LimitedAtonement7-Oct-09 5:57
LimitedAtonement7-Oct-09 5:57 
Dear Sirs,

I just came across some code and changed it. I'm implementing the java Gridbag layout manager to a panel in C#, and in the Insets.Equals(object) code, the following could be found:

if (obj instanceof Insets) {
	    Insets insets = (Insets)obj;
	    return ((top == insets.top) && (left == insets.left) &&
		    (bottom == insets.bottom) && (right == insets.right));
	}
return false;

So I sharpified it:
if (obj is Insets)
{
    Insets insets = obj as Insets;
    return ((top == insets.top) && (left == insets.left) && (bottom == insets.bottom) && (right == insets.right));
}
return false;    

Then, almost compulsively, I modified it thus:
Insets insets = obj as Insets;
if (ReferenceEquals(insets, null)) return false;
return ((top == insets.top) && (left == insets.left) && (bottom == insets.bottom) && (right == insets.right));

I do this kind of thing ALL the time. I would like some comments on readability and efficiency.

I guess to characterize the modification, I would say that I make it read more sequentially, without having to skip code (casting your eyes over code because in certain cases it would not be executed). Also, I'm crazy about conserving lines. I suppose that's because I'm not paid per line Wink | ;) . I always use the same-line `if' if I can, and if not, I resort to the no-brace 'if'. Which sometimes means...oh yeah, let me show a line I wrote the other day. Here's what I first wrote:
foreach (Point a in _gr_pts)
{
    if (prev == Point.Empty || prev == a)
    {
        prev = a;
        continue;
    }
    g.DrawArc(_ap_pen, prev, a);
    prev = a;
}

Then, I changed it to:
foreach (Point a in _gr_pts)
{
    if (prev == Point.Empty || prev == a)
    {
        prev =a;
        continue;
    }
    //Crazy, I know.
    g.DrawLine(_ap_pen, prev, prev = a);
}

I tried for a while to figure out how to get the braces out of the if statement...oh yeah, I just realized that this is possible:
(I'm writing free-hand, so there may be typos...)
foreach (Point a in _gr_pts)
    if (prev == Point.Empty || prev == a)
    {
        prev = a;
        continue;
    }
    else g.DrawLine(_ap_pen, prev, prev = a);


I know some people swear by ALWAYS using braces in if, else, do, while, for, foreach statements, (and if I remember correctly, Visual studio has some option about including those compulsively) and I am a little curious if any of you are that way, or how many would do what I do.

To sum up, some items on which to comment:
I cast the object first without asking about the cast. I use ReferenceEquals, not `=='. I return false inline with the if-statement; I don't use else.

When I say things like `I don't use else,' I don't mean all the time, but I don't use it if I don't need it. Does anyone know if this affects effeciency in any way? I suppose I'm too lazy to check out the disassembly (because it's always SOO confusing Frown | :( ).

I look forward to hearing back from you, my esteemed colleagues.

In Christ,
Aaron Laws
AnswerRe: Code Readability Poll Pin
Kevin Marois7-Oct-09 6:34
professionalKevin Marois7-Oct-09 6:34 
GeneralRe: Code Readability Poll Pin
N a v a n e e t h7-Oct-09 7:38
N a v a n e e t h7-Oct-09 7:38 
GeneralRe: Code Readability Poll Pin
PIEBALDconsult7-Oct-09 8:22
mvePIEBALDconsult7-Oct-09 8:22 
GeneralRe: Code Readability Poll Pin
Gideon Engelberth7-Oct-09 9:14
Gideon Engelberth7-Oct-09 9:14 
GeneralRe: Code Readability Poll Pin
PIEBALDconsult7-Oct-09 13:49
mvePIEBALDconsult7-Oct-09 13:49 
AnswerRe: Code Readability Poll Pin
Luc Pattyn7-Oct-09 6:40
sitebuilderLuc Pattyn7-Oct-09 6:40 
GeneralRe: Code Readability Poll Pin
Ennis Ray Lynch, Jr.7-Oct-09 8:59
Ennis Ray Lynch, Jr.7-Oct-09 8:59 
GeneralRe: Code Readability Poll Pin
Luc Pattyn7-Oct-09 9:14
sitebuilderLuc Pattyn7-Oct-09 9:14 
GeneralRe: Code Readability Poll Pin
PIEBALDconsult7-Oct-09 12:58
mvePIEBALDconsult7-Oct-09 12:58 
AnswerRe: Code Readability Poll Pin
N a v a n e e t h7-Oct-09 6:42
N a v a n e e t h7-Oct-09 6:42 
GeneralRe: Code Readability Poll Pin
PIEBALDconsult7-Oct-09 7:32
mvePIEBALDconsult7-Oct-09 7:32 
GeneralRe: Code Readability Poll Pin
N a v a n e e t h7-Oct-09 7:42
N a v a n e e t h7-Oct-09 7:42 
GeneralRe: Code Readability Poll Pin
DaveyM697-Oct-09 9:08
professionalDaveyM697-Oct-09 9:08 
GeneralRe: Code Readability Poll Pin
LimitedAtonement7-Oct-09 10:02
LimitedAtonement7-Oct-09 10:02 
GeneralRe: Code Readability Poll [modified] Pin
DaveyM697-Oct-09 11:12
professionalDaveyM697-Oct-09 11:12 
AnswerRe: Code Readability Poll Pin
Not Active7-Oct-09 6:44
mentorNot Active7-Oct-09 6:44 
GeneralRe: Code Readability Poll Pin
Kevin Marois7-Oct-09 6:53
professionalKevin Marois7-Oct-09 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.