android 暗码_暗码风格的学院:如果陈述破坏了

本文探讨了通过编写难以维护的代码来确保工作安全性的策略,重点介绍了如何故意破坏if语句,包括逆置条件、复杂化条件、转换布尔返回值、避免方法提取等技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 暗码

image

Do you want to raise your salary? Do you want always to be in demand? Do you want to have your job as long as you want? It is absolutely real! You just need to change the way you write your code. Basically, you need to increase your job security. You have to write code which will be almost impossible to maintain for everyone except you. And in these series of articles, I will tell you how to achieve it. Welcome under the cut.

你想加薪吗? 您想一直需求吗? 您是否想长期工作? 绝对是真实的! 您只需要更改编写代码的方式即可。 基本上,您需要提高工作安全性。 您必须编写几乎除您之外的所有人都无法维护的代码。 在这些系列文章中,我将告诉您如何实现它。 欢迎下切。

如何破坏if语句 (How to spoil if statement)

Let’s start from if statement. Developers usually underestimate the importance of if. But it is a really powerful statement. It is very easy to confuse other developers using special approaches. So, here are a few basic ways to spoil if statement.

让我们从if语句开始。 开发人员通常会低估if的重要性。 但这是一个非常有力的声明。 使用特殊方法使其他开发人员容易混淆。 因此,这里有一些破坏if语句的基本方法。

