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

refactoring cyclomatic complexity code smells #145

Closed
wants to merge 2 commits into from
Closed

refactoring cyclomatic complexity code smells #145

wants to merge 2 commits into from

Conversation

vinicius-lemos
Copy link

refactored cyclomatic complexity code smell #144

Copy link
Owner

@ktuukkan ktuukkan left a comment

Choose a reason for hiding this comment

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

Although I assume good intentions and a genuine attempt to fix certain code smells, I unfortunately must conclude that this PR adds other smells in exchange, introduces probable bugs and breaks the build by removing the package/import declarations. In order to approve the PR, all these should be fixed and the missing license/copyright notices must be restored.

public static boolean isCorrect(long value) {
return (MINVALUE <= value) && (value <= MAXVALUE);
}
private static final String[] REGION_DESCRIPTIONS = {
Copy link
Owner

@ktuukkan ktuukkan Nov 20, 2023

Choose a reason for hiding this comment

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

I'd rather use Map here to make it explicit instead of trusting array indices, think of what happens if one line is accidentally removed or the order is changed. Mapping will probably make toString a bit easier too, e.g. by removing the need to check if the given value falls within valid range.

@@ -33,74 +33,52 @@ public class NavAidType {
* @param deviceType Device type value to Stringify.
* @return a text string describing the Nav Aid type
Copy link
Owner

@ktuukkan ktuukkan Nov 20, 2023

Choose a reason for hiding this comment

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

An array doesn't "return" anything and doesn't have deviceType parameter, as it is not a method. It simply holds values that can be retrieved by an index. This is also a rather simple private field, so perhaps just // NavAid mapping or such would be sufficient, if need be.

Comment on lines +40 to +46
"not used",
"not used",
"not used",
"not used",
"not used",
"not used",
"not used",
Copy link
Owner

Choose a reason for hiding this comment

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

Now that we are into static analysis and fixing code smells: Don't Repeat Yourself (DRY).

This is basically just wasted memory and again could be easily avoided by using an explicit mapping.

Comment on lines -1 to -27
/*
* ShipType.java
* Copyright (C) 2015-2020 Lázár József, Joshua Sweaney
*
* This file is part of Java Marine API.
* <http://ktuukkan.github.io/marine-api/>
*
* Java Marine API is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Java Marine API is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Java Marine API. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.marineapi.ais.util;

/**
* Checks the ship type for validity.
*
* @author Lázár József, Joshua Sweaney
*/
Copy link
Owner

Choose a reason for hiding this comment

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

When working on code that you don't own, especially when using someone else's work for your own benefit, the licensing and copyright info shall never be removed or altered. In addition, the package declaration is also gone.

Comment on lines +19 to +25
handleRMCSentence((RMCSentence) s, p, sog, cog, d, t, mode);
} else if (s instanceof VTGSentence) {
handleVTGSentence((VTGSentence) s, sog, cog);
} else if (s instanceof GGASentence) {
handleGGASentence((GGASentence) s, p, d, t, fix);
} else if (s instanceof GLLSentence) {
handleGLLSentence((GLLSentence) s, p);
Copy link
Owner

@ktuukkan ktuukkan Nov 20, 2023

Choose a reason for hiding this comment

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

Ok, some more on code smells: re-assignment on parameters is generally considered a bad practice. Depending on your config, this is something that also PMD could warn you about.

Moreover, your intent is to make changes to variables declared in other scope, which is a side effect and thus also a bad practice (see orthogonality and Law of Demeter).

Finally, your approach doesn't work in Java because the parameters are passed in by-value instead of by-reference. Hence, assigning a new value to method parameter does not overwrite the variable declared in the calling scope.

* @see net.sf.marineapi.provider.event.PositionListener
* @see net.sf.marineapi.provider.event.PositionEvent
* @see net.sf.marineapi.nmea.io.SentenceReader
*/
Copy link
Owner

Choose a reason for hiding this comment

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

Why removed these?

@@ -21,42 +21,67 @@
package net.sf.marineapi.ais.util;

/**
* Checks the positioning device type for validity.
* Provides functionality related to positioning devices.
Copy link
Owner

Choose a reason for hiding this comment

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

How does the changed comment make an improvement here? As compared to the original, I find it now more abstract and vague, telling less about the purpose of this class.

@vinicius-lemos vinicius-lemos closed this by deleting the head repository Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants