CLEAN CODE 6 11st Front Dev. Team
6 1. 2. 3. checked exception 4. 5. 6.
11 : 2 4 : java (50%), javascript (35%), SQL/PL-SQL (15%) : Spring, ibatis, Oracle, jquery
?
, (, ) ( )
클린코드를 무시한다면
. 6
1.
,,,! ( : http://javacan.tistory.com/entry/oo-basic-2-abstractionandpolymorphism)
,
,
.. ( ),.
. public List<ListingProduct> getadproducts(listingrequest listingrequest) { List<ListingProduct> adproducts = adproductservice.getadproducts(listingrequest); } List<ListingProduct> alternatives = searchservice.search(listingrequest).getproducts(); if (type.equals(power_click)) alternatives = alternatives.sublist(0,5); if (type.equals(chance_shopping)) alternatives = alternatives.sublist(5,10); if (type.equals(all_kill)) alternatives = alternatives.sublist(10,15); if (type.equals(premium)) alternatives = alternatives.sublist(15,20); int limit = 0; if( listingrequest.getviewtype().equals(image) ) { if(listingrequest.istab() && (adproducts.size() % 4)!= 0){ limit = 4 - ( adproducts.size() % 4 ); } else { if ((adproducts.size() % 2)!= 0){ limit = 1; } } } else if (listingrequest.getviewtype().equals(bigimage)){ if(listingrequest.istab() && (adproducts.size() % 2)!= 0){ limit = 1; } } return concatexceptduplicate(alternatives, limit, adproducts );.. +
@Override public List<ListingProduct> getadproductandalternatives(listingrequest listingrequest) { List<ListingProduct> adproducts = adproductservice.getadproducts(listingrequest); List<ListingProduct> alternatives = getalternativeproducts(listingrequest, adproducts); adproducts.addall(alternatives); return adproducts; } private List<ListingProduct> getalternativeproducts (ListingRequest listingrequest, List<ListingProduct> adproducts) { List<ListingProduct> alternatives = getalternativeproductfilteredbytype(listingrequest); int limit = calculatelimit(listingrequest, adproducts); alternatives = alternatives.sublist(0, limit); return alternatives; }
private List<ListingProduct> getalternativeproductfilteredbytype(listingrequest listingrequest) { List<ListingProduct> alternatives = searchservice.search(listingrequest).getproducts(); if (type.equals(power_click)) alternatives = alternatives.sublist(0,5); if (type.equals(chance_shopping)) alternatives = alternatives.sublist(5,10); if (type.equals(all_kill)) alternatives = alternatives.sublist(10,15); if (type.equals(premium)) alternatives = alternatives.sublist(15,20); return alternatives; } private int calculatelimit(listingrequest listingrequest, List<ListingProduct> adproducts) { int limit = 0; if( listingrequest.getviewtype().equals(image) ) { if(listingrequest.istab() && (adproducts.size() % 4)!= 0){ limit = 4 - ( adproducts.size() % 4 ); } else { if ((adproducts.size() % 2)!= 0){ limit = 1; } } } else if (listingrequest.getviewtype().equals(bigimage)){ if(listingrequest.istab() && (adproducts.size() % 2)!= 0){ limit = 1; } } return limit; }
., ( ).,..
2.
,, ( ).,,.
....
...
http://geeksquiz.com/heap-sort/
OLD HEAP SORT ublic void sort(int arr[]) int n = arr.length; // for (int i = n / 2-1; i >= 0; i--) heapify(arr, n, i); // for (int i=n-1; i>=0; i--) { //. int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; } //. heapify(arr, i, 0); // i. // n. void heapify(int arr[], int n, int i) { int largest = i; // largest int l = 2*i + 1; // left. int r = 2*i + 2; // right. } // if (l < n && arr[l] > arr[largest]) largest = l; // if (r < n && arr[r] > arr[largest]) largest = r; // largest if (largest!= i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; } //. heapify(arr, n, largest);
n, l, r?...
HEAP SORT REFACTORING public void sort(int arr[]) { int n = arr.length; } // for (int i = n / 2-1; i >= 0; i--) heapify(arr, n, i); // for (int i=n-1; i>=0; i--) { //. int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; } //. heapify(arr, i, 0); public void sort(int arr[]) { buildheap(arr); heapsort(arr); } private void buildheap(int[] arr) { int sizeofheap = arr.length; for (int i = sizeofheap / 2-1; i >= 0; i--) heapify(arr, sizeofheap, i); } private void heapsort(int[] arr) { int sizeofheap = arr.length; // for (int i=sizeofheap-1; i>=0; i--) { //. int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; //. heapify(arr, i, 0); } }
HEAP SORT REFACTORING public void sort(int arr[]) { buildheap(arr); heapsort(arr); } private void buildheap(int[] arr) { int sizeofheap = arr.length; for (int i = sizeofheap / 2-1; i >= 0; i--) heapify(arr, sizeofheap, i); } private void heapsort(int[] arr) { int sizeofheap = arr.length; // for (int i=sizeofheap-1; i>=0; i--) { //. int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; //. heapify(arr, i, 0); } } private final int ROOT_INDEX = 0; public void sort(int arr[]) { buildheap(arr); heapsort(arr); } private void buildheap(int[] arr) { int sizeofheap = arr.length; for (int i = sizeofheap / 2-1; i >= 0; i--) heapify(arr, sizeofheap, i); } private void heapsort(int[] arr) { int sizeofheap = arr.length; for (int i = sizeofheap-1; i >= 0; i--) { moveroottoend(arr, i); heapify(arr, i, 0); } } private void moveroottoend(int[] arr, int endindex) { int temp = arr[root_index]; arr[root_index] = arr[endindex]; arr[endindex] = temp; }
HEAP SORT REFACTORING // i. // n. void heapify(int arr[], int n, int i) { int largest = i; // largest int l = 2*i + 1; // left. int r = 2*i + 2; // right. // if (l < n && arr[l] > arr[largest]) largest = l; // if (r < n && arr[r] > arr[largest]) largest = r; // largest if (largest!= i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; //. heapify(arr, n, largest); } } void heapify(int arr[], int heapsize, int rootindex) { int largest = getlargestsubroot( arr, heapsize, rootindex); if (largest!= rootindex) { swap(arr, rootindex, largest); heapify(arr, heapsize, largest); } } private int getlargestsubroot( int[] arr, int heapsize, int rootindex) { int largest = rootindex; int leftsubroot = 2*rootIndex + 1; int rightsubroot = 2*rootIndex + 2; if (leftsubroot < heapsize && arr[leftsubroot] > arr[largest]) largest = leftsubroot; if (rightsubroot < heapsize && arr[rightsubroot] > arr[largest]) largest = rightsubroot; return largest; } private void swap (int[] arr, int left, int right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; }
NEW HEAP SORT public class HeapSort { private final int ROOT_INDEX = 0; public void sort(int arr[]) { buildheap(arr); heapsort(arr); } private void buildheap(int[] arr) { int sizeofheap = arr.length; for (int i=sizeofheap/2-1; i>=0; i--) heapify(arr, sizeofheap, i); } private void heapsort(int[] arr) { int sizeofheap = arr.length; for (int i=sizeofheap-1; i>=0; i--) { moveroottoend(arr, i); heapify(arr, i, 0); } } private void moveroottoend( int[] arr, int endindex) { swap(arr, ROOT_INDEX, endindex); } } void heapify(int arr[], int heapsize, int rootindex){ int largest = getlargestsubroot( arr, heapsize, rootindex); if (largest!= rootindex) { swap(arr, rootindex, largest); heapify(arr, heapsize, largest); } } private int getlargestsubroot( int[] arr, int heapsize, int rootindex) { int largest = rootindex; int leftsubroot = 2*rootIndex + 1; int rightsubroot = 2*rootIndex + 2; } if (leftsubroot < heapsize && arr[leftsubroot] > arr[largest]) largest = leftsubroot; if (rightsubroot < heapsize && arr[rightsubroot] > arr[largest]) largest = rightsubroot; return largest; private void swap (int[] arr, int left, int right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; }
...
3. CHECKED EXCEPTION
EXCEPTION TREE : Checked Exception.. (throw, try-catch.) : Unchecked Exception Exception (, NullPointerException)
CHECKED EXCEPTION.. Exception,. (OCP ) try-catch. ( ).
( ) throws Exception ( checked exception ) Exception Exception catch throws Exception
public interface ListingSearchService { ListingResult search(listingrequest listingrequest) throws Exception; //... } public class ListingDataService { public ListingSearchService searchservice; public ListingData getdata(listingrequest parameter) { ListingData listingdata = new ListingData(); //... ListingResult result = null; try { result = searchservice.search(parameter); } catch (Exception e) { e.printstacktrace(); } //... ListingResult brandresult = searchservice.searchinbrand(parameter); //... return listingdata; } }
throws Exception Exception service Exception catch! service checked exception.
API, checked exception...
4.
API
SEARCH API 2000
SearchRequest( API ). API SearchRequest. (.). ( ).
SEARCH REQUEST BUILDER builder. SearchRequest.
BUILDER.. API API.
. API.?
( )..
package com.wym.search.listing; import com.wym.search.api.*; public class ListingSearchServiceImpl implements ListingSearchService { public static SearchService searchservice = new SearchServiceImpl(); @Override public ListingResult search(listingrequest listingrequest) { SearchRequest request = SearchRequestBuilder.build(listingRequest); SearchResult<Product> result = searchservice.searchproduct(request); return ListingResult.create(result); } }
API package com.wym.search; import com.wym.search.listing.listingrequest; import com.wym.search.listing.listingresult; import com.wym.search.listing.listingsearchservice; public class ListingDataService { public ListingSearchService searchservice; public ListingData getdata(listingrequest parameter) { ListingData listingdata = new ListingData(); //... ListingResult result = searchservice.search(parameter) //... ListingResult brandresult = searchservice.searchinbrand(parameter); //... return listingdata; } }
API ListingSearchService. ( ). Open Closed Principle
...
5.
( ) ( ). main 2 : 1 : 3 : 1.1 : << >> co :
11 APIVersionChecker Controler JSONAssemblerFactory <<interface>> JSONAssembler JSONAssemblerV1 JSONAssemblerV2 1. APIVersionChecker API 2. JSONAssemblerFactory API JSONAssembler.
<<interface>> JSONAssembler JSONAssemblerV1 JSONAssemblerV2 ProductToJSON JSON.. (, ).
public class ProductToJSON { public JSONObject convert(listingproduct product) { JSONObject result = new JSONObject(); result.put("productname", product.getname()); result.put("discountedprice", product.getdiscountedprice()); //... // 3.2.1 result.put("discounticons", makediscounticonsjson()); // 4.0.1 result.put("deliverytext", deliveryinfo.tostring()); //... } }
(ProductToJSON).. ProductToJSON.
: <<interface>> JSONAssembler JSONAssemblerV1 JSONAssemblerV2 <<interface>> ProductToJSON ProductToJSONV1 ProductToJSONV2
: package com.wym.search.json; import com.wym.search.listingdata; import com.wym.search.json.product.productjsonconverter; public class JSONAssemblerV1 implements JSONAssembler { private ListingData data; private ProductToJSON productjsonconverter; public JSONAssemblerV1(ListingData data, ProductToJSON productjsonconverter) { this.data = data; this.productjsonconverter = productjsonconverter; } @Override public JSONData assemble() { JSONData result = new JSONData(); //... return result; } }
: (Factory) ( ) package com.wym.search.json; import com.wym.search.listingdata; import com.wym.search.json.product.productjsonconverterv1; import com.wym.search.json.product.productjsonconverterv2; public class JSONAssemblerFactory { public static JSONAssembler create(listingdata data, int apiversion) { if (apiversion <= 1) { return new JSONAssemblerV1(data, ProductJSONConverterV1.getInstance()); } else if (apiversion <= 2) { return new JSONAssemblerV2(data, ProductJSONConverterV1.getInstance()); } else { return new JSONAssemblerV3(data, ProductJSONConverterV2.getInstance()); } } }
: (Factory). package com.wym.search; import com.wym.search.json.jsonassembler; import com.wym.search.json.jsonassemblerfactory; import com.wym.search.json.jsondata; import com.wym.search.listing.apiversionchecker; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class ListingController { public void listing(httpservletrequest request, HttpServletResponse response) { int apiversion = APIVersionChecker.getVersion(request, response); //... JSONAssembler jsonassembler = JSONAssemblerFactory.create(data, apiversion); JSONData result = jsonassembler.assemble(); //... } }
!. DI.
,. (, ).,.
6.
..!
( )
( )
Q & A