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.

\

Piyasa Fırsatı
PVS Logosu
PVS Fiyatı(PVS)
$0,001818
$0,001818$0,001818
-11,57%
USD
PVS (PVS) Canlı Fiyat Grafiği
Sorumluluk Reddi: Bu sitede yeniden yayınlanan makaleler, halka açık platformlardan alınmıştır ve yalnızca bilgilendirme amaçlıdır. MEXC'nin görüşlerini yansıtmayabilir. Tüm hakları telif sahiplerine aittir. Herhangi bir içeriğin üçüncü taraf haklarını ihlal ettiğini düşünüyorsanız, kaldırılması için lütfen service@support.mexc.com ile iletişime geçin. MEXC, içeriğin doğruluğu, eksiksizliği veya güncelliği konusunda hiçbir garanti vermez ve sağlanan bilgilere dayalı olarak alınan herhangi bir eylemden sorumlu değildir. İçerik, finansal, yasal veya diğer profesyonel tavsiye niteliğinde değildir ve MEXC tarafından bir tavsiye veya onay olarak değerlendirilmemelidir.

Ayrıca Şunları da Beğenebilirsiniz

Why Bitcoin’s Bear Case Is Suddenly Back on the Table

Why Bitcoin’s Bear Case Is Suddenly Back on the Table

Fear, Liquidity, and Market Structure Collide at a Critical Moment Bitcoin has spent most of January 2026 trading under pressure, slipping below key psycho
Paylaş
Medium2026/01/20 20:55
USD/JPY drops to near 157.80 as US-EU disputes batter US Dollar

USD/JPY drops to near 157.80 as US-EU disputes batter US Dollar

The post USD/JPY drops to near 157.80 as US-EU disputes batter US Dollar appeared on BitcoinEthereumNews.com. The USD/JPY pair is down 0.2% to near 157.80 during
Paylaş
BitcoinEthereumNews2026/01/20 21:27
Tapzi is Investors’ 1000x Pick in Volatile Market

Tapzi is Investors’ 1000x Pick in Volatile Market

The post Tapzi is Investors’ 1000x Pick in Volatile Market appeared on BitcoinEthereumNews.com. Crypto News 18 September 2025 | 00:05 Bitcoin swings after CPI data release as Tapzi’s presale gains momentum, emerging as a top crypto project in 2025. The crypto market moved sharply last week after the release of US Consumer Price Index (CPI) data. Bitcoin, the largest digital asset, reacted within minutes of the announcement, recording rapid swings before settling back near earlier levels.  At the same time, presale projects continued to attract investors, with Tapzi emerging as one of the most-watched tokens this month. It is being picked by investors as the next crypto to explode due to its high-growth potential in Tier 1 and Tier 2 countries, with Web3 gaming’s increasing adoption. Tapzi Presale Draws Attention While Bitcoin reacted to economic data, Tapzi’s presale has become a focal point among both retail and larger investors. Tapzi is a Web3 gaming platform designed to merge competitive gameplay with blockchain-based settlements. Players stake TAPZI tokens in head-to-head matches of chess, checkers, rock-paper-scissors, and tic-tac-toe. Winners receive tokens directly from prize pools funded by players, not by inflationary rewards. Don’t Watch the Wave – Ride It With $TAPZI! The presale opened with tokens priced at $0.0035. More than 27 million tokens have already been sold, with prices set to increase in each new stage. Analysts following the sale point to potential gains of around 300% once TAPZI lists on exchanges later this year. Liquidity locks and vesting schedules are in place to reduce the risks of sharp sell-offs after launch. This has placed Tapzi on the radar of investors searching for the best crypto to buy now. Bitcoin Price Reacts to CPI Last week, Bitcoin climbed toward $114,000 before jumping to $114,500, its highest level in weeks. The gains were short-lived as the price quickly dropped by $1,000. At press time, Bitcoin…
Paylaş
BitcoinEthereumNews2025/09/18 06:26