投稿数 - 437, コメント - 59536, トラックバック - 156

無闇に初期化しちゃダメ

public void Func1()
{
    string s = null;
    …
}

C#、または VB で、上記のようにローカル変数を「とりあえず」初期化しているコードは非常によく見かける。あまつさえ、このようなコーディング作法を推奨している書籍すらある。

このような作法は、はっきり言って不味い。

C/C++ ではポインタを null(0) で初期化するのは当然だ。C/C++ ではポインタを初期化しておかないと、適当にその辺にあるアドレスが入っているので、そのまま使うと、とんでもないアドレスにアクセスする。初期化してようがなかろうが、ポインタに正常なアドレスを渡していなければバグだが、初期化していればバグにすぐ気付く。

ただし、これは C/C++ の話だ(もちろん C/C++ でも無闇に初期化するのはどうかと思うが)。C#、VB なら話が違う。

C# 使いなら誰でも「未割り当てのローカル変数 's' が使用されました」というコンパイラのメッセージを一度は見たことがあるだろう。コンパイラは進化してここまで教えてくれるようになった。VB ならば、なんとコンパイルする前にエディタが教えてくれる(でもコンパイルはできる)。だったら意味のない初期化は全く必要ない。

このコンパイルエラーを無くす目的のためだけに初期化してはいけない。これでは、折角見えた落とし穴の上に新聞紙を置いているだけに過ぎない。落とし穴を見つけたら必ず土で埋めるようにしよう。そもそも、未割り当てのローカル変数を使おうとしているのは、ロジックにミスがあるという事なのだから。

投稿日時 : 2006年10月28日 1:00

フィードバック

# タイトル

コメント
2007/12/22 14:47 | 名前

# I visited many web pages except the audio feature for audio songs current at this web page is actually excellent.

I visited many web pages except the audio feature for audio songs current at
this web page is actually excellent.

# Your style is really unique compared to other people I've read stuff from. Many thanks for posting when you've got the opportunity, Guess I will just book mark this web site.

Your style is really unique compared to other people I've read stuff from.
Many thanks for posting when you've got the opportunity, Guess I
will just book mark this web site.

# Hello, Neat post. There is an issue together with your website in web explorer, might test this? IE still is the market leader and a big portion of other people will leave out your fantastic writing because of this problem.

Hello, Neat post. There is an issue together with your website in web explorer, might test this?
IE still is the market leader and a big portion of other people will leave out your fantastic writing because of this problem.

# great put up, very informative. I'm wondering why the opposite experts of this sector don't notice this. You should proceed your writing. I am confident, you have a huge readers' base already!

great put up, very informative. I'm wondering why the opposite experts of this sector don't notice this.
You should proceed your writing. I am confident, you have a huge readers' base already!

# Wonderful site you have here but I was curious if you knew of any community forums that cover the same topics talked about here? I'd really love to be a part of online community where I can get feedback from other experienced individuals that share the

Wonderful site you have here but I was curious
if you knew of any community forums that cover the same topics talked about here?
I'd really love to be a part of online community where
I can get feedback from other experienced individuals that share
the same interest. If you have any suggestions, please let me know.
Many thanks!

# I used to be able to find good info from your content.

I used to be able to find good info from your content.

# I have learn several good stuff here. Definitely value bookmarking for revisiting. I wonder how so much attempt you put to create this kind of excellent informative site.

I have learn several good stuff here. Definitely value bookmarking for revisiting.
I wonder how so much attempt you put to create this kind of
excellent informative site.

# Hi, all is going well here and ofcourse every one is sharing data, that's truly good, keep up writing.

Hi, all is going well here and ofcourse every one
is sharing data, that's truly good, keep up writing.

# It's impressive that you are getting thoughts from this paragraph as well as from our argument made at this place.

It's impressive that you are getting thoughts from this paragraph as well as from our argument made
at this place.

# Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is great blog. An excellent read. I'll definitely

Its like you read my mind! You seem to know a lot about this, like
you wrote the book in it or something. I think
that you can do with some pics to drive the message home
a bit, but other than that, this is great blog. An excellent read.
I'll definitely be back.

# Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is great blog. An excellent read. I'll definitely

Its like you read my mind! You seem to know a lot about this, like
you wrote the book in it or something. I think
that you can do with some pics to drive the message home
a bit, but other than that, this is great blog. An excellent read.
I'll definitely be back.

# Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is great blog. An excellent read. I'll definitely

Its like you read my mind! You seem to know a lot about this, like
you wrote the book in it or something. I think
that you can do with some pics to drive the message home
a bit, but other than that, this is great blog. An excellent read.
I'll definitely be back.

# Illikebuisse hmdth

hydrochlroquine https://www.pharmaceptica.com/
2021/07/03 6:23 | pharmaceptica.com

# re: ???????????

chlooquine https://chloroquineorigin.com/# does hydroxychloroquine cause heart problems
2021/07/10 5:19 | hydochloroquine

# will erectile dysfunction go away

hydroxichloraquin https://plaquenilx.com/# whats hcq

# re: ???????????

chloroquine natural sources https://chloroquineorigin.com/# hydro chloroquine

# re: ???????????

chloroquine moa https://chloroquineorigin.com/# plaquenil 200 mg twice a day
2021/08/09 10:10 | hydroxyclorine

# hudpmmivedyt

hydroxychloroquine and azithromycin https://hydroxychloroquinetc.com/
2021/11/29 19:09 | dwedayykdf

コメントの投稿

タイトル
名前
URL
コメント