473,789 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About best coding Style of C#

1.
int[] intArr = new int[255];

2.
int[] intArr = new int[255];
rgnNumberArr.In itialize();

3.
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}

intArr allways initilized with 0 value?
or should I initialize it?

choose best code 1,2,3?

Jun 21 '06
42 1962
While I'm reading readings on YAGNI, I have a feeling that YAGNI isn't good
for all cases, especially when you foresee there will be a change breaking
"fundamenta l assumptions" when you design the application. In that case you
will really curse yourself for not writing "not currently needed code" in
the very beginning.

I have a project in hand that because the origional writer does not foresee
there will be other completely different types of products (yes, the
origional code does handle a range of products, but they all have common
characteristics , so the "alien types" added later cased huge trouble), he
ends up need to rewrite nearly half of the application just for that. If he
had the idea that "there might be other type of products that have
completely different characteritics" , he'd write more flexible code and
suffer less when the new type is added.

"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:MP***** *************** ****@msnews.mic rosoft.com...
Giulio Petrucci <gi************ *@RIMUOVIspeech village.com> wrote:
Google for YAGNI for more on this :)

Jun 23 '06 #21
"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
While I'm reading readings on YAGNI, I have a feeling that YAGNI isn't
good for all cases, especially when you foresee there will be a change
breaking "fundamenta l assumptions" when you design the application.
If you can accurately foresee such changes, then Y-Are-GNI. If not, you
should just make sure you follow normal OOP principles, and then you'll be
prepared to change the design when and as needed.
In that case you will really curse yourself for not writing "not currently
needed code" in the very beginning.
The problem is in knowing what code will be needed. If you do spend
significant time handling nonexistent requirements, you may never get a
chance to see if those requirements are ever introduced, because your
project doesn't ship in time and your company runs out of money!
I have a project in hand that because the origional writer does not
foresee there will be other completely different types of products (yes,
the origional code does handle a range of products, but they all have
common characteristics , so the "alien types" added later cased huge
trouble), he ends up need to rewrite nearly half of the application just
for that.
Well, there's bad code of all flavors. And a design that required rewriting
half the application when it changed was never a good design.

But should you really design for "completely different types of products"
when there is absolutely no indication that you'll ever need that
capability? If you're selling insurance policies, should you design it so
you can also sell bicycle tires? It's going to take longer if you do design
it for bicycle tires, and cost more. And you may go out of business while
you're doing it.
If he had the idea that "there might be other type of products that have
completely different characteritics" , he'd write more flexible code and
suffer less when the new type is added.


Predicting the future is hard, and we always look bad when we guess wrong.
That's why YAGNI says not to worry about the future. Changing software to
meet changing requirements when necessary should not be hard, because those
requirements certainly will change. The trick is to write good, separated,
encapsulated, seamful (polymorphic) code that can be changed with a minimum
of bother, not code that can handle unrequired requirements.

(Jon will no doubt address this much better than me, and I'm looking forward
to his reply.)

///ark
Jun 23 '06 #22
Mark Wilden <Ma********@new sgroups.nospam> wrote:

<snip>
(Jon will no doubt address this much better than me, and I'm looking forward
to his reply.)


Sorry to disappoint, but I think you said it all better than I could
have done anyway :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 23 '06 #23
Lau Lei Cheong <le****@yehoo.c om.hk> wrote:
I won't rely on behaviours that I'm not sure at the time I write something.
That's reasonable - but it's also reasonable *to* rely on behaviour
when it's specified. (The way you phrased it originally suggested that
even when it's documented you shouldn't rely on it.)
I search the MSDN library, but can't find anywhere explicitly say this. The
Array Overview said nothing about it too.
Indeed - it's in the C# language spec in this case.
And everywhere here and there
inside it declares variable like this:

Copied from "Properties overview" :
ms-help://MS.MSDNQTR.2006 JAN.1033/cpguide/html/cpconProperties Overview.htm

