Simplify Expression of if else in golang

huangapple go评论89阅读模式
英文:

Simplify Expression of if else in golang

问题

我可以帮你简化代码。根据你提供的注释,我理解你想要根据不同的查询条件执行不同的操作。以下是简化后的代码:

if offset != defaultOffset && limit != defaultLimit && page != defaultPage && head != 0 && last != 0 {
	return make([]bson.M, 0), errors.New("无法显示所有查询")
} else if offset != defaultOffset && limit != defaultLimit && page != defaultPage {
	return make([]bson.M, 0), errors.New("无法显示所有查询")
} else if offset != defaultOffset && limit == defaultLimit && page != defaultPage {
	return make([]bson.M, 0), errors.New("无法合并页码和偏移量")
} else if head != 0 && last != 0 {
	return make([]bson.M, 0), errors.New("无法合并头部和尾部")
} else if limit <= 0 || page <= 0 {
	return make([]bson.M, 0), errors.New("BSON字段值必须大于等于0,实际值为-20")
} else if offset == defaultOffset && limit != defaultLimit && page == defaultPage {
	opt.SetLimit(limit)
} else if offset != defaultOffset && limit == defaultLimit && page == defaultPage {
	opt.SetSkip(offset).SetLimit(limit)
} else if offset == defaultOffset && limit == defaultLimit && page != defaultPage {
	opt.SetSkip(paginate).SetLimit(defaultLimit)
} else if offset == defaultOffset && limit != defaultLimit && page != defaultPage {
	opt.SetSkip(paginate).SetLimit(limit)
} else if offset != defaultOffset && limit != defaultLimit && page == defaultPage {
	opt.SetSkip(offset).SetLimit(limit)
} else if head == 0 && last != 0 {
	opt.SetLimit(last).SetSort(bson.M{"$natural": -1})
} else if head != 0 && last == 0 {
	opt.SetSkip(0).SetLimit(head)
} else {
	options.Find()
}

这样,你的代码就被简化了。我希望这对你有帮助!如果你还有其他问题,请随时问我。

英文:

i have logic module for head, last, page, limit and offset filter.

such as :

> ../example/param if no query, return all value
>
> ../example/param?limit=10 return the first 10 value
>
> ..example/param?offset=3 return value from number 4 to 23 (default limit is 20)
>
> ..example/param?limit=15&offset=2 return value 3 to 17
>
> ../example/param?page=2&limit=5 return value 6 to 10
>
> ../example/param?head=15 return first 15 value
>
> ../example/param?last=15 return last 15 value
>
> ../example/param?head=8&last=4 return error
>
> ../example/param?page=3&offset=4 return error
>
> ../example/param?page=2&limit=5&offset=6 return error

i have default parameter like a :

var defaultPageParameter = PageParameter{
Limit:  20,
Offset: 0,
Page: 1,
Head: 0,
Last: 0,
}
limit := pagePara.Limit
offset := pagePara.Offset
page := pagePara.Page
head := pagePara.Head
last := pagePara.Last
defaultPage := defaultPageParameter.Page
defaultLimit := defaultPageParameter.Limit
defaultOffset := defaultPageParameter.Offset
paginate := (page*limit)-limit

i have write some expression if else, but this code really to much, I want to simplify code.