情况1 (Case #1)

Take a look at this piece of code. What’s wrong here?

看一下这段代码。 怎么了

if (condition) 
{
    // actions when true
}
else
{
    // actions when false
}

Of course, you noticed. Here we missed the simplest place to confuse the reader. Always inverse condition. At least once.

当然,您注意到了。 在这里,我们错过了使读者感到困惑的最简单的地方。 始终处于逆状态。 至少一次。

if (!condition) 
{
    WhenFalse();
}
else
{
    WhenTrue();
}

My favorite one is !string.IsNotEmpty(value).

我最喜欢的是!string.IsNotEmpty(value)

Lesson: Always keep inverse condition.
课:始终保持逆状态。

情况#2 (Case #2)

Ok, that was an easy one! How about this?

好的,那很简单! 这个怎么样?

if (nameIsValid) 
{
    // ...
}

Yes! You are right. We don’t need readable and clear conditions. It becomes much easier to read and maintain such if. The easiest way to spoil this one is to paste calculation directly into the condition.

是! 你是对的。 我们不需要可读且清晰的条件。 如果这样阅读和维护,将变得更加容易。 破坏这一结果的最简单方法是将计算结果直接粘贴到条件中。

if (!string.IsNullOrEmpty(name) && name.Length > 4 && name.Length < 20) 
{
    // ...
}

Now is much better.

现在好多了。

Lesson: Make the condition as complex as you can.
课程:使条件尽可能复杂。

情况#3 (Case #3)

Go to the next one. This one is quite simple as well. Thoughts?

转到下一个。 这个也很简单。 有什么想法吗?

return condition;

Absolutely! If you don’t have if statement then you must add it.

绝对! 如果没有if语句,则必须添加它。

if (condition)
{
    return true;
}
else
{
   return false;
}

Looks good.

看起来挺好的。

Lesson: Transform boolean return values to conditional statements.
课程:将布尔返回值转换为条件语句。

情况#4 (Case #4)

What about this one? Take your time to think!

这个如何? 花点时间思考!

if (condition)
{
    UsefulFunction();
}
else
{
    OtherUsefulFuntion();
}

Do we need to decompose complicated parts to separate methods? Of course not. Developer who reads this code should be busy trying to understand what that method does in order to make him forget what was the relevant condition. Moreover, while he will be parsing code from else part he should already forget what was in a then part.

我们是否需要将复杂的部分分解为单独的方法? 当然不是。 阅读此代码的开发人员应该忙于试图了解该方法的作用,以使他忘记相关条件。 而且,尽管他将从其他部分解析代码,但他应该已经忘记了随后的部分。

if (condition)
{
    argsCount++;
    queue.Enqueue(new[] { default(Element), default(Element), a });
    _scanner.DeleteRestorePoint();
    _writer.ChangeCursor();
    value = 42 * days;
}
else
{
    var element = default(Element);
    var children = queue.Dequeue();
    while (children[0] == Grammar.New)
    {
        children = queue.Dequeue();
    }
    element = new Element(ElementType.MemberExpression, children);
}

Isn’t it awesome? We will cover the ways how to generate such a useless code later. Anyway, don’t even try to understand what is going on there. I don’t know that either.

很棒吗 稍后我们将介绍如何生成这种无用代码的方法。 无论如何,甚至不要试图了解那里发生了什么。 我也不知道

Lesson: Do not extract methods and leave complicated code inside if statement.
课程:不要提取方法并将复杂的代码留在if语句中。

案例5 (Case #5)

One of my favorites. What is wrong here?

我最喜欢的之一。 怎么了

statusHandlers[status].Handle();

We don’t need to produce clean code! We don’t need any patterns or other smart stuff. Leave Strategies and Chains of responsibility to junior developers. Checking status case by case makes code bloating. Now it becomes much more difficult to reuse such code. Developers who will be supporting this can easily get confused and produce a bug. That is what we need. Job security!

我们不需要产生干净的代码! 我们不需要任何模式或其他聪明的东西。 将策略和责任链留给初级开发人员。 逐个检查状态会使代码膨胀。 现在,重用此类代码变得更加困难。 将支持此操作的开发人员很容易感到困惑并产生错误。 那就是我们所需要的。 就业保障!

if (status == Status.New)
{
    NewHandler();
}
else if (status == Status.Open)
{
    OpenHandler();
}
else if (status == Status.Closed)
{
    ClosedHandler();
}

We can make it worse. Replace enum with magic numbers or strings and as we learned before – paste methods code inside then. Enjoy.

我们会让情况变得更糟。 如前所述,用魔术数字或字符串替换枚举–然后将方法代码粘贴到其中。 请享用。

Lesson: Do not use patterns. Seriously.
课程:请勿使用模式。 说真的

情况#6 (Case #6)

Relax, here is not so complicated way to make your code dirty. Take a look at this piece of code.

放松一下,这不是使代码肮脏的复杂方法。 看一下这段代码。

var condition = first && second;

if (condition)
{
    // code
}

See and operation? It means that you can split condition and place each one into its own if statement.

看和操作? 这意味着您可以拆分条件并将每个条件放入其自己的if语句中。

if (first)
{
    if (second)
    {
        // code
    }
}
Lesson: Place conditional inside each other if you can.
经验教训:如果可以的话,将有条件的放在一起。

案例#7 (Case #7)

This one is difficult.

这很难。

return string.Format("{0:P2}", systemPower);

Do not use formulas or any calculations. Why do you need to spend such an awesome opportunity to spoil the code? You can provide value for every case. The code becomes huge, difficult to read and understand.

请勿使用公式或任何计算。 为什么您需要花这么大的机会破坏代码? 您可以为每种情况提供价值。 代码变得庞大,难以阅读和理解。

if (systemPower == 1)
{
    return "100%";
}
else if (systemPower == 0.99)
{
    return "99%";
}
else if (systemPower == 0.98)
{
    return "98%";
}
...
else if (systemPower == 0) {
    return "0%";
}

Simple change which will give you a couple of unforgettable days of fixing bugs with double values.

简单的更改将为您带来难忘的几天来修复具有双值的错误。

Lesson: Provide values for every case in a separate if statement.
课程:在单独的if语句中为每种情况提供值。

What I need to add, is that you must be careful about code reviews and quality gates on your CI. Later on, we will cover those topics, but for now, think out what you just learned.

我需要补充的是,您必须注意CI上的代码审查和质量检查。 稍后,我们将讨论这些主题,但是现在,请考虑一下您刚刚学到的知识。

If you are mature enough so that you have your way to spoil if statement, let me know in comments. I will keep this post updated.

如果您足够成熟,可以通过声明破坏自己,请在评论中告诉我。 我将保持此职位更新。

翻译自: https://habr.com/en/post/516822/

android 暗码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值