From b7af9f2a5d90043cd2e71fd01cbe6a68a08bcb43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6kalp=20G=C3=BCrb=C3=BCzer?= Date: Sun, 16 Sep 2018 21:58:04 +0300 Subject: [PATCH] code-review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit yordamın adı `run` yerine `slugify` olmalı, `run`, `Runnable`'dan türediği izlenimini yaratıyor. sınıfın yordamlarının tamamı static olduğu için `private` ctor eklendi - yeni bir instance yaratılmasını engellemek için `String#replaceAll` satırlarındaki string atamaları gereksiz. ayrıca gönderilen orijinal metni değiştirmek yerine başka bir metin referansı dönmek daha anlamlı olur -metnin orijinali sonraki satırlarda kullanılmak istenebilir. bunun dışında, kütüphane olarak kullanılacak bir kodun karşılaştığı hataları (sarmalayarak ya da sarmalamadan) fırlatması daha doğru olur `convertCharactersFromTurkishToEnglish` yordamı `private` olabilirdi, yapıldı `String#length` control yerine `String#isEmpty` önerilir --- .../src/jug/istanbul/slugifier/Slugifier.java | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/workshop-1/jugistanbul-slugifier/src/jug/istanbul/slugifier/Slugifier.java b/workshop-1/jugistanbul-slugifier/src/jug/istanbul/slugifier/Slugifier.java index 8a8bb76..44bd1ed 100644 --- a/workshop-1/jugistanbul-slugifier/src/jug/istanbul/slugifier/Slugifier.java +++ b/workshop-1/jugistanbul-slugifier/src/jug/istanbul/slugifier/Slugifier.java @@ -6,48 +6,39 @@ public class Slugifier { - public static String run ( String text ) + private Slugifier() { + } + + public static String slugify ( String text ) throws UnsupportedEncodingException { - if ( text == null || text.length () == 0 ) + if ( text == null || text.isEmpty() ) { return ""; } - String toReturn = ""; - try - { - String normalizedText = normalize ( text ); - normalizedText = normalizedText.replaceAll ( "[^\\w\\s\\-]", "" ); - normalizedText = normalizedText.replaceAll ( " ", "-" ); - normalizedText = normalizedText.toLowerCase (); - toReturn = URLEncoder.encode ( normalizedText, "UTF-8" ); - } - catch ( UnsupportedEncodingException e ) - { - // TODO: Log the exception - e.printStackTrace (); - } - - return toReturn; + String normalizedText = normalize ( text ) + .replaceAll ( "[^\\w\\s\\-]", "" ) + .replaceAll ( " ", "-" ) + .toLowerCase (); + return URLEncoder.encode ( normalizedText, "UTF-8" ); } - public static String convertCharactersFromTurkishToEnglish ( String text ) + private static String convertCharactersFromTurkishToEnglish ( String text ) { - text = text.replaceAll ( "ü", "u" ); - text = text.replaceAll ( "ı", "i" ); - text = text.replaceAll ( "ö", "o" ); - text = text.replaceAll ( "ü", "u" ); - text = text.replaceAll ( "ş", "s" ); - text = text.replaceAll ( "ğ", "g" ); - text = text.replaceAll ( "ç", "c" ); - text = text.replaceAll ( "Ü", "U" ); - text = text.replaceAll ( "İ", "I" ); - text = text.replaceAll ( "Ö", "O" ); - text = text.replaceAll ( "Ü", "U" ); - text = text.replaceAll ( "Ş", "S" ); - text = text.replaceAll ( "Ğ", "G" ); - text = text.replaceAll ( "Ç", "C" ); - return text; + return text.replaceAll ( "ü", "u" ) + .replaceAll ( "ı", "i" ) + .replaceAll ( "ö", "o" ) + .replaceAll ( "ü", "u" ) + .replaceAll ( "ş", "s" ) + .replaceAll ( "ğ", "g" ) + .replaceAll ( "ç", "c" ) + .replaceAll ( "Ü", "U" ) + .replaceAll ( "İ", "I" ) + .replaceAll ( "Ö", "O" ) + .replaceAll ( "Ü", "U" ) + .replaceAll ( "Ş", "S" ) + .replaceAll ( "Ğ", "G" ) + .replaceAll ( "Ç", "C" ); } private static String normalize ( String input )