How do you assign default values

by Miha Markič 15. November 2007 12:11

In previous post I was asking whether a method returning an unassigned variable will compile and the answer is no, it won't. So this brings an interesting question, how do you initialize default values?

Are you doing something like this (1) - assigning default value right at the start:

bool Tubo(string text) { bool value = false; if (text == "Tubo") value = true; return value; }

or this (2):

bool Tubo(string text) { bool value; if (text == "Tubo") value = true; else value = false; return value; }

or this one (3) (valid only for bool results):

bool Tubo(string text) { bool value = text == "Tubo"; return value; }

Personally I would go with the last one if I am dealing with bool values. If not, I prefer the first one.

Tags:

.net

Comments (5) -

Giuseppe
Giuseppe
11/15/2007 2:06:15 PM #

why not this way? return value == "Tubo";

Reply

Miha Markic
Miha Markic
11/15/2007 2:19:52 PM #

Sure, why not. I preffer explicit varibales because they are easier to inspect/modify with debugger.

Reply

Mladen
Mladen
11/15/2007 3:50:03 PM #

i do #1 too

Reply

Andrej Tozon
Andrej Tozon
11/15/2007 6:33:28 PM #

I usually don't set default values inside such methods, just to avoid double assigments (value = false, then value = true). So I guess it's #2 for me (with #3 for bools that is). I also tend to use guard statements like [and this is just a simplified example!]:

bool Tubo(string text)
{
  if (text == "Tubo")
    return true;

  return false;
}

Reply

Sorin
Sorin United States
6/3/2009 2:28:37 PM #

If is a bool then
bool Tubo(string text)
{
  return "Tubo" == text; // notice order - too much assertions give auto-reflexes
}

Tong

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS