【问题标题】:Which piece of code is better? [closed]哪一段代码更好? [关闭]
【发布时间】:2016-10-27 21:57:04
【问题描述】:

我正在编写一些代码,但我不确定哪个更好。一方面,它更容易阅读正在发生的事情,但我有更多的代码行。 另一方面,你的代码行更少,但我认为更难理解。

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();

【问题讨论】:

  • 追求可读性。顺便说一句,这个词是“一块”,而不是“和平”。 “和平”意味着不打架。
  • 谢谢@MikeDunlavey
  • 另外我认为这最好作为一个java问题。我认为这里没有与 Android 相关的内容或需要了解 Android 的内容。
  • 虽然是安卓代码我觉得你说的对,我还是删掉吧。谢谢

标签: java performance optimization


【解决方案1】:

分差。我会做这样的事情:

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();
}

我不认为从你的 textview 中提取 String 对象会使事情更具可读性,因为你的 textview 的名称非常清楚。

【讨论】:

  • SimpleDateFormat 将是对答案的一个很好的补充
  • 我不知道 Timeconversion() 是什么,或者它是否有不同的功能,或者我很乐意更新我的答案。根据 timeInMillis 部分,我猜测返回值很长。如果 OP 想阐述特定值,很乐意优化答案。这里的关键是,由于该特定块有点大,将其放入一个明确命名的方法中会使代码更具可读性。
  • 显然date.getYear()+"/"+date.getMonth() 正在获取一个字符串。这就是我的观点
  • 如果您想放入 SDF,请编辑答案。我会批准修改。
  • Timeconversion 是一个包含将日期字符串转换为毫秒的方法的类
猜你喜欢
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2017-08-06
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多