2016-10-27 3 views
1

일부 코드를 작성했지만 어느 것이 더 좋을지 잘 모르겠습니다. 한 가지 방법으로 무슨 일이 일어나는지 쉽게 읽을 수 있지만 더 많은 코드 라인이 있습니다. 다른면에서는 코드 줄이 적지 만 이해하기가 더 어려울 것이라고 생각합니다.어떤 코드가 더 좋습니까?

String imp = importance.getSelectedItem().toString(); 
String title_str = title.getText().toString(); 
String body_str = body.getText().toString(); 
String location_str = location.getText().toString(); 
int day = date.getDayOfMonth(); 
int month = date.getMonth()+1; 
int year = date.getYear(); 
int hh = time.getCurrentHour(); 
int mm = time.getCurrentMinute(); 
String date_str = year+"/"+month+"/"+day+" " + hh+":"+mm +":00"; // yyyy/MM/dd HH:mm:ss 
long dateMilliseconds = new Timeconversion().timeConversion(date_str); 

Conference conference = ConferenceBuilder.conference() 
     .id(idConf) 
     .importance(Double.parseDouble(imp)) 
     .title(title_str) 
     .body(body_str) 
     .location(location_str) 
     .timeInMilliseconds(dateMilliseconds) 
     .build(); 

또는

Conference conference2 = ConferenceBuilder.conference() 
           .id(idConf) 
           .importance(Double.parseDouble(importance.getSelectedItem().toString())) 
           .title(title.getText().toString()) 
           .body(body.getText().toString()) 
           .location(location.getText().toString()) 
           // yyyy/MM/dd HH:mm:ss 
           .timeInMilliseconds(new Timeconversion().timeConversion(date.getYear()+"/"+date.getMonth()+1+"/"+date.getDayOfMonth()+" " + time.getCurrentHour()+":"+time.getCurrentMinute() +":00")) 
           .build(); 
+3

가독성을 위해 이동하십시오. 그런데 그 단어는 "평화"가 아니라 "조각"입니다. "평화"란 싸우지 않는 것을 의미합니다. –

+0

고마워요 @MikeDunlavey – mavi

+0

또한이 자바 질문으로 더 나은 생각합니다. 안드로이드와 관련이 없거나 안드로이드에 대한 지식이 필요하다고 생각합니다. –

답변

1

분할 차이. 당신의 textviews 꽤 명확하게 명명되기 때문에, 당신의 textviews에서 String 객체를 추출하는 일을 더 쉽게 읽을 생각하지 않습니다

Conference conference2 = ConferenceBuilder.conference() 
      .id(idConf) 
      .importance(Double.parseDouble(importance.getSelectedItem().toString())) 
      .title(title.getText().toString()) 
      .body(body.getText().toString()) 
      .location(location.getText().toString()) 
      // yyyy/MM/dd HH:mm:ss 
      .timeInMilliseconds(getTimeInMillis(datePicker, timePicker)) 
      .build(); 
} 

private long getTimeInMillis(DatePicker datePicker, TimePicker timePicker) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
    timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0); 
    return calendar.getTimeInMillis(); 
} 

: 나는 같은 것을 할 것입니다.

+0

'SimpleDateFormat'이 답변에 대한 멋진 추가가 될 것입니다. –

+0

Timeconversion()이 무엇인지, 아니면 다른 것을하는지, 아니면 내 대답을 업데이트 해 드리겠습니다. 나는 반환 값이 timeInMillis 부분을 기반으로 길다는 것을 추측했다. OP가 특정 가치에 대해 설명하고 싶다면 답을 최적화하십시오. 여기에서 핵심은 특정 덩어리가 약간 무거 우므로 코드를 명확하게 명명 된 메서드에 넣으면 코드를보다 쉽게 ​​읽을 수 있다는 것입니다. –

+0

명백히'date.getYear() + "/"+ date.getMonth()'는 String을 얻고있다. 그게 내 포인트 야. –

관련 문제