2 .pr_agent_auto_best_practices
root edited this page 2025-07-26 14:20:23 +00:00

Pattern 1: Add null/undefined checks before accessing object properties or calling methods to prevent runtime exceptions. This includes checking if variables, object references, or array elements exist before performing operations on them.

Example code before:

const result = someObject.property;
someRef.current.method();

Example code after:

if (someObject && someObject.property) {
    const result = someObject.property;
}
if (someRef.current) {
    someRef.current.method();
}
Relevant past accepted suggestions:
Suggestion 1:

Fix null reference exception risk

The null check for schema should occur before accessing schema.PrimaryKey to prevent potential null reference exceptions. Move the null validation to the beginning of the constructor.

WelsonJS.Toolkit/WelsonJS.Esent/EsentDatabase.cs [35-41]

 public EsentDatabase(Schema schema, string workingDirectory, ICompatibleLogger logger = null)
 {
+    if (schema == null)
+        throw new ArgumentNullException(nameof(schema));
+
     _logger = logger ?? new TraceLogger();
-
     _primaryKey = schema.PrimaryKey;
 
-    if (schema == null)
-

Suggestion 2:

Fix grammar and add validation

The comment contains a grammatical error ("an unwanted features" should be "unwanted features"). Additionally, consider adding validation to ensure disableFeatures is an array before calling join() to prevent runtime errors.

lib/chrome.js [323-324]

-// disable an unwanted features
-cmd.push("--disable-features=" + this.disableFeatures.join(','));
+// disable unwanted features
+if (Array.isArray(this.disableFeatures) && this.disableFeatures.length > 0) {
+    cmd.push("--disable-features=" + this.disableFeatures.join(','));
+}

Suggestion 3:

Add null check

The function doesn't check if promptEditorRef.current exists before calling methods on it, which could lead to runtime errors if the ref isn't initialized.

WelsonJS.Toolkit/WelsonJS.Launcher/editor.html [195-202]

 const invoke = () => {
     try {
-        const updated = promptEditorRef.current.get();
-        promptMessagesRef.current = updated;
+        if (promptEditorRef.current) {
+            const updated = promptEditorRef.current.get();
+            promptMessagesRef.current = updated;
+        }
     } catch (e) {
         console.error("Invalid JSON structure", e);
     }
 };

Pattern 2: Add type checks before performing operations that assume a specific data type, such as parsing JSON strings or calling array methods. Verify the data type matches expectations before proceeding with type-specific operations.

Example code before:

const parsed = JSON.parse(response);
cmd.push("--disable-features=" + features.join(','));

Example code after:

const parsed = typeof response === 'string' ? JSON.parse(response) : response;
if (Array.isArray(features) && features.length > 0) {
    cmd.push("--disable-features=" + features.join(','));
}
Relevant past accepted suggestions:
Suggestion 1:

Fix grammar and add validation

The comment contains a grammatical error ("an unwanted features" should be "unwanted features"). Additionally, consider adding validation to ensure disableFeatures is an array before calling join() to prevent runtime errors.

lib/chrome.js [323-324]

-// disable an unwanted features
-cmd.push("--disable-features=" + this.disableFeatures.join(','));
+// disable unwanted features
+if (Array.isArray(this.disableFeatures) && this.disableFeatures.length > 0) {
+    cmd.push("--disable-features=" + this.disableFeatures.join(','));
+}

Suggestion 2:

Add type check before parsing

The code assumes the response is always a string that needs parsing, but HTTP responses might already be parsed objects. This could cause errors if the response is already a JSON object. Add a type check before parsing.

lib/language-inference-engine.js [217]

-response = JSON.parse(response)
+response = typeof response === 'string' ? JSON.parse(response) : response;

[Auto-generated best practices - 2025-07-26]