[dba-VB] ControlChars in C#

Shamil Salakhetdinov shamil at users.mns.ru
Wed Mar 26 09:05:29 CDT 2008


Hi Gustav,

I'm not arguing :) - below are the best practices from my experience:

Defining and assigning local variable could be less efficient on runtime
than not using them - as I noted this is compiler's job to optimize
generated code, which is executed on runtime, and C# (ILDASM) optimizer is
very well done as far as I know...

..usage of local variables "stinks" according to the code refactoring rules
because if you imagine you have a large code (function) where a local
variable/local variables is/are defined and you need to refactor this
function's code by splitting it into several parts then you will have to
duplicate, triplicate... your variables definitions etc... IOW refactoring
by copy and paste parts of code is clearly much easier (and safer) procedure
than refactoring code when you have several variables - and taking into
account the note above on code optimization refactored code with several
duplicated variables is becoming less and less efficient with every
refactoring cycle... 

<<<
it is considered good coding practice to _always_ include these 
>>>
Well, I'd note that when you can write one line

     if (something) e.Handled = true  

then better write this one line - reason IMO is that the display space is
limited and it's better to have your code lines as compact as possible (but
without loosing readability of course) - you can watch this trend in VS2008
where properties (get/set) can be defined in one line
(http://refact.blogspot.com/2008/01/c-in-vs-2008.html )

That code:

private void bindingNavigatorPositionItem_KeyPress(
      object sender, KeyPressEventArgs e)
{
if (Char.IsControl(e.KeyChar) &&
    e.KeyChar != ControlChars.Back)
       e.Handled = true  
}

Would be a four lines code or even two lines code if I write it in my
projects:

- 1st line - function signature;
- 2nd line - { <function body> }

That's a useful feature of C# you can format your code manually as it better
suits you - and forget the automated code beautifiers - these are not for
real men :)

Of course there are no absolute rules, and coding style is often very
dependent on one's own taste, IOW once again I'm not arguing, I'm just
explaining why I'd recommend the coding style I usually follow, the coding
style, which is based on many different recommendations and experiences
including my own...

Thank you :)

--
Shamil
 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Wednesday, March 26, 2008 1:35 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] ControlChars in C#

Hi Shamil

You are right. It was due to old habits that I used the nested if .. and
because during my testing it was easier to edit.

As for the local variables they often make code much easier to browse (not
in this simple example) and - if you need to assign the variable something
else - it will be done at one place only.

As for the curly brackets or not in a simple if statement I've read several
places that it is considered good coding practice to _always_ include these
- even for one-line code like here:

     if (something)
         e.Handled = true  

Preferred:

     if (something)
     {
         e.Handled = true  
     }

For example, the add-in, CodeIt.Right, corrects this:

http://submain.com/default.aspx?nav=products.guidelines

/gustav

>>> shamil at users.mns.ru 25-03-2008 20:22 >>>
Hi Gustav,

Sorry, I missed the logic (it's a storm and snowfall here - my brain cells
could have been influenced by that :))

I should have coded:

private void bindingNavigatorPositionItem_KeyPress(
      object sender, KeyPressEventArgs e)
{
if (Char.IsControl(e.KeyChar) &&
    e.KeyChar != ControlChars.Back)
       e.Handled = true  
}

How about that? Still missing something here? 

Notes on coding style above, which might look unusual for VB(A) veterans
(I'm one of them):

1. "Code refactoring gurus" recommend avoiding using local variables - it's
a compiler's job to optimize compiled code...

2. Using 'if' with two conditional expressions "glued with &&" doesn't
introduce any performance hits because if the first condition fails the
second one will not be evaluated...

Thanks.

--
Shamil
 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Tuesday, March 25, 2008 9:59 PM
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] ControlChars in C#

Hi Shamil

Yes I need all that code. If omitted, any other keypress than backspace will
be "handled" meaning be inactive; thus I can only delete the digits in the
record position selector (filled and updated when you navigate the table)
with the backspace key, not typing any new digits.

/gustav



_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list