Get Strings

S

Shapper

Hello,

On a application I need to obtain a few String values.

I have two distinct situations which I will describe:

1) In the case the Strings have only one version I am doing the following:

public static class RegexFactory {
public const String Password = @"^(\w|\-)*$";
public const String Username = @"^(\w|\-)*$";
} // RegexFactory

So I use it simply as follows: RegexFactory.Password();

Note: I am not sure if RegexFactory is a good name for this ...

Maybe RegexProvider?

And should I use Static Properties instead of Consts?

2) I have other situations where one String might have two values.

Consider I have an argument which affects which value of Password I will get.

If argument passed = A then return RegexFactory.Password returns "X".

If argument passed = B then return RegexFactory.Password returns "Y".

...

Basically, I need to have a "table" argument,field that does the association:

Password: A > X, B > Y, C > Z, ...

Username: A > 1, B > 2, C > 3, ...

Which approach would you use?

Note: Theses classes (Both 1 and 2) are used often in my application.

Thank You,

Miguel
 
J

Jeff Johnson

IMHO, there is only the bareest
justification for restricting the format of the username. There is
absolutely no justification for restricting the format of the password, as
doing so can only reduce the strength of the security that the password
provides.

The only exception would be if you are writing code that is simply
interfacing with someone else's poorly-designed code that imposes these
restrictions.

Please, oh please heed Peter's words, Miguel. Saying "passwords can only
contain the following characters" reeks of laziness or just plain stupidity
on the part of the programmer, barring the
interfacing-with-existing-crap-code exception.

This is just my own bigoted opinion, but I feel deep down that the reason
password character restrictions exist is because some idiot Web programmer
(pardon the redundancy) didn't understand his language well enough to be
able to handle "special" characters and just imposed a limitation, which
other (incapable-of-thinking-for-themselves) Web programmers just blindly
copied.
 
S

Shapper

I think the name of the class is much less important than the name of the

fields, which currently don't make sense. The strings are clearly not the

actual password or username themselves, so naming the fields "Password" and

"Username" is at the very least confusing.

I changed the name of the class to RegularExpression.

And the username field to ValidUsername.

I think it makes more sense.
Not in my opinion, no. What benefit do you think you would gain by doing

so?

Agree. But I wasn't sure ...
That said, I have a suspicion that these strings are being used to validate

the format of the username and password. IMHO, there is only the bareest

justification for restricting the format of the username. There is

absolutely no justification for restricting the format of the password, as

doing so can only reduce the strength of the security that the password

provides.

In relation to Username I am not sure and I am still considering.
The username will be used in verification emails so in URLs.
And also in other situations ... I am just not sure at the moment.
Maybe using a Slug to ... Again, still thinking about this.

In relation to password I am not validating ...
I just copied the Username field so I would have 2 as an example.
I am just limiting the length to 80 characters.
The only exception would be if you are writing code that is simply

interfacing with someone else's poorly-designed code that imposes these

restrictions.

Not the case ...
Your question is so vague, the only obvious answer is to just use a

dictionary. That's what dictionaries are good at: mapping one value to

some other value.



Now, that doesn't mean that's what you should do. It's possible that

you're actually talking about a transformation that can be coded

algorithmically, in which case you should do it that way. But absent any

other information, it seems to me that a dictionary is what you want.

I am trying to create something that behaves as a Resource file.
Well, not exactly, but at the moment it would be enough.

The application does the following:

Choose Version > Choose Key > Get Value for Key and Version Specified

So, in fact, it is 3 dimensional: X = Version, Y = Key, Z = F(X,Y) = Value

And this will be used regularly by the application ...

And I would like to make it easy to add new values to the code.

Does this help?
 
A

Arne Vajhøj

On a application I need to obtain a few String values.

I have two distinct situations which I will describe:

1) In the case the Strings have only one version I am doing the following:

public static class RegexFactory {
public const String Password = @"^(\w|\-)*$";
public const String Username = @"^(\w|\-)*$";
} // RegexFactory

So I use it simply as follows: RegexFactory.Password();

Note: I am not sure if RegexFactory is a good name for this ...

Maybe RegexProvider?

And should I use Static Properties instead of Consts?

That is better.

But non static properties would be even better.
2) I have other situations where one String might have two values.

Consider I have an argument which affects which value of Password I will get.

If argument passed = A then return RegexFactory.Password returns "X".

If argument passed = B then return RegexFactory.Password returns "Y".

...

Basically, I need to have a "table" argument,field that does the association:

Password: A > X, B > Y, C > Z, ...

Username: A > 1, B > 2, C > 3, ...

Which approach would you use?

If the argument is different per get string method, then you add the
argument to the method and have the method lookup in a private
Dictionary.

If the argument is not different per get string method then you
should instantiate different classes implementing same interface
depending on the argument (this of course assumes non static
classes and methods).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top