Skip to content

Instantly share code, notes, and snippets.

View tany3's full-sized avatar
😇
I may be slow to respond.

Masahiro Taniuchi tany3

😇
I may be slow to respond.
  • Tokyo, Japan
View GitHub Profile
@tany3
tany3 / gist:2d032cbbbd9db04b976d
Created May 15, 2014 08:23
重複処理 IEnumerableをそのまま使い回さない - Possible multiple enumeration of IEnumerable

IEnumerableをそのまま使うと、使う度に毎回変換される。遅延実行。 http://confluence.jetbrains.com/display/ReSharper/Possible+multiple+enumeration+of+IEnumerable

Assuming that GetNames() returns an IEnumerable, we are, effectively, doing extra work by enumerating this collection twice in the two foreach statements. If GetNames() results in a database query, you end up doing that query twice, while both times getting the same data.

ということで、特に複数回使う場合など適宜ToList()やToArray()しておく。

参考:LINQ クエリの概要 (C#) http://msdn.microsoft.com/ja-jp/library/bb397906.aspx

@tany3
tany3 / countries
Last active August 29, 2015 13:57
Test data : countries
# Test data
## countries
Afghanistan
Albania
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
@tany3
tany3 / gist:9245701
Created February 27, 2014 07:08
仕様を明確にする - Field can be readonly

###仕様を明確にする - Field can be readonly 初期化時だけセットするフィールドはreadonlyにすることで、副作用を防ぐことができる(安全弁になる)。

public class Book
{
  private string readonly name;
  private int readonly ISBN;
  
  public Book(string name, int isbn)
 {
@tany3
tany3 / gist:9245669
Created February 27, 2014 07:05
カウントを取らなくてい - Use method Any()

##カウントを取らなくてい - Use method Any() あるcollectionが0件であることを知りたいなら、!Any()を使う。 Any()は有無を返す。Count()は個数を返すので、数え上げ処理が動く。 ###変更前

if (collection.Count() == 0) { ... } else { ... }

###変更後

if (!collection.Any()) { ... } else { ... }
@tany3
tany3 / gist:9245655
Created February 27, 2014 07:03
重複処理 WhereとFirstOrDefault

##重複処理 WhereとFirstOrDefault - Replace with single call to FirstOrDefault()

###変更前

  collection.where(item => item.Equals(value)).FirstOrDefault();

###変更後

 collection.FirstOrDefault(item => item.Equals(value));
@tany3
tany3 / gist:9245645
Last active August 29, 2015 13:56
冗長なelse - Redundant 'else' keyword

##冗長なelse - Redundant 'else' keyword ブロック節を使って直ぐにreturnするならば、elseは要らない。

###変更前

if (!collection.Any())
{
  return false;
}
else
@tany3
tany3 / gist:9245638
Last active August 29, 2015 13:56
冗長なネスト - Invert 'if' statement to reduce nesting

##冗長なネスト - Invert 'if' statement to reduce nesting ネストが深くならないように工夫できる。

###変更前

foreach (var data in values)
{
  if (data.hoge == VALUE_HOGE) // 値HOGEのときだけ処理したい
  {
 //何かの処理
@tany3
tany3 / gist:9068853
Last active August 29, 2015 13:56
そのDEBUGディレクティブは要らない

そのDEBUGディレクティブは要らない

System.Diagnostics.Debugクラスは、ConditionalAttributeが付与されている。

###変更前

#if DEBUG
 System.Diagnostics.Debug.WriteLine("デバッグの時だけ出したい");
@tany3
tany3 / gist:9068761
Last active August 29, 2015 13:56
助長なネスト - Invert 'if' to reduce nesting
@tany3
tany3 / gist:9068724
Last active August 29, 2015 13:56
T ? vs Nullable<T>

T ? vs Nullable

C#ではNullableと書く必要はなく、T?と書いて良い。

In C#, it is not necessary to use the full Nullable type name because the language allows you to write T? instead. >Consequently, ReSharper offers the option to replace the long type name with the shorthand.

###変更前

Nullable<DateTime> t;