Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I improve readability and length of a method with many if statements?

I have a method with 195 ifs. Here is a shorter version:

private BigDecimal calculateTax(String country, BigDecimal amount) throws Exception {     if(country.equals("POLAND")){         return new BigDecimal(0.23).multiply(amount);     }     else if(country.equals("AUSTRIA")) {         return new BigDecimal(0.20).multiply(amount);     }     else if(country.equals("CYPRUS")) {         return new BigDecimal(0.19).multiply(amount);     }     else {         throw new Exception("Country not supported");     } } 

I can change ifs to switches:

private BigDecimal calculateTax(String country, BigDecimal amount) throws Exception {     switch (country) {         case "POLAND":             return new BigDecimal(0.23).multiply(amount);         case "AUSTRIA":             return new BigDecimal(0.20).multiply(amount);         case "CYPRUS":             return new BigDecimal(0.19).multiply(amount);         default:             throw new Exception("Country not supported");     } } 

but 195 cases is still so long. How could I improve readability and length of that method? What pattern would be the best in this case?

like image 757
Michu93 Avatar asked Jun 13 '19 18:06

Michu93


People also ask

Can I use multiple IF statements in Java?

You can make multiple if statements behave like a single if else-if .. else statement if each of the condition blocks breaks out of the block that contains the if statements (for example, by returning from the method or breaking from a loop).

How do you determine the readability of a text?

In order to accurately determine a text’s readability, many factors need to be considered and calculated. Precise computer algorithms are formulated to analyze multiple components of a text including word count, sentence length, characters per word, etc. Several of the most widely used readability scoring methods are listed and explained below.

How much do shorter sentences affect readability?

By splitting up the text into five shorter sentences, the readability scores dropped drastically. Now, the Flesch-Kincaid Grade Level is dropped by 14.3 points to a much more reasonable 14.1. Similarly, the Gunning-Fog score dropped by 12.8 points to a new score of 18.4.

What are the components of readability?

Along with sentence length, the other key component of readability formula is word length. For some tests such as Flesch- Kincaid, calculate word length on the number of syllables. For others such as Coleman-Liau, word length is calculated on letter count.

What is readability and why is it important in writing?

By ensuring the readability of a piece of text – a patient care guide, a promotional piece for a particular brand, a policy – you increase the chances of your message being understood by your reader. If you are writing text for the general public you should aim for a grade level score of around 8.


1 Answers

Create a Map<String,Double> that maps country names to their corresponding tax rates:

Map<String,Double> taxRates = new HashMap<> (); taxRates.put("POLAND",0.23); ... 

Use that Map as follows:

private BigDecimal calculateTax(String country, BigDecimal amount) throws Exception {     if (taxRates.containsKey(country)) {         return new BigDecimal(taxRates.get(country)).multiply(amount);     } else {         throw new Exception("Country not supported");     } } 
like image 194
Eran Avatar answered Oct 09 '22 00:10

Eran