Skip to content
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

[refactor] Improve code structure #347 #348

Merged

Conversation

dee077
Copy link
Contributor

@dee077 dee077 commented Mar 7, 2025

Refactored src/js/netjsongraph.js

Fixes #347

Checklist

Reference to Existing Issue

Closes #347.

Description of Changes

Refactored src/js/netjsongraph.js

Refactored src/js/netjsongraph.js

Fixes openwisp#347
@dee077 dee077 force-pushed the issues/347-refactor-netjsongraph.js branch from a239c8f to fe99e6e Compare March 7, 2025 12:41
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

Looks good @dee077! 👍

I have two minor requests for improvement regarding comments below.

class NetJSONGraph {
/**
* @constructor
* Initializes a new NetJSONGraph instance.
Copy link
Member

Choose a reason for hiding this comment

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

Any programmer should know that a constructor initializes a new instance of the class, I think this comment can be removed.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Initializes a new NetJSONGraph instance.

Please remove this. Not useful.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

Please read https://stackoverflow.blog/2021/12/23/best-practices-for-writing-code-comments/

I want to underline:

Rule 1: Comments should not duplicate the code.

The point is to explain what the comment achieves from a functional perspective or from the user perspective, eg: this code block implements the switching from geographic map to topology chart.

Rule 3: If you can't write a clear comment, there may be a problem with the code.

Rule 4: Comments should dispel confusion, not cause it.

Rule 5: Explain unidiomatic code in comments.

Rule 8: Add comments when fixing bugs.

Rule 9: Use comments to mark incomplete implementations.

And I would add: use comments when implementing workaround for issues, eg:

// This code works around a nasty bug in component X, TODO: solve the root cause,
// github issue link https://github.com/openwisp/.........

class NetJSONGraph {
/**
* @constructor
* Initializes a new NetJSONGraph instance.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Initializes a new NetJSONGraph instance.

Please remove this. Not useful.

@dee077
Copy link
Contributor Author

dee077 commented Mar 15, 2025

Thanks for the clarification! I'll update the comments accordingly.

return {
...config,
render:
config.render === "map"
Copy link
Member

Choose a reason for hiding this comment

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

@dee077 & @nemesifier
I found this in the readme

In extreme cases, you can also pass your own render function if you don't want Echarts to render. We will pass in the processed netjson data and netjsongraph object.

https://github.com/openwisp/netjsongraph.js?tab=readme-ov-file#configuration-instructions

I think, config can be undefined and user can pass custom render function so this need to be handled.

Copy link
Member

@niteshsinha17 niteshsinha17 left a comment

Choose a reason for hiding this comment

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

Looks good to me. Left few comments.

* @param {Object} config - The user-defined configuration.
* @returns {Object} - The final configuration object.
*/
initializeConfig(config) {
initializeConfig(config = {}) {
Copy link
Member

Choose a reason for hiding this comment

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

I am not 100% sure if in Javascript this is an antipattern how in python is, but we have been bitten quite a few times by this issue in Python: https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

Are you sure this is not goint to be problematic here? Are you sure this change is really necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went through this doc
Evaluated at call time
The default argument is evaluated at call time. Unlike with Python (for example), a new object is created each time the function is called.
With this, I don't think there would be any problem.

@@ -52,9 +49,6 @@ class NetJSONGraph {
};
}

/**
* Sets up rendering utilities, GUI, and event handling. Used in constructor
*/
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't remove this as it mentions quite a few interesting details which are good to know for someone who doesn't know the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added it!

@@ -64,19 +58,15 @@ class NetJSONGraph {
}

/**
* Initializes the ECharts rendering engine. Used in constructor
* Used in example Switch render mode
Copy link
Member

Choose a reason for hiding this comment

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

let's not mention the examples in the main code, the main code must not know about nor depend on the examples, it's the opposite: the examples depend on and know about the core netjsongraph.js code.

this.echarts.clear();
this.config.render = this.utils.mapRender;
this.utils.mapRender(this.data, this);

// Show Leaflet UI elements when back in map mode
Copy link
Member

Choose a reason for hiding this comment

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

I'd explain that we are showing again the credits and zoom control buttons when in map mode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comment on both cases

if (this.config.switchMode && this.type === "netjson") {
this.gui.renderModeSelector.onclick = () => {
// Switch from map to graph mode, first clear canvasContainer and then render
Copy link
Member

Choose a reason for hiding this comment

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

This one wasn't bad!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Readded it!

@nemesifier nemesifier merged commit d5aa5a1 into openwisp:master Mar 21, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[refactor] Improve src/js/netjsongraph.js for more readability and maintainability
3 participants