PVS-Studio team creates new diagnostic rules, and gradually refines the existing ones. We've recently enhanced one of the oldest diagnostic rules in the C# analyzerPVS-Studio team creates new diagnostic rules, and gradually refines the existing ones. We've recently enhanced one of the oldest diagnostic rules in the C# analyzer

How to Prevent Your Code From Turning Into Sausage That Goes Beyond the Screen

Today, we’ll talk about a bug that shows in practice and how "code sausage" can cause a series of problems related to the last line effect and careless copy-paste, as well as lead to new errors.

\ The PVS-Studio team not only creates new diagnostic rules, but also gradually refines the existing ones. For example, we've recently enhanced one of the oldest diagnostic rules in the C# analyzer, V3001, to make it detect redundant parentheses more accurately. As a result, the analyzer started detecting new bugs, one of which we show you.

\ This case was detected in the Space Engineers project; this is one of the projects in our internal regression testing database. We use a specific old project version to compare how the analyzer behaves on the same code across updates. But if we look at the latest source code, we'll find that the bug is still there. Let's take a look at the Contains function in BoundingBox.cs.

\ See the problem? Probably not.

\ Why's that? Because long and indecipherable code lines are developers' foes that should be avoided. It's very easy to make a mistake there, as you can see. Let's rewrite the code a little bit to make it clearer.

public ContainmentType Contains(BoundingSphere sphere) { Vector3 result1; Vector3.Clamp(ref sphere.Center, ref this.Min, ref this.Max, out result1); float result2; Vector3.DistanceSquared(ref sphere.Center, ref result1, out result2); float num = sphere.Radius; if ((double)result2 > (double)num * (double)num) return ContainmentType.Disjoint; return (double)this.Min.X + (double)num > (double)sphere.Center.X || (double)sphere.Center.X > (double)this.Max.X - (double)num || ((double)this.Max.X - (double)this.Min.X <= (double)num || (double)this.Min.Y + (double)num > (double)sphere.Center.Y) || ((double)sphere.Center.Y > (double)this.Max.Y - (double)num || (double)this.Max.Y - (double)this.Min.Y <= (double)num || ((double)this.Min.Z + (double)num > (double)sphere.Center.Z || (double)sphere.Center.Z > (double)this.Max.Z - (double)num)) || (double)this.Max.X - (double)this.Min.X <= (double)num ? ContainmentType.Intersects : ContainmentType.Contains; }

\ Better now, yeah? However, we have to make an effort to spot the error, though. Take a look at the last line of the logical condition:

(double)this.Max.X - (double)this.Min.X <= (double)num

\ As we can see, it duplicates the third line. The condition is enclosed in extra parentheses, but they're superfluous, as all checks are joined with the OR operator anyway.

\ In practice, there should be a check of the Z coordinate:

(double)this.Max.Z - (double)this.Min.Z <= (double)num

\ The analyzer detects it and issues a warning: V3001 There are identical sub-expressions '(double)this.Max.X - (double)this.Min.X <= (double)num' to the left and to the right of the '||' operator.

\ This is a good example of how a static analyzer complements code review because it's strenuous to manually discern a little typo in such a massive line. We like to call such code "code sausage"—and we've already written a note about how it lures bugs to your code.

\ The "last line effect" is also shown in all its glory. Typos most often appear at the end of similar code fragments. Technically, we can't talk about lines, since there is a single line. However, the idea still applies: the error occurred in the very last segment of a long, repetitive block.

\ The bug came from a copy-paste typo. Most likely, developers have copied one sub-expression, pasted it as a new one, and just forgotten to modify it. However, that's not all: this entire line with the error has been copied again and shows up just a few lines below, in the nearby Contains function:

public void Contains(ref BoundingSphere sphere, out ContainmentType result) { .... if ((double)result2 > (double)num * (double)num) result = ContainmentType.Disjoint; else result = (double)this.Min.X + (double)num > (double)sphere.Center.X || (double)sphere.Center.X > (double)this.Max.X - (double)num || ((double)this.Max.X - (double)this.Min.X <= (double)num || (double)this.Min.Y + (double)num > (double)sphere.Center.Y) || ((double)sphere.Center.Y > (double)this.Max.Y - (double)num || (double)this.Max.Y - (double)this.Min.Y <= (double)num || ((double)this.Min.Z + (double)num > (double)sphere.Center.Z || (double)sphere.Center.Z > (double)this.Max.Z - (double)num)) || (double)this.Max.X - (double)this.Min.X <= (double)num ? ContainmentType.Intersects : ContainmentType.Contains; }

It's the same issue with the same warning from the analyzer.

Conclusion

There's no need to go into a long explanation of why this code is problematic, as well as how it should be changed to avoid specific errors. Our readers probably already know that it all comes down to following these tips:

  1. Use table-style code formatting.
  2. Place the similar code in functions.
  3. Avoid redundant operations. For example, instead of type casting of (double)num everywhere, we could simply declare the num variable as double.
  4. Run PVS-Studio static analyzer regularly for additional control.

\

Market Opportunity
PVS Logo
PVS Price(PVS)
$0.001999
$0.001999$0.001999
+2.98%
USD
PVS (PVS) Live Price Chart
Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact [email protected] for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

Sberbank explores crypto-backed loans as Russia softens stance on digital assets

Sberbank explores crypto-backed loans as Russia softens stance on digital assets

Russian financial services giant Sberbank may soon start offering loans secured by cryptocurrency, one of its top executives unveiled.         The news comes right
Share
Cryptopolitan2025/12/25 23:38
Why The Green Bay Packers Must Take The Cleveland Browns Seriously — As Hard As That Might Be

Why The Green Bay Packers Must Take The Cleveland Browns Seriously — As Hard As That Might Be

The post Why The Green Bay Packers Must Take The Cleveland Browns Seriously — As Hard As That Might Be appeared on BitcoinEthereumNews.com. Jordan Love and the Green Bay Packers are off to a 2-0 start. Getty Images The Green Bay Packers are, once again, one of the NFL’s better teams. The Cleveland Browns are, once again, one of the league’s doormats. It’s why unbeaten Green Bay (2-0) is a 8-point favorite at winless Cleveland (0-2) Sunday according to betmgm.com. The money line is also Green Bay -500. Most expect this to be a Packers’ rout, and it very well could be. But Green Bay knows taking anyone in this league for granted can prove costly. “I think if you look at their roster, the paper, who they have on that team, what they can do, they got a lot of talent and things can turn around quickly for them,” Packers safety Xavier McKinney said. “We just got to kind of keep that in mind and know we not just walking into something and they just going to lay down. That’s not what they going to do.” The Browns certainly haven’t laid down on defense. Far from. Cleveland is allowing an NFL-best 191.5 yards per game. The Browns gave up 141 yards to Cincinnati in Week 1, including just seven in the second half, but still lost, 17-16. Cleveland has given up an NFL-best 45.5 rushing yards per game and just 2.1 rushing yards per attempt. “The biggest thing is our defensive line is much, much improved over last year and I think we’ve got back to our personality,” defensive coordinator Jim Schwartz said recently. “When we play our best, our D-line leads us there as our engine.” The Browns rank third in the league in passing defense, allowing just 146.0 yards per game. Cleveland has also gone 30 straight games without allowing a 300-yard passer, the longest active streak in the NFL.…
Share
BitcoinEthereumNews2025/09/18 00:41
Understanding the Construction Industry Scheme

Understanding the Construction Industry Scheme

The Construction Industry Scheme, commonly known as CIS, is a tax system used in the UK construction sector. It sets out how payments made by contractors to subcontractors
Share
Techbullion2025/12/25 23:53