[C#]
private int number = 0;
[Visual Basic]
Private number As Integer = 0

So I think there should be a reason for initializing the variables
explicitly.


There's plenty of bad code in MSDN - and initialising a single variable
to its default value isn't actually so bad anyway. Going through and
initialising a whole array suggests that something useful is happening
when it isn't.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 23 '06 #24
One OT(since it's a C# newsgroup) question - Given it's a C# specification
behaviour, would I be safe to assume that it'd be the same in VB.NET?

"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:MP***** *************** ****@msnews.mic rosoft.com...
Indeed - it's in the C# language spec in this case.

Jun 23 '06 #25
Lau Lei Cheong wrote:
One OT(since it's a C# newsgroup) question - Given it's a C# specification
behaviour, would I be safe to assume that it'd be the same in VB.NET?


Not necessarily. In fact, I *believe* it's also specified in the CLI,
although a quick flick through doesn't make it obvious, unless this is
covered in 8.9.6.6 or partition 1, which says that user-visible memory
for a new value of an object type is zeroed.

It would be best to check in the VB.NET specification - although I'd be
very surprised if it weren't the same.

Jon

Jun 23 '06 #26
Jon Skeet [C# MVP] wrote:
....
There's plenty of bad code in MSDN - and initialising a single variable
to its default value isn't actually so bad anyway.


Except there are more instructions generated in IL, while the default
initialization is performed some other way.
Another thing looks strange. We know now that explicit initialization
to default value is redundant and just plain wrong because efficiency
suffers. This is documented and supposed to be followed, at least at MS.
Then, why compiler does not allow me do things like this:
int i;
int ii = i;
throwing "using unassigned variable" error?
Jun 23 '06 #27
"Sericinus hunter" <se*****@flash. net> wrote in message
news:cz******** ************@ne wssvr21.news.pr odigy.com...
Another thing looks strange. We know now that explicit initialization
to default value is redundant and just plain wrong because efficiency
suffers.
To me, initialization of fields to default values just screams "the writer
doesn't know C#!"

I'm glad to have another reason besides snobbery to deprecate it.
This is documented and supposed to be followed, at least at MS.
Then, why compiler does not allow me do things like this:
int i;
int ii = i;
throwing "using unassigned variable" error?


(I assume you're talking about fields, not local variables - in the latter
case, i is not initialized.)

True, but you can't do

int i = 0;
int ii = i + 1;

either. For whatever reason, field initializers aren't allowed to access
nonstatic fields.

///ark


Jun 23 '06 #28
Mark Wilden wrote:
"Sericinus hunter" <se*****@flash. net> wrote in message
news:cz******** ************@ne wssvr21.news.pr odigy.com...
Another thing looks strange. We know now that explicit initialization
to default value is redundant and just plain wrong because efficiency
suffers.


To me, initialization of fields to default values just screams "the writer
doesn't know C#!"

I'm glad to have another reason besides snobbery to deprecate it.
This is documented and supposed to be followed, at least at MS.
Then, why compiler does not allow me do things like this:
int i;
int ii = i;
throwing "using unassigned variable" error?


(I assume you're talking about fields, not local variables - in the latter
case, i is not initialized.)


Sorry, I just jumped a step ahead without warning... We discussed
specification for field initialization, but I am talking about local
variables now. Looks like it is initialized. If I write:

int i;
i = 1;

and stop in the debugger right after its declaration, I can see it having value 0.
And the message is quite clear on that: it says "unassigned ". It is probably
due to the language spec vs CLI spec, and designed so just to be safer.
Jun 23 '06 #29


Mark Wilden wrote:
"Sericinus hunter" <se*****@flash. net> wrote in message
news:cz******** ************@ne wssvr21.news.pr odigy.com...

Another thing looks strange. We know now that explicit initialization
to default value is redundant and just plain wrong because efficiency
suffers.

To me, initialization of fields to default values just screams "the writer
doesn't know C#!"


It doesn't mean any such thing. It means that the coder has a variable that
he/she wants initialized to 0.
Jun 23 '06 #30

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

Similar topics

5
1940
by: SungSoo, Han | last post by:
There are two coding style when python import module. (1) import sys (2) from import sys (or from import *) I prefer (1) style. Because it's easy to read, understand module in any category .. Which prefer ? Are there any difference of the 2 styles.
125
14858
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
144
6976
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
4
2015
by: Josh Golden | last post by:
i lead a small development team (based on some of my posts that might cause some people to choke themselves, but have no fear, i am NOT the lead developer, the people on my team are great - i'm just the manager) for my company. although we attempt to use good practices for development, we have no real experience in documentation of a project _before_ it's coded. Are there any great books out there on how to document a project before we...
9
2117
by: Rhino | last post by:
I've been updating some CSS today and got one odd error from the validator at http://jigsaw.w3.org/css-validator/. Every time I had 'background: transparent;' (or background-color: transparent;) associated with a selector, it gave me this error message: Line : 30 (Level : 1) You have no background-color with your color : #navlist li a Here's the CSS fragment that it doesn't like:
1
1463
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as much memory as I can.
10
1430
by: kiplring | last post by:
I made a input window derived from System.Windows.Forms.Form. What I use is just one property "inputWindow.Message". But System.Windows.Forms.Form has so many properties, functions, evnets. So I want to hide all things except the one property "inputWindow.Message".
13
1595
by: charpour | last post by:
Hello, in the program below: #include <stdio.h> int main(void) { int x = 1, y = 2, z; z = x+++y; return 0; }
12
1481
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if (hashString == "MD5") { return System.Security.Cryptography.MD5.Create(); } else { return System.Security.Cryptography.SHA512.Create(); }
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.