When declaring member variables inside a class is it better to instantiate the object on the same line as the declaration or within the constructor.
i.e.
public class MyClass
{
protected ArrayList _myList = new ArrayList();
}
or
public class MyClass
{
protected ArrayList _myList;
public MyClass
{
_myList = new ArrayList();
}
}
-
-
The first. They compile down to the same IL, but the first is more readable.
-
Sven Groot wrote:The first. They compile down to the same IL, but the first is more readable.
Cool I always use the first one, wanted to know if there was some kind of difference either way didn't think there was.
Try explaining the concept of readable code to the people I'm working with
. I even left a copy of Code complete on the desk to give a subtle hint.
I later resorted to laughing histerically whenever I came accross messy code
-
Even more reason for initialising inline is that if at some point you realise the class can be made a static class, you don't have to go to the bother of unpicking the constructor.
(I'm doing performance optimisations at the moment, and making methods which don't rely on instance properties static helps save making virtual function calls... Unfortunately when you make the whole class static, having a static constructor adds more IL for the "have I been built yet" logic than not having one.) -
As an added protection, I always add the 'readonly' modifier for fields that I will only assign to in the constructor or inline. That way, you can't accidently overwrite the field later. Of course, the object itself is not read-only, just the variable.
-
On a performance note... Avoid using ArrayLists if you can

-
Cheers guys.
Another one.
Does anyone write one line conditional statements without curly braces?
e.g.
if (1 == 1)
doSomething;
as opposed to:
if (1 == 1)
{
doSomething;
}
I always use the latter but it seems everyone here uses the first method even for 'for' and 'foreach' loops.
I just find it very messy and unreadable. -
Massif wrote:
On a performance note... Avoid using ArrayLists if you can

Whats a better alternative to ArrayLists standard arrays?
I use the collection classes quite alot in .net just find them nice to work with plus I didn't think the performance hit would be anything to worry about?
-
I don't really have a preference for that, I'll use either one depending on my mood. As long as you indent properly I don't really think either is more or less readable than the other.
-
leeappdalecom wrote:

Massif wrote:
On a performance note... Avoid using ArrayLists if you can

Whats a better alternative to ArrayLists standard arrays?
I use the collection classes quite alot in .net just find them nice to work with plus I didn't think the performance hit would be anything to worry about?
List<T> has better performance than ArrayList, especially for value types (no boxing needed) and in foreach loops, while offering the exact same (plus more) benefits. -
Sven Groot wrote:I don't really have a preference for that, I'll use either one depending on my mood. As long as you indent properly I don't really think either is more or less readable than the other.
Maybe I have OCD then lol
I hate it when things aren't consistent, I suppose I prefer curly braces in a way because I come from a VB background although my C style language experience now far outweighs my VB experience lol
I even use BEGIN and END with every conditional statement in SQL!
-
Sven Groot wrote:

leeappdalecom wrote:

Massif wrote:
On a performance note... Avoid using ArrayLists if you can

Whats a better alternative to ArrayLists standard arrays?
I use the collection classes quite alot in .net just find them nice to work with plus I didn't think the performance hit would be anything to worry about?
List<T> has better performance than ArrayList, especially for value types (no boxing needed) and in foreach loops, while offering the exact same (plus more) benefits.
Ah ok cool thanks for that.
-
leeappdalecom wrote:Cheers guys.
Another one.
Does anyone write one line conditional statements without curly braces?
e.g.
if (1 == 1)
doSomething;
as opposed to:
if (1 == 1)
{
doSomething;
}
I always use the latter but it seems everyone here uses the first method even for 'for' and 'foreach' loops.
I just find it very messy and unreadable.
I find the former to be more error prone as it's easy to miss where the conditional/loop code starts and stops. And if you have to add more code, you have to remember to add the braces.
Another one for you:
I hate
if(!flag)
and I always use
if(flag == false)
The ! character is too easy to miss-read.
Herbie
-
I use if/while/using/... without curly braces when it's appropriate and readable. It also has the advantage that you gain some vertical space and you can see more lines of code at once. Here's some (real) code that demonstrates this:
void WriteCtfData(IDataReader reader) { using (Stream lWriteStream = CreateWriteStream()) using (Stream lEncryptionStream = Encryption.CreateEncryptionStream(lWriteStream, fPassword)) using (Stream lCompressionStream = Compression.CreateCompressionStream(lEncryptionStream)) using (CtfWriter lWriter = new CtfWriter(lCompressionStream)) DataUtils.Write(lWriter, reader); }
Imagine if I were to use curly braces for each using-block:void WriteCtfData(IDataReader reader) { using (Stream lWriteStream = CreateWriteStream()) { using (Stream lEncryptionStream = Encryption.CreateEncryptionStream(lWriteStream, fPassword)) { using (Stream lCompressionStream = Compression.CreateCompressionStream(lEncryptionStream)) { using (CtfWriter lWriter = new CtfWriter(lCompressionStream)) { DataUtils.Write(lWriter, reader); } } } } } -
leeappdalecom wrote:

Massif wrote:
On a performance note... Avoid using ArrayLists if you can

Whats a better alternative to ArrayLists standard arrays?
I use the collection classes quite alot in .net just find them nice to work with plus I didn't think the performance hit would be anything to worry about?
Well that entirely depends on your performance requirements.
(on the if / loop note I always use curly braces, so that all control structures are clearly and consistently demarked... losing the curly braces is just plain annoying and does very occasionally cause a bug to slip through.)
But if you're putting the same type in, use a Generic List<Type>. I've lost the references, but the differences are that if you're putting in primitive types ArrayLists require them to be boxed (pathetically minor performance problem) and (for reasons I don't understand) have to create an iterator each and every time you foreach through the list. (Or is it that they have to create a more complicated iterator? That could be it... It certainly means you end up checking and casting the type when retrieving data from the arraylist.)
Anyway, in short, if you know the type that's going in, and you're always putting in the same type... use Generics over any collection which stores things as just Objects. -
leeappdalecom wrote:I hate it when things aren't consistent, I suppose I prefer curly braces in a way because I come from a VB background although my C style language experience now far outweighs my VB experience lol
Well I come from a VB background too so that can't be it.
On a style note, I started working in C-style languages in 98 and my style is based on what MSDN used back then, so I do this:
if( x == 0 )
{
// stuff
}
for( int y = 0; y < 10; ++y )
{
// stuff
}
Instead of this:
if (x == 0)
{
// stuff
}
for (int y = 0; y < 10; ++y)
{
// stuff
}
I do this for all control flow (if, for, foreach, switch, catch, while). I know it's just a matter of what I'm used to, but I think the latter just looks incredibly awkward.
Unfortunately the latter is the default in C#'s autoformatter since VS2005. Fortunately it can be changed, and it is indeed the first thing I change after installing VS. But that doesn't help me with other people's code. -
Dr Herbie wrote:
Another one for you:
I hate
if(!flag)
and I always use
if(flag == false)
The ! character is too easy to miss-read.
Herbie
I love the exclamation mark... But there you go... (also what's with VB using <> instead of !=... having ! universally mean NOT is nice because it means the operators can be consistent.)
What I really hate is
if(flag == true)
which I was made to do by stupid coding standards during my first job.
if(flag)
makes just as much sense, and is shorter. -
When you get other people's code files, just press Ctrl+E followed by D, and VS will format the entire file to your preferences.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.