0

initializing the value in constructor or variable declaration? Which one is better (or good practice) and why?

class checker 
{   
   public bool exit_code_1 = false; 
}

or

class checker
{
  public bool exit_code_1;
  public checker()
  {
     exit_code_1 = false;
  }
}
John Ryann
  • 2,283
  • 11
  • 43
  • 60
  • 5
    For bool -> false, it's better to do neither and just let default init take care of it. – Steve Townsend Mar 13 '13 at 19:21
  • 6
    (And aside from everything else, please follow .NET naming conventions...) – Jon Skeet Mar 13 '13 at 19:21
  • 1
    Both of those are asking for it. Use a property... – Tony Hopkinson Mar 13 '13 at 19:25
  • @SteveTownsend I disagree. It's best to not rely on default behaviors as it forces the reader to remember what the defaults are. Given that they aren't always what people's initial expectations are, and that they even vary between languages, it's not something you should expect. Worse still, someone could see that it's not initialized and add the initialization in; if they were mistaken as to what the default behavior is they could end up changing working code to non-working code. – Servy Mar 13 '13 at 19:31

0 Answers0