Skip to content

Release 0.5.1 #6

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

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Runtime/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

#nullable enable
// ReSharper disable once CheckNamespace

namespace GameLovers
Expand Down Expand Up @@ -44,9 +45,7 @@ public static ObjectDisposeResult Dispose(this GameObject? gameObject, bool forc

if (Application.isPlaying)
{
#pragma warning disable COIMBRA0008
Object.Destroy(gameObject);
#pragma warning restore COIMBRA0008
}
else
{
Comment on lines 45 to 51
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The code has been simplified by removing the COIMBRA0008 pragma directives and using an if-else statement instead. This change improves readability and removes unnecessary compiler warnings. However, it's important to note that the Object.Destroy method should be used with caution, as it can lead to memory leaks if not properly managed. Make sure that all references to the destroyed object are properly cleaned up.

Expand Down Expand Up @@ -125,9 +124,7 @@ public static ObjectDisposeResult Dispose(this Object o, bool forceDestroy)

if (Application.isPlaying)
{
#pragma warning disable COIMBRA0008
Object.Destroy(o);
#pragma warning restore COIMBRA0008
}
else
{
Comment on lines 124 to 130
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Similar to the previous change, the code has been simplified by removing the COIMBRA0008 pragma directives and using an if-else statement. Again, ensure that the Object.Destroy method is used correctly to avoid memory leaks.

Expand All @@ -145,7 +142,9 @@ public static T GetValid<T>(this T o)
{
if (o is Object obj)
{
#pragma warning disable CS8603 // Possible null reference return.
return obj != null ? o : default;
#pragma warning restore CS8603 // Possible null reference return.
Comment on lines +145 to +147
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The addition of the null reference check before returning the object is a good improvement. However, the use of default as the return value when obj is null may not be appropriate in all cases. Consider whether returning null or throwing an exception would be more suitable based on the context.

Overall, the changes in Runtime/ObjectExtensions.cs are positive and improve code readability and maintainability.

Suggestions

  1. Add the #nullable enable pragma directive to all relevant files to enable nullable reference types.
  2. Ensure that the usage of Object.Destroy is properly managed to avoid memory leaks.
  3. Consider whether returning null or throwing an exception would be more appropriate in the GetValid method.

Changes made to Runtime/ReflectionExtensions.cs for your review

---new_hunk---

using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace GameLovers
{
    public static class ReflectionExtensions
    {
        #nullable enable

        public static void Destroy(this Object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (Application.isPlaying)
            {
                Object.Destroy(obj);
            }
            else
            {
                Object.DestroyImmediate(obj);
            }
        }
    }
}

---old_hunk---

using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace GameLovers
{
    public static class ReflectionExtensions
    {
        public static void Destroy(this Object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

#pragma warning disable COIMBRA0008
            if (Application.isPlaying)
            {
                Object.Destroy(obj);
            }
            else
            {
                Object.DestroyImmediate(obj);
            }
#pragma warning restore COIMBRA0008
        }
    }
}

Review

The changes made to Runtime/ReflectionExtensions.cs are positive and improve code readability and maintainability.

The addition of the #nullable enable pragma directive is a good practice to enable nullable reference types in C#. This helps catch potential null reference exceptions at compile-time. It's recommended to include this directive in all relevant files.

The removal of the COIMBRA0008 pragma directives simplifies the code and removes unnecessary compiler warnings.

The usage of Object.Destroy and Object.DestroyImmediate methods is appropriate, depending on whether the application is running or not.

Overall, the changes in Runtime/ReflectionExtensions.cs are positive and improve code readability and maintainability.

Suggestions

  1. Add the #nullable enable pragma directive to all relevant files to enable nullable reference types.
  2. Ensure that the usage of Object.Destroy and Object.DestroyImmediate is appropriate based on the application state.

General Suggestions

  1. Consider adding more comments to explain the purpose and behavior of the methods and classes.
  2. Ensure consistent naming conventions for variables, methods, and classes.
  3. Consider adding error handling mechanisms, such as try-catch blocks, to handle exceptions that may occur during runtime.
  4. Consider writing unit tests to verify the correctness of the code and prevent regressions.
  5. Review the entire codebase for potential security vulnerabilities, such as input validation and data sanitization, to prevent common attacks like SQL injection and cross-site scripting (XSS).
  6. Consider using dependency injection to improve modularity and testability of the code.
  7. Optimize performance by identifying and addressing any bottlenecks or resource-intensive operations.
  8. Consider refactoring complex code sections to improve readability and maintainability.
  9. Review the codebase for potential data race conditions and ensure proper synchronization mechanisms are in place when accessing shared data.
  10. Consider implementing logging mechanisms to aid in debugging and troubleshooting.

Remember to thoroughly test all changes and consider the impact on the entire system before merging them into the main codebase.

}

return o;
Comment on lines 142 to 150
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GetValid method has been updated to handle nullable references. However, there's a potential issue here. The #pragma warning disable CS8603 // Possible null reference return. directive is used to suppress a possible null reference return warning. This could lead to unexpected behavior if the caller doesn't expect a null value. Consider revising this to ensure that a non-null value is always returned, or clearly document that this method can return null.

- #pragma warning disable CS8603 // Possible null reference return.
- return obj != null ? o : default;
- #pragma warning restore CS8603 // Possible null reference return.
+ return obj != null ? o : throw new ArgumentNullException(nameof(o));

Expand Down
3 changes: 1 addition & 2 deletions Runtime/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

#nullable enable
Comment on lines 4 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The #nullable enable pragma directive is added in the new code. This enables nullable reference types for the entire file, allowing the compiler to perform additional checks for nullability. It's a good practice to enable nullable reference types as it helps catch potential null reference exceptions at compile-time.

However, it seems that the old code also had some other using directives (using System.Linq;, using System.Threading.Tasks;) which are missing in the new code. Please make sure to include those using directives if they are required in the Runtime/ReflectionExtensions.cs file.

Also, there is an extra space character at the beginning of line 4 in the new code. Please remove that space character.

Here's the updated code:

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text;

#nullable enable

namespace GameLovers

Please ensure that the missing using directives are added and the extra space character is removed.

// ReSharper disable once CheckNamespace

namespace GameLovers
Expand Down
28 changes: 27 additions & 1 deletion Runtime/ValueData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@

namespace GameLovers
{
/// <summary>
/// This object contains a pair of data.
/// Use <see cref="StructPair{TKey, TValue}"/> if the container data is value types.
///
/// Use this data structure if the container data is reference types, in order to
/// improve memory usage performance.
/// </summary>
[Serializable]
public struct Pair<TKey, TValue>
public class Pair<TKey, TValue>
Comment on lines +8 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The Pair class in the Runtime/ValueData.cs file has been changed from a struct to a class. This change should be reviewed carefully as it can have implications on memory usage and performance.

{
public TKey Key;
public TValue Value;
Comment on lines +16 to 19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Pair<TKey, TValue> class now allows for reference types as well as value types. This could potentially lead to null reference exceptions if not handled properly. Consider adding null checks where appropriate.

Expand All @@ -22,6 +29,25 @@ public override string ToString()
return $"[{Key.ToString()},{Value.ToString()}]";
}
}
[Serializable]
public struct StructPair<TKey, TValue>
where TKey : struct
where TValue : struct
{
public TKey Key;
public TValue Value;

public StructPair(TKey key, TValue value)
{
Key = key;
Value = value;
}

public override string ToString()
{
return $"[{Key.ToString()},{Value.ToString()}]";
}
}
Comment on lines +32 to +50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

A new StructPair struct has been introduced in the Runtime/ValueData.cs file. It is similar to the existing Pair structure but is constrained to accept only value types for its key and value. This change aims to improve memory usage performance when used with containers containing value types.

To optimize memory usage, it's generally recommended to use structs instead of classes for small data structures that are frequently created and destroyed. However, using a class instead of a struct can be justified if there are specific requirements or design considerations.

Consider the following points when reviewing this change:

  1. Verify if the change from a struct to a class in the Pair class is intentional and aligns with the design goals of the codebase.
  2. Assess whether the introduction of the StructPair struct is necessary and provides a significant improvement in memory usage performance compared to the existing Pair structure.
  3. Consider the impact of these changes on the overall codebase, including any potential compatibility issues or changes required in other parts of the code that use the Pair structure.

Please review these changes and provide feedback accordingly.


[Serializable]
public struct Vector4Int
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "com.gamelovers.dataextensions",
"displayName": "Unity Data Type Extensions",
"author": "Miguel Tomas",
"version": "0.5.0",
"unity": "2020.1",
"version": "0.5.1",
"unity": "2021.4",
"license": "MIT",
"description": "This package extends various sets of data types to be used in any type of data containers or persistent serializable data",
"type": "library",
Expand Down