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

Improve the code on throwing exceptions #52

Merged
merged 18 commits into from
Jun 24, 2023
Merged

Improve the code on throwing exceptions #52

merged 18 commits into from
Jun 24, 2023

Conversation

mitsuki31
Copy link
Owner

@mitsuki31 mitsuki31 commented Jun 23, 2023

What's Changed

Options.java

  • Changed the parameter on raiseError and raiseErrorMsg methods. More details.

    Comparison

    Before

    void raiseError(Exception);
    void raiseError(Exception, int);
    void raiseErrorMsg(Exception);
    void raiseError(Exception, int);
    

    After

    void raiseError(Throwable);
    void raiseError(Throwable, int);
    void raiseErrorMsg(Throwable);
    void raiseErrorMsg(Throwable, int);
    

Matrix.java

  • Added new private attribute/member named cause with Throwable as identifier type.
    private static Throwable cause = null;
    
  • Removed all try-catch blocks on several methods.
  • Minor updates on javadoc of several methods.
Commits
  • Improve the raiseError and raiseErrorMsg (a59ca9d)
  • Improve the code on catching exceptions (5cf9f61)
  • Added cause as private attribute (ff14d89)
  • Remove the duplicate cause variables (7c576a3)
  • Remove try-catch on create method (79cf264)
  • Remove try-catch on select method (c7a7ff4)
  • Remove try-catch on change method (2f37a56)
  • Remove try-catch on sum methods (0857039)
  • Remove try-catch on sub methods (b67f3a7)
  • Remove try-catch on mult methods (5ebc23f)
  • Remove try-catch on transpose methods (9f95d4d)
  • Remove try-catch on display methods (304a555)
  • Remove try-catch on several methods (f5e0196)
  • Remove try-catch on get method (ff6a18f)
  • Update the version (forced) (7544291)
  • Update the version (954a041)
  • Update the version (4269773)

Description

Added new (private) attribute named cause with Throwable class as the identifier type.
It's used to holds some exception that could occurs during runtime execution.
So, it makes the code more readable and flexible on catching and throwing the exceptions rather than
using try-catch blocks on everywhere, that makes code more dirty and maybe some peoples
can't understand it.

Removing the try-catch on some methods doesn't make it "a method with non-exceptions", instead it takes the exception and stores it into cause variable (which is private attribute) and then it checks whether the cause is null, if no it will indicates the cause stored some exception and will get raised immediately, otherwise passes it.

Let's take a look this code example below.

public boolean isSquare() {
    /* Here's code that checks the entries of this matrix before comparing its sizes.
     * Rather than using `try-catch` statement that makes code longer,
     * instead create new the exception and store it into `cause` variable.
     */
    if (this.ENTRIES == null) {
        // Create new exception and assign it into `cause`
        cause = new NullMatrixException("the message goes here...");
    }

    /* And here will check the `cause` variable whether it has stored some exception.
     * If `true`, that's indicates the `cause` stored an exception then raise that
     * exception using `raiseError` method, pass otherwise.
     */
    if (cause != null) Options.raiseError(cause);

    return (this.ROWS == this.COLS);
}

* Now `raiseError` and `raiseErrorMsg` uses `Throwable` class for the parameter type instead the `Exception` class.
* Change all parameter names.
* Update the javadocs.
* Remove some unnecessary imported classes.
* Added new variable that stores exception named "cause" with `Throwable` identifier (so it can be used by any exception classes).

This improve readability and reducing code for the exception blocks, by storing specific exception and then get thrown if the variable that stores exception is not null, passes it otherwise.
The `cause` itself will stores the exception temporarily and get thrown later.
Also remove unused else-if block that checks the `selectedIndex` is negative value or not (negative means the user haven't called `select(int)` yet),
this check would never run because to checks the user has select any index row it uses `hasSelect` instead.
This changes included:
  * sum(Matrix)
  * sum(double[][])
  * sum(Matrix, Matrix)
  * sum(double[][], double[][])
This changes included:
  * sub(Matrix)
  * sub(double[][])
  * sub(Matrix, Matrix)
  * sub(double[][], double[][])
This changes included:
  * mult(Matrix)
  * mult(double[][])
  * mult(Matrix, Matrix)
  * mult(double[][], double[][])
  * mult(double)
  * mult(Matrix, double)

Note: The last two is methods that perfoms scalar multiplication.
This changes included:
  * transpose()
  * transpose(double[][])
  * transpose(Matrix)
@mitsuki31 mitsuki31 added enhancement Enhancing existing features lang:java Some changes on Java code patch Patch update labels Jun 23, 2023
@mitsuki31 mitsuki31 self-assigned this Jun 23, 2023
This changes included:
  * display()
  * display(int)
  * display(double[][])
  * display(double[][], int)

Also change the message to display the null or empty two-dimensional array, its changed into "<null_2darray>".
This changes included:
  * isSquare()
  * isSquare(double[][])
  * isSquare(Matrix)
  * isDiagonal()
  * isDiagonal(double[][])
  * isDiagonal(Matrix)
  * sort()
  * sort(double[][])
  * sort(Matrix)
* @see java.lang.Throwable
* @see java.lang.Throwable#printStackTrace
*/
final public static void raiseError(final Throwable cause) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Changed the raiseError parameter from Exception ex to Throwable cause, so it can be used by any type of exceptions either RuntimeException and Exception.

This also changed on these following methods:

  • raiseError(Exception, int) -> raiseError(Throwable, int)
  • raiseErrorMsg(Exception) -> raiseErrorMsg(Throwable)
  • raiseErrorMsg(Exception, int) -> raiseErrorMsg(Throwable, int)

@mitsuki31 mitsuki31 changed the title Improve the code on catching exceptions Improve the code on throwing exceptions Jun 24, 2023
@mitsuki31 mitsuki31 added minor Minor update and removed patch Patch update labels Jun 24, 2023
From `v1.0.0`  to `v1.1.0`
From `v1.0.0`  to `v1.1.0`
From `v1.0.0` to `v1.1.0
@mitsuki31 mitsuki31 marked this pull request as ready for review June 24, 2023 12:35
@mitsuki31 mitsuki31 merged commit fbb9dfb into master Jun 24, 2023
@mitsuki31 mitsuki31 deleted the develop branch July 3, 2023 08:12
@mitsuki31 mitsuki31 restored the develop branch July 3, 2023 08:12
@mitsuki31 mitsuki31 deleted the develop branch July 3, 2023 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancing existing features lang:java Some changes on Java code minor Minor update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant