Detect user in group only once
-
Good code or bad code? Runs during a form constructor (testUser is a class bool).
public class a : Form
{
bool testUser;
a()
{
testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
// ...
}
}I think my SQL experience influenced me slightly on this one.
-
Good code or bad code? Runs during a form constructor (testUser is a class bool).
public class a : Form
{
bool testUser;
a()
{
testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
// ...
}
}I think my SQL experience influenced me slightly on this one.
-
Looks OK for a test code. And it's easy to extend. It would be bad if it was in a released code.
Greetings - Jacek
-
It's not in test code - it just sets a bool to determine whether features still being tested are available or not.
-
SortaCore wrote:
It's not in test code (...) features still being tested
With a "test code" I (imprecisely) meant a "code being tested" -- which is the case.
Greetings - Jacek
-
Good code or bad code? Runs during a form constructor (testUser is a class bool).
public class a : Form
{
bool testUser;
a()
{
testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
// ...
}
}I think my SQL experience influenced me slightly on this one.
As stated, it's OK. You could use a case-insensitive HashSet -- I would make it static in case the set of testers becomes large. And perhaps use the
# if DEBUG
directive. If you find yourself copying this code to many forms, you could put it in a library method. -
Good code or bad code? Runs during a form constructor (testUser is a class bool).
public class a : Form
{
bool testUser;
a()
{
testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
// ...
}
}I think my SQL experience influenced me slightly on this one.
SortaCore wrote:
testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
If you want to get a little tighter about it:
testUser = Array.Exists(new [] { "a", "b" }, t => t=="a");
First of all, you don't need
string[]
because the compiler can infer the array type. Second, using "Exists" makes it clearer to the reader what you're doing, especially since you're not interested in the index, you just want to know of the value exists. Lastly, the use of the predicatet => t == 'a'
means you could do some fancier things if you wanted later on -- it's a more general purpose solution. (Where'a'
would actually be in your caseEnvironment.UserName.ToLower()
Marc