-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvement #42
base: master
Are you sure you want to change the base?
Improvement #42
Conversation
Syntax improvement and code cleanup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention of the code changes are good, but when committing to an open source project, please keep in mind that there is an existing code style/standard. Much of this PR is modifying the code away from the existing standard (primarily with the change to use explicit variable type names in place of the exiting usage of var
).
string passPhrase = "test"; | ||
HashSet<string> privKeySet = new HashSet<string>(); | ||
HashSet<string> pubKeySet = new HashSet<string>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you change all of the var
lines to explicit type declarations? That is not a code cleanup, but a change in the overall standard of how the code is written within this project.
KeyGenerator keyGenerator = new KeyGenerator(256); //default key size | ||
KeyPair? pair = keyGenerator.GenerateKeyPair(); | ||
string? privateKey = pair.ToEncryptedPrivateKeyString(passPhrase); | ||
string? publicKey = pair.ToPublicKeyString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In C#, the ? after a type denotes a nullable value type. This means that the variable can hold either a value of that type or null. This is particularly useful for value types (like int, double, bool, etc.) which cannot naturally be null.
Here are some reasons why you might want to use a nullable type:
- Representing Absence of Value: It allows you to represent the absence of a value in a way that's consistent with how reference types work (where null can be used). For example, int? can represent an integer that might not have a value.
- Default Values: For value types, the default value is generally 0, false, or another default. A nullable type allows you to explicitly indicate that the value is not set or unknown by setting it to null.
- Database Interactions: When working with databases, many fields can have null values. Using nullable types ensures your C# code can accurately represent and handle these scenarios.
- Conditional Logic: You can easily check if a nullable type has a value using .HasValue and retrieve the value using .Value.
- Enhanced Readability and Maintenance: By explicitly declaring a type as nullable, it makes the code more readable and maintainable, clearly indicating that the variable is allowed to have no value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ammarheidari yes, this is the syntax for a nullable type, but the along with the change to coding style here, you changed the nullability of the values. For instance GenerateKeyPair()
does not return a nullable value, so along with teh style change, this is a behavioral change.
KeyGenerator? keyGenerator = KeyGenerator.Create(); | ||
KeyPair? keyPair = keyGenerator.GenerateKeyPair(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again
License? license = License.New() | ||
.CreateAndSignWithPrivateKey(privateKey, passPhrase); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again
|
||
Assert.That(license, Is.Not.Null); | ||
Assert.That(license.Signature, Is.Not.Null); | ||
|
||
// validate xml | ||
var xmlElement = XElement.Parse(license.ToString(), LoadOptions.None); | ||
XElement xmlElement = XElement.Parse(license.ToString(), LoadOptions.None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
|
||
#else | ||
#else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spacing of the preprocessor directive
|
||
validator.Validate = license => assemblies.All( | ||
asm => | ||
asm.GetCustomAttributes<AssemblyBuildDateAttribute>() | ||
.Cast<AssemblyBuildDateAttribute>() | ||
.All(a => a.BuildDate < license.Expiration)); | ||
|
||
#endif | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spacing of the preprocessor directive
@@ -125,11 +133,15 @@ public static IValidationChain ProductBuildDate(this IStartValidationChain valid | |||
/// <returns>An instance of <see cref="IStartValidationChain"/>.</returns> | |||
public static IValidationChain AssertThat(this IStartValidationChain validationChain, Predicate<License> predicate, IValidationFailure failure) | |||
{ | |||
var validationChainBuilder = (validationChain as ValidationChainBuilder); | |||
var validator = validationChainBuilder.StartValidatorChain(); | |||
ValidationChainBuilder validationChainBuilder = validationChain as ValidationChainBuilder; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change from var
var validationChainBuilder = (validationChain as ValidationChainBuilder); | ||
var validator = validationChainBuilder.StartValidatorChain(); | ||
validator.Validate = license => license.VerifySignature(publicKey); | ||
ValidationChainBuilder validationChainBuilder = validationChain as ValidationChainBuilder; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change from var
@@ -70,17 +73,13 @@ public IEnumerable<IValidationFailure> AssertValidLicense() | |||
{ | |||
CompleteValidatorChain(); | |||
|
|||
foreach (var validator in validators) | |||
foreach (ILicenseValidator validator in validators.Where(validator => validator.ValidateWhen == null || validator.ValidateWhen(license)).Where(validator => !validator.Validate(license))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change from var
Changing var lines to explicit type declarations in C# can be beneficial for several reasons. Let's explore why:
|
To be honest, the My bigger point here is that there is already an established coding style in the project and this PR is mainly about changing that style. There are advantages and disadvantages to both styles, and both sides have their adherents. So the point of my comments was not that "this style is wrong", but rather that the existing code (and the project that it is based upon) follow the style of "use var when possible". If you would like to raise it as an issue and have the community discuss it, that would be great, and if others in the project feel like the change would be welcome, then the style could be changed, but it is something that the community should decide. |
Thanks for the follow-up and response. This was a suggestion for improvement. And whatever decision the community makes will be the same. |
@ammarheidari |
@jshergal |
@ammarheidari - I am not a maintainer of the project and thus have no rights to merge PRs. I was simply providing some feedback. |
Syntax improvement and code cleanup