【问题标题】:Why is the following code better than what I wrote?为什么下面的代码比我写的好?
【发布时间】:2022-11-28 20:49:03
【问题描述】:

我得到了相同的结果,但想知道哪种方法更好以及为什么。

课程解决方案

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeArticleTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    ComposeArticleApp()
                }
            }
        }
    }
}

@Composable
fun ComposeArticleApp() {
    ArticleCard(
        title = stringResource(R.string.title_jetpack_compose_tutorial),
        shortDescription = stringResource(R.string.compose_short_desc),
        longDescription = stringResource(R.string.compose_long_desc),
        imagePainter = painterResource(R.drawable.bg_compose_background)
    )
}

@Composable
private fun ArticleCard(
    title: String,
    shortDescription: String,
    longDescription: String,
    imagePainter: Painter,
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier) {
        Image(painter = imagePainter, contentDescription = null)
        Text(
            text = title,
            fontSize = 24.sp,
            modifier = Modifier.padding(16.dp)
        )
        Text(
            text = shortDescription,
            textAlign = TextAlign.Justify,
            modifier = Modifier.padding(start = 16.dp, end = 16.dp)
        )
        Text(
            text = longDescription,
            textAlign = TextAlign.Justify,
            modifier = Modifier.padding(16.dp)
        )
    }
}

我写的

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeArticleTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    ArticleWithImage(
                        getString(R.string.article_title),
                        getString(R.string.article_para1),
                        getString(R.string.article_para2)
                    )
                }
            }
        }
    }
}

@Composable
fun ArticleWithImage(title: String, para1 : String, para2 : String) {
    Box {
        Column {
            Image(
                painter = painterResource(R.drawable.bg_compose_background),
                contentDescription = null
            )
            ArticleWithText(title = title, para1 = para1, para2 = para2)
        }
    }
}

@Composable
fun ArticleWithText(title: String, para1 : String, para2 : String) {
    Column{
        Text(text = title, fontSize = 24.sp, textAlign = TextAlign.Justify, modifier = Modifier.padding(10.dp))
        Text(text = para1, fontSize = 18.sp, textAlign = TextAlign.Justify, modifier = Modifier.padding(10.dp))
        Text(text = para2, fontSize = 18.sp, textAlign = TextAlign.Justify, modifier = Modifier.padding(10.dp))
    }
}

添加额外的文本作为 stackoverflow 不允许我发布主要是代码的查询

我们为什么用它?

一个早已确立的事实是,读者在查看页面布局时会被页面的可读内容分散注意力。使用 Lorem Ipsum 的要点在于它或多或少具有正态分布的字母,而不是使用“Content here,content here”,使其看起来像可读的英语。许多桌面出版包和网页编辑器现在使用 Lorem Ipsum 作为他们的默认模型文本,搜索“lorem ipsum”会发现许多网站仍处于起步阶段。这些年来出现了各种版本,有时是偶然的,有时是故意的(注入幽默等)。

它从何而来?

与流行的看法相反,Lorem Ipsum 不仅仅是随机文本。它起源于公元前 45 年的一部古典拉丁文学,至今已有 2000 多年的历史。理查德·麦克林托克 (Richard McClintock) 是弗吉尼亚州汉普登-悉尼学院 (Hampden-Sydney College) 的拉丁语教授,他从 Lorem Ipsum 段落中查找了一个比较晦涩的拉丁词 consectetur,并查阅了该词在古典文学中的引用,发现了无疑的来源。 Lorem Ipsum 出自公元前 45 年西塞罗所著的“de Finibus Bonorum et Malorum”(善恶的极端)的第 1.10.32 和 1.10.33 节。这本书是关于伦理学理论的专着,在文艺复兴时期非常流行。 Lorem Ipsum 的第一行“Lorem ipsum dolor sit amet..”来自 1.10.32 节中的一行。

自 1500 年代以来使用的标准 Lorem Ipsum 块在下面为感兴趣的人复制。西塞罗的“de Finibus Bonorum et Malorum”中的第 1.10.32 和 1.10.33 节也以其原始形式复制,并附有 H. Rackham 1914 年译本的英文版本。 我在哪里可以买到一些?

Lorem Ipsum 的段落有很多变体,但大多数都以某种形式进行了更改,通过注入幽默或随机单词,看起来甚至一点都不可信。如果你打算使用 Lorem Ipsum 的段落,你需要确保文本中间没有隐藏任何令人尴尬的内容。 Internet 上的所有 Lorem Ipsum 生成器都倾向于根据需要重复预定义的块,这使它成为 Internet 上第一个真正的生成器。它使用包含 200 多个拉丁词的字典,结合一些模型句子结构,生成看起来合理的 Lorem Ipsum。因此,生成的 Lorem Ipsum 始终没有重复、注入的幽默或非特征词等。

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    当然,解决方案使用较少的可组合函数/组件来显示相同​​的 UI,这是首选,因为您希望代码尽可能短。例如,您在哪里使用

    Box {
        Column {
            Image(...
    

    课程作者使用

    Column(modifier = modifier) {
        Image(painter = imagePainter, contentDescription = null)
    

    如您所见,不需要 Box 可组合项。

    在codestyle方面,course的solution更易读,因为它没有太长的行并且更方便阅读函数的参数。

    例如,你写了

    Column{
        Text(text = title, fontSize = 24.sp, textAlign = TextAlign.Justify, modifier = Modifier.padding(10.dp))
        Text(text = para1, fontSize = 18.sp, textAlign = TextAlign.Justify, modifier = Modifier.padding(10.dp))
        Text(text = para2, fontSize = 18.sp, textAlign = TextAlign.Justify, modifier = Modifier.padding(10.dp))
    }
    

    这有点难以阅读,因为行太长(至少对我而言)。当然,解决方案也不包含过多的嵌套,如下所示。

    Box {
        Column {
            Image(
                painter = painterResource(R.drawable.bg_compose_background),
                contentDescription = null
            )
            ArticleWithText(title = title, para1 = para1, para2 = para2)
        }
    }
    

    您也不想在 onCreate 中描述可组合项的参数。你应该尽量避免它。在您的解决方案中,您可以轻松避免在 onCreate 方法中放置这么多行可组合代码。

    当然,这都是意见相关的,但是在生产中更可能看到课程作者提供的代码。但是请不要认为我的回答很苛刻,你做得很好,代码风格是经验带来的。

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2018-08-16
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多