473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deedback on .NET Coding Best Practices

Hello,

Can I get feedback on these .NET coding best practices please. Thanks
to Konrad Rudolph and Jon Skeet for the replies to my previous post on
'Writing Properties.' Thanks in advance to all replies to this post as
well, as I am having probs posting from MS Communities site (posting
from Google).
===========

Avoid Returning null Instead of Array, String, or Collection

In almost all situations except when performance can be an issue (e.g.
heavily looped or performance tuned code), null should not be returned
from a method that returns an array, string, collections, ArrayList,
BitArray, HashTable, Queue, SortedList, or Stack. It should not be
necessary for a user of a method to handle a null that is returned
from a method. Instead only empty or zero-length data types such as 0
item arrays, empty strings (String.Empty), and 0 item collections
should be returned.
Avoid Checking String Equality Using == and = Operators

Using == operator in C# and = operator in Visual Basic to compare
strings is slower than using the System.String.Equals method. In
addition, if you need to do case sensitive or insensitive comparisons
the System.String.Compare method is the preferred way to do so. The
String.Compare method can compare two strings based on their
alphabetical sort order as well, something that is important in
developing applications for the international market.
Testing for Empty Strings

The most efficient method to determine if a string is empty is via
it's Length property.

[C#]
if(var1.Length < 1)

[Visual Basic]
If (var1.Length < 1) Then
Avoid Hard Coded String Literals

Use String.Empty rather than "" for empty strings. Other strings
should be placed in a constants class.
[C#]
string var1 = ""; // Avoid
string var2 = String.Empty; // Recommended

[Visual Basic]
Dim var1 As String = "" ' Avoid
Dim var2 As String = String.Empty ' Recommended
Nov 16 '05 #1
1 1909
Rasika Wijayaratne <sc*****@hotmail.com> wrote:
Avoid Returning null Instead of Array, String, or Collection

In almost all situations except when performance can be an issue (e.g.
heavily looped or performance tuned code), null should not be returned
from a method that returns an array, string, collections, ArrayList,
BitArray, HashTable, Queue, SortedList, or Stack. It should not be
necessary for a user of a method to handle a null that is returned
from a method. Instead only empty or zero-length data types such as 0
item arrays, empty strings (String.Empty), and 0 item collections
should be returned.
That really depends on the meaning of the method. For some operations,
like "Find all matching elements" it makes sense to return an empty
array or collection. For others, it makes sense to return null. It's
certainly worth always *considering* it, but it's not really something
to avoid as such.
Avoid Checking String Equality Using == and = Operators

Using == operator in C# and = operator in Visual Basic to compare
strings is slower than using the System.String.Equals method.
Do you have benchmarks to back this up? I'd expect them to be identical
in speed, after the JIT had inlined the call to == to be just
String.Equals(a, b). It's also less readable (IMO) to use Equals than
==.
Testing for Empty Strings

The most efficient method to determine if a string is empty is via
it's Length property.

[C#]
if(var1.Length < 1)
I suspect that

if (var1.Length==0)

is actually more efficient the < 1 test, although I haven't tested it.
It's clearer in meaning, however (IMO).
Avoid Hard Coded String Literals

Use String.Empty rather than "" for empty strings.
Why? I see no reason for this.
Other strings should be placed in a constants class.


Rather than having a single constants class, constants should be
declared appropriately within their domain. For instance, you would
place SQL column names etc within database-domain classes, but you
wouldn't want to mix them with literals to do with completely different
concepts.

I don't see anything wrong with having some hard-coded string literals
in the middle of the code though. If you're only going to use a string
once (eg for a SQL query) there's little point in making it a constant.

User interface strings should usually end up as resources, one way or
another, to allow for internationalisation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Isaac Rodriguez | last post by:
Hi, I am fairily new to Python, but I am really liking what I am seeing. My team is going to re-design some automation projects, and we were going to use Python as our programming language. One...
55
by: Jonas Smithson | last post by:
I've seen a few attractive multi-column sites whose geometry is based on pure CSS-P, but they're what you might call "code afficionado" sites, where the subject matter of the site is "coding...
1
by: Todd | last post by:
Does anyone know of a book for C# .NET on coding standards and guidelines? My company is in the process of defining this stuff as we move to C# .NET. I could swear I picked up a book like this...
4
by: dotNetDave | last post by:
About three weeks ago I released the first .NET coding standards book titled "VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/ speaker Deborah Kurata says about it: ...
27
by: Stuart Gerchick | last post by:
C++ Coding Standards : 101 Rules, Guidelines, and Best Practices by Herb Sutter, Andrei Alexandrescu is now a month or so away from release. What is people's opinion on this...is it going to be a...
18
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
26
by: puzzlecracker | last post by:
It'd be interesting to compare the learning practices of c++ practitioners. I'll start with mine The C++ Programming Language C++ Primer Effective C++ More Effective C++ Effective STL The...
3
by: John Dalberg | last post by:
I am looking for an ASP.NET application on CodePlex which exemplifies best practices for the following: - Use of interfaces - Seperation of the UI, business and data tiers - Data Tier that uses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.