Next, let's extend the example above by implementing the transition from one stage of a pizza order to another. We can define constructors, methods, and fields inside enum types, which makes them very powerful. What Is Clean Code So, before we jump into the details of clean code, let's understand what do we mean by clean code. Further, we'll see if there are any tools available to help us out. We'll also understand why clean code is important and how to achieve that in Java. The compiler will flag the comparison as an incompatibility error: Overview In this tutorial, we'll go through clean coding principles. We avoid this issue by using the “=” operator. This is because the values of the enum and the getStatus method coincidentally are the same however, logically the comparison should be false. If(testPz.getStatus() = ) Īs for compile-time safety, let's look at an example where we'll determine that an enum of a different type is equal by comparing it using the equals method. Conversely, if we use the equals method, we will get a NullPointerException: if(testPz.getStatus().equals()) Either value can be null and we won't get a NullPointerException. Furthermore, the “=” operator provides compile-time and run-time safety.įirst, we'll look at run-time safety in the following snippet, where we'll use the “=” operator to compare statuses. Since enum types ensure that only one instance of the constants exist in the JVM, we can safely use the “=” operator to compare two variables, like we did in the above example.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |