A quick question, about so-called 'best practices', I'm interested in
which of A/B of the two examples people would choose, and why.
[A]
public enum MyEnum
{
Option1 = 0,
Option2 = 1,
Option3 = 2,
Option4 = 3
}
[b]
public enum MyEnum
{
Option1,
Option2,
Option3,
Option4
}
The second one:
[A]
public string Foo
{
get
{
return this.foo;
}
set
{
if ( this.foo != value )
return this.foo
}
}
[b]
public string Foo
{
get
{
return this.foo;
}
set
{
return this.foo
}
}
I'm also interested in best practices for null checks for paramters in
methods and other best practices. The Microsoft website has a few
(try/catch, chunky calls etc.) in its best practices page, but I'm
after a more comprehensive list.
Thanks 13 2242
Glaring error/typo with the second example, which should read:
[A]
public string Foo
{
get
{
return this.foo;
}
set
{
if ( this.foo != value )
this.foo = value;
}
}
[b]
public string Foo
{
get
{
return this.foo;
}
set
{
this.foo = value;
}
}
In message <11**********************@o13g2000cwo.googlegroups .com>, john
doe <sl********@gmail.com> writes set { if ( this.foo != value ) this.foo = value; }
What does this achieve, other than a needless string comparison? If they
are equal before the operation, after it foo == value, if they aren't,
foo == value. If you don't do the compare, same result.
--
Steve Walker
"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:qT**************@otolith.demon.co.uk... In message <11**********************@o13g2000cwo.googlegroups .com>, john doe <sl********@gmail.com> writes set { if ( this.foo != value ) this.foo = value; }
What does this achieve, other than a needless string comparison? If they are equal before the operation, after it foo == value, if they aren't, foo == value. If you don't do the compare, same result.
-- Steve Walker
I guess it depends on which is the more expensive operation: a needless
comparison or a needless assignment (i.e. if the strings are equal). I'm
not sure, but I would typically just do the assignment. I would only
interogate the new value it it needed validation.
"john doe" <sl********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com... A quick question, about so-called 'best practices', I'm interested in which of A/B of the two examples people would choose, and why.
[A] public enum MyEnum { Option1 = 0, Option2 = 1, Option3 = 2, Option4 = 3 }
[b] public enum MyEnum { Option1, Option2, Option3, Option4 }
[snip]
It depends. If the actual values of the enumeration don't matter then I
would use B.
In message <11**********************@f14g2000cwb.googlegroups .com>, john
doe <sl********@gmail.com> writes A quick question, about so-called 'best practices', I'm interested in which of A/B of the two examples people would choose, and why.
[A] public enum MyEnum { Option1 = 0, Option2 = 1, Option3 = 2, Option4 = 3 }
[b] public enum MyEnum { Option1, Option2, Option3, Option4 }
Depends on whether the integer values matter or not. If they are ever
persisted (to a file, a database, etc) then it's important that someone
doesn't come along later and do:
public enum MyEnum
{
Option0,
Option1,
Option2,
Option3,
Option4
}
It also matters if you want to do something like:
public enum MyEnum
{
Option0=1,
Option1=2,
Option2=4,
Option3=8,
Option4=16
}
DoSomething(MyEnum.Option1 | MyEnum.Option2);
--
Steve Walker
In message <#N**************@TK2MSFTNGP10.phx.gbl>, Andy Walldorff
<an************@daytonrcs.REMOVE.com> writes "Steve Walker" <st***@otolith.demon.co.uk> wrote in message news:qT**************@otolith.demon.co.uk... In message <11**********************@o13g2000cwo.googlegroups .com>, john doe <sl********@gmail.com> writes set { if ( this.foo != value ) this.foo = value; } What does this achieve, other than a needless string comparison? If they are equal before the operation, after it foo == value, if they aren't, foo == value. If you don't do the compare, same result.
I guess it depends on which is the more expensive operation: a needless comparison or a needless assignment (i.e. if the strings are equal).
Well, quite. I can't see a string comparison ever being cheaper than an
assignment.
I'm not sure, but I would typically just do the assignment. I would only interogate the new value it it needed validation.
Or if the 'assignment' wasn't just that, but required some pretty heavy
processing; revalidation of the whole object, parsing the string, etc.
--
Steve Walker
Thanks for the replies, the two answers given - _not_ assigning
enumeration values unless its got the flags attribute, and _not_
comparing in the set, are practices I would use.
My other question was about null checking - and the best place to check
for null values in a chain of methods. For example, 3 methods:
method1(SomeCollection val)
method2(SomeCollection val)
method3(SomeCollection val)
with method1 calling method2 which calls method3. Assuming method3
iterates through the 'val' collection, is this where null checking
should be performed, or is it up to method2 or method3 to do the checks?
If method1 and method2 just pass through the val collection to method3
without otherwise accessing it, the null check should be left to method3.
I'd go farther and say that regardless of what method1 and method2 do, if
method3 can't handle the collection being null it should check for it. If
performance is critical at least assert that it is not null so you have a
chance of catching it in a debug build if the precondition is not met.
"john doe" <sl********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Thanks for the replies, the two answers given - _not_ assigning enumeration values unless its got the flags attribute, and _not_ comparing in the set, are practices I would use.
My other question was about null checking - and the best place to check for null values in a chain of methods. For example, 3 methods:
method1(SomeCollection val) method2(SomeCollection val) method3(SomeCollection val)
with method1 calling method2 which calls method3. Assuming method3 iterates through the 'val' collection, is this where null checking should be performed, or is it up to method2 or method3 to do the checks?
In message <11**********************@z14g2000cwz.googlegroups .com>, john
doe <sl********@gmail.com> writes Thanks for the replies, the two answers given - _not_ assigning enumeration values unless its got the flags attribute, and _not_ comparing in the set, are practices I would use.
My other question was about null checking - and the best place to check for null values in a chain of methods. For example, 3 methods:
method1(SomeCollection val) method2(SomeCollection val) method3(SomeCollection val)
with method1 calling method2 which calls method3. Assuming method3 iterates through the 'val' collection, is this where null checking should be performed, or is it up to method2 or method3 to do the checks?
Depends entirely on the context, I'd say.
--
Steve Walker
Hi,
Well in the worst case it will be an comparision & an assignment , versus
only the assignment.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andy Walldorff" <an************@daytonrcs.REMOVE.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... "Steve Walker" <st***@otolith.demon.co.uk> wrote in message news:qT**************@otolith.demon.co.uk... In message <11**********************@o13g2000cwo.googlegroups .com>, john doe <sl********@gmail.com> writes set { if ( this.foo != value ) this.foo = value; }
What does this achieve, other than a needless string comparison? If they are equal before the operation, after it foo == value, if they aren't, foo == value. If you don't do the compare, same result.
-- Steve Walker
I guess it depends on which is the more expensive operation: a needless comparison or a needless assignment (i.e. if the strings are equal). I'm not sure, but I would typically just do the assignment. I would only interogate the new value it it needed validation.
Hi,
I would do it in method3 , it;s where it's used,
You may have the case where method3 is called from a place other than
method2 so if you do not check it inside method3 you could get an exception
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"john doe" <sl********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Thanks for the replies, the two answers given - _not_ assigning enumeration values unless its got the flags attribute, and _not_ comparing in the set, are practices I would use.
My other question was about null checking - and the best place to check for null values in a chain of methods. For example, 3 methods:
method1(SomeCollection val) method2(SomeCollection val) method3(SomeCollection val)
with method1 calling method2 which calls method3. Assuming method3 iterates through the 'val' collection, is this where null checking should be performed, or is it up to method2 or method3 to do the checks?
john doe wrote: with method1 calling method2 which calls method3. Assuming method3 iterates through the 'val' collection, is this where null checking should be performed, or is it up to method2 or method3 to do the checks?
Why do null-check at all? if you don't check you will get an exception,
which has all the information about what went wrong.
What is your "null-check" going to do, that is better?
--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Helge Jensen <he**********@slog.dk> wrote: with method1 calling method2 which calls method3. Assuming method3 iterates through the 'val' collection, is this where null checking should be performed, or is it up to method2 or method3 to do the checks?
Why do null-check at all? if you don't check you will get an exception, which has all the information about what went wrong.
What is your "null-check" going to do, that is better?
1) Using an ArgumentNullException makes it clear that it was an
argument that was incorrect rather than logic inside a method
2) Using an ArgumentNullException can give more information about
*which* argument (or part of argument) was null
3) Throwing an exception before doing any other work leaves objects in
a more consistent state
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: byrocat |
last post by:
I'm chasing after a documetn that was available on one of the Microsoft
websites that was titled somethign like "MS SQL Server Best Practices"
and detailed a nyumber of best practices about...
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their...
|
by: |
last post by:
Hi can someone send or point me to Any nice Material on .NET Best Practices
-regards
|
by: Amelyan |
last post by:
Could anyone recommend a book (or a web site) that defines best practices in
ASP.NET application development?
E.g.
1) Precede your control id's with type of control btnSubmit, txtName, etc.
2)...
|
by: Luis Esteban Valencia |
last post by:
Hello. Can somebody recomend me books of design patterns in c# and best
practices too.
|
by: jojobar |
last post by:
Hello,
I am trying to use vs.net 2005 to migrate a project originally in vs.net
2003. I started with creation of a "web site", and then created folders for
each component of the site.
I read...
|
by: Louis Aslett |
last post by:
I hope this is the correct newsgroup for this query (if not please
give me a pointer to where is best):
I understand the theory of normalisation etc and am trying to follow
best practices in the...
|
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?
...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |