-2
\$\begingroup\$

How to know if my code is spaghetti or not?

Is this is spaghetti code?

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {

                ((Core) getActivity()).emailCore();
                ((Core) getActivity()).messengerSharer();

            } catch (NumberFormatException e) {
                e.printStackTrace();

                Toast.makeText(getActivity(),
                        R.string.text_field_toast, Toast.LENGTH_SHORT)
                        .show();

            } catch (Exception e) {
                e.printStackTrace();

                Toast.makeText(getActivity(),
                        R.string.text_field_toast, Toast.LENGTH_SHORT)
                        .show();

            } catch (OutOfMemoryError error) {
                error.printStackTrace();

                Toast.makeText(getActivity(),
                        Html.fromHtml(getString(R.string.html_toast)), Toast.LENGTH_LONG)
                        .show();

            } catch (Error e) {
                e.printStackTrace();

                Toast.makeText(getActivity(),
                        R.string.text_field_toast, Toast.LENGTH_SHORT)
                        .show();

            }
        }
    });

And how to write clean and good java code?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question. \$\endgroup\$ Commented Nov 24, 2015 at 12:08
  • \$\begingroup\$ It's event for button click in android and it's from old app i made. I just give example to know how to write clean code and make it readable to anyone will mess with my code later \$\endgroup\$
    – Stephan
    Commented Nov 24, 2015 at 12:13
  • 1
    \$\begingroup\$ Please add the explanation into your question, by clicking the edit link below it. \$\endgroup\$
    – Phrancis
    Commented Nov 24, 2015 at 12:38
  • \$\begingroup\$ No, it is a pasta code. Lol. Anyway - no - a spaghetti code is an incomprehensible code at all. But you can "optimize" it ofcourse (there isn't so much to say with this code). \$\endgroup\$ Commented Nov 24, 2015 at 12:43

2 Answers 2

2
\$\begingroup\$
catch (OutOfMemoryError error)
{    
}
catch (Error e)
{
}

Errors should never be caught in Java, only exceptions. An error differs from an exception in the sense that an error is the kind of problem that means something went really wrong and not just something was faulty handled in your application.

Take another look at the first one you catch: OutOfMemoryError. This literally means your app has ran out of memory. By definition, you can't do anything anymore which includes error handling.

Maybe the official documentation speaks for itself here:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

\$\endgroup\$
4
  • \$\begingroup\$ Depends on the error. A NoClassDefError means a class available at compile time is no longer available. A UnsatisfiedLinkError means a native library cannot be used (it can't be found, or it's missing a dependency). You can catch both and decide to use a workaround. But I agree that catching errors should be done with very much caution. And OutOfMemoryErrors should never be caught at all. \$\endgroup\$ Commented Nov 24, 2015 at 12:41
  • \$\begingroup\$ The Android Studio forced me to catch this error \$\endgroup\$
    – Stephan
    Commented Nov 24, 2015 at 12:41
  • \$\begingroup\$ @DrProgrammer fair enough,, there are some exceptions to it. Though for the purpose of not confusing new devs, I'll leave it in as a golden rule. \$\endgroup\$ Commented Nov 24, 2015 at 12:47
  • \$\begingroup\$ @Stephan: forced? Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions. (from the docs) \$\endgroup\$ Commented Nov 24, 2015 at 12:48
0
\$\begingroup\$

How to know if my code is spaghetti or not?

There are GoF Patterns and OOD-Principles (SOLID is an couple of good working principles) agains "Spaghetti-Code".

Is this is spaghetti code?

A little bit.

And how to write clean and good java code?

  1. Find OOD-Principles you like to use in parts of your project.
  2. Order them by importance
  3. Energy Mr. Scott.
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.