if offset != defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page != defaultPage &amp;&amp; head != 0 &amp;&amp; last != 0{
return make([]bson.M, 0), errors.New(&quot;can&#39;t show all queries&quot;)
//if there are all query
} else if offset != defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page != defaultPage {
return make([]bson.M, 0), errors.New(&quot;can&#39;t show all queries&quot;)
//if there are all query
} else if offset != defaultOffset &amp;&amp; limit == defaultLimit &amp;&amp; page != defaultPage{
return make([]bson.M, 0), errors.New(&quot;can&#39;t merge page and offset&quot;)
//if combine page &amp; offset
} else if head != 0 &amp;&amp; last != 0 {
return make([]bson.M, 0), errors.New(&quot;can&#39;t merge head and last&quot;)
//if combine head &amp; last
} else if limit &lt;=0 || page &lt;= 0{
return make([]bson.M, 0), errors.New(&quot;BSON field value must be &gt;= 0, actual value -20&quot;)
//if limit &amp; page value smaller than 0
}else if offset == defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page == defaultPage {
opt.SetLimit(limit)
//if just limit
} else if offset != defaultOffset &amp;&amp; limit == defaultLimit &amp;&amp; page == defaultPage{
opt.SetSkip(offset).SetLimit(limit)
//if just offset
} else if offset == defaultOffset &amp;&amp; limit == defaultLimit &amp;&amp; page != defaultPage{
opt.SetSkip(paginate).SetLimit(defaultLimit)
//if just page
} else if offset == defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page != defaultPage{
opt.SetSkip(paginate).SetLimit(limit)
//if limit &amp; page
} else if offset != defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page == defaultPage{
opt.SetSkip(offset).SetLimit(limit)
//if offset &amp; limit
} else if head == 0 &amp;&amp; last != 0{
opt.SetLimit(last).SetSort(bson.M{&quot;$natural&quot;: -1})
//if just last
} else if head != 0 &amp;&amp; last == 0{
opt.SetSkip(0).SetLimit(head)
//if just head
} else {
options.Find()
//else return default
}

this code refer to notes above, my code really to much expression,
can anyone simplify?

答案1

得分: 0

很难在不了解周围代码的情况下简化整个问题并给出完整的代码。但是我有几个建议,我认为你可以应用这些建议来更轻松地简化逻辑。

建议一:
我在这里看到的一个问题是,使用基本类型(int)作为输入变量(offset、limit、page、head、last)会使逻辑更加混乱。

问题在于你将输入变量与默认值(在Go中称为“零值”)进行比较,例如0或defaultLimit,而实际上你想要检查的是用户是否提供了这些输入。

我的建议是将这些输入变量改为指针(*int),这样你可以将它们与nil进行比较,以更轻松、清晰地检查用户是否发送了这些输入。我认为这将使逻辑更易读。

例如,你可以将offset和limit简化为以下代码:

if offset != nil {
  opt.SetSkip(*offset)
}

if limit != nil {
  opt.SetLimit(*limit)
}

建议二:
我还建议以不同的方式处理错误。

首先检查错误情况。如果输入无效,则返回错误。

然后在没有错误的情况下处理成功情况。这是编写Go代码(以及其他语言)的一种常见方式。

类似这样:

// 在你的帖子中的函数中:

// 检查错误情况
err := checkError(offset, limit, page, head, last)
if err != nil {
  return make([]bson.M, 0), err
}

// 处理成功(非错误)情况
if offset == defaultOffset && limit != defaultLimit && page == defaultPage {
    opt.SetLimit(limit)
    // 如果只有limit
} else if // ... 其他成功情况 ...


// ... 在同一文件(或包)的其他位置 ...

func checkError(offset, limit, page, head, last int) error {
  if offset != defaultOffset && limit != defaultLimit && page != defaultPage && head != 0 && last != 0{
      return errors.New("无法显示所有查询")
      // 如果有所有查询
  } else if offset != defaultOffset && limit != defaultLimit && page != defaultPage {
      return errors.New("无法显示所有查询")
      // 如果有所有查询
  } else if offset != defaultOffset && limit == defaultLimit && page != defaultPage{
      return errors.New("无法合并页码和偏移量")
      // 如果合并了页码和偏移量
  } else if head != 0 && last != 0 {
      return errors.New("无法合并头部和尾部")
      // 如果合并了头部和尾部
  } else if limit <= 0 || page <= 0{
      return errors.New("BSON字段值必须大于等于0,实际值为-20")
      // 如果limit和page的值小于0
  }

  return nil
}

以上是你要翻译的内容。

英文:

It's hard to simplify the whole thing and give you the finished code without knowing a bit more of the surrounding code.

But I have a couple suggestions for you which I think you can apply to make it a lot easier for you to simplify the logic yourself.

Suggestion #1

One issue I see here which I believe is making the logic more confusing is the usage of basic types (int) for the input variables (offset, limit, page, head, last).

The problem is that you are comparing input variables with default values (called "zero values" in Go) such as 0 or defaultLimit when what you are actually trying to check is whether the user provided these inputs or not.

My suggestion is to make these input variables pointers (*int) so that you can instead compare them with nil to more easily and clearly check if the user sent these inputs or not. I think this will make the logic much more readable.

For example, offset & limit you might be able to simplify to something like this:

if offset != nil {
opt.SetSkip(*offset)
}
if limit != nil {
opt.SetLimit(*limit)
}

Suggestion #2

I would also suggest handling errors differently.

Check error cases first. Return an error if the input is invalid.

Then handle success cases after that if there was no error. This is a pretty common way of writing code in Golang (and other languages too).

Something like this:

// in the function from your post:
// check for error cases
err := checkError(offset, limit, page, head, last)
if err != nil {
return make([]bson.M, 0), err
}
// handle success (non error) cases
if offset == defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page == defaultPage {
opt.SetLimit(limit)
//if just limit
} else if // ... the rest of the success cases ...
// ... elsewhere in the same file (or package) ...
func checkError(offset, limit, page, head, last int) error {
if offset != defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page != defaultPage &amp;&amp; head != 0 &amp;&amp; last != 0{
return errors.New(&quot;can&#39;t show all queries&quot;)
//if there are all query
} else if offset != defaultOffset &amp;&amp; limit != defaultLimit &amp;&amp; page != defaultPage {
return errors.New(&quot;can&#39;t show all queries&quot;)
//if there are all query
} else if offset != defaultOffset &amp;&amp; limit == defaultLimit &amp;&amp; page != defaultPage{
return errors.New(&quot;can&#39;t merge page and offset&quot;)
//if combine page &amp; offset
} else if head != 0 &amp;&amp; last != 0 {
return errors.New(&quot;can&#39;t merge head and last&quot;)
//if combine head &amp; last
} else if limit &lt;=0 || page &lt;= 0{
return errors.New(&quot;BSON field value must be &gt;= 0, actual value -20&quot;)
//if limit &amp; page value smaller than 0
}
return nil
}

huangapple
  • 本文由 发表于 2022年1月5日 07:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/70586283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定