Step-by-step teaching you to write non-maintainable PHP code

Source: Internet
Author: User

Translator Note: This is a great article, using interesting narrative, from the reverse side to explain what is a good PHP engineer, what you can not do. Please note that the behavior listed in this article is what you should try to avoid.

As unemployment gets higher, many people realise how important it is to preserve their jobs. So, what is a good way to keep your job and make yourself irreplaceable? A very simple fact is that as long as your code is no one can maintain, then you have successfully saved the job. Writing non-maintainable code is a special skill, but oddly enough, it seems to be natural for some developers. But for the rest of the developers, here are some tips and hints to get you started writing non-maintainable code.

The first thing to do

The first step is to start by looking for a job. You should look for the right company, where you can play and realize your non-maintainable potential. You don't have to be the company's PHP Daniel if that's better. When looking for a job, if the job description mentions the need to move from other to PHP (so you know you will), or you can search for misleading work that requires 10 years of PHP5 work experience, plus skilled use of FrontPage and Netscape Composer.

Once you get this golden opportunity, take action from day one. Speak at the meeting and let your opinion be heard. Talk about the object-oriented architecture design, the enterprise, the reform plan, how to make it better, and of course you have to make corresponding commitments. Make sure everyone is consulted on important coding links.

Non-maintainable core

Inspired by the excellent article "Writing unmaintainable Code" (the people who want to keep the job will see it), here are two important concepts you need to master and master:

    1. You should make it impossible for others to modify anything easily unless he destroys something else.

      Maintenance people don't have time to understand your code. Maintainable code means being able to quickly navigate to a specific part of a mountain of code and quickly understand how it works and modify it without destroying something. You can't do this. Don't let others easily search for something or find it where they want to be.

    2. Your code cannot be "seemingly" non-maintainable (because others will suspect it) and it must be "is" not maintainable.

      The code in the maintenance staff should look normal, but give them a surprise when they least expect it.

Best practices
  1. prohibit code contracts. about coding and naming conventions are endless to scold. Such a situation must not be seen in your excellent organization. You have a fantastic project to do, and you can't spend countless hours discussing tab or space. Besides, the agreement is the limit. If a newcomer is in the job and he is not used to your agreement, he will be miserable. Unhappy programmers are inefficient programmers. Whoever asks you, you explain to them. Let everyone write code in the style they like. As for your own code-transform your conventions. Monday was camelCase named after the small hump, Tuesday in all_lowercase full lowercase, Friday mixed, with Hungarian nomenclature on every February 29.
  2. do not write comments. Your code is beautiful, it doesn't need to be commented on. If someone doesn't understand your code, chances are they're not such a good programmer. If, with this possibility, you are forced to write a comment, write it directly and exaggerate it. Detailed description of the most obvious and least important code, skip the others.

        // 下面这一块代码    // 我们增加两个变量:    // 命名为变量 a 和变量 b    // 两个都是整数    // 声明变量 a    // 并给它赋值整数 1    $a = 1;    // 声明变量 b    $b = 2;    // 给变量 a 和 b 求和    // 声明和初始化见上面    // 并且将结果赋值给    // 一个新的变量 c    $c = $a + $b;
  3. use Notepad to encode. or use other editors that do not have code indentation to display. Make other people endure the pain and eventually leave the team. So you don't always have to listen to their complaints. If someone asks you why you are using Notepad, be prepared to explain: because it comes from Windows (the only one today, the operating system for creative programmers), there is no need for any training or cost. I'm sure you can find it online. You can use any of the program's references, even Word, to write your Web page code, but only Notepad is the real authority, after all, your company hired only you are the authority.
  4. Reject unit tests. explain to anyone who doubts you that you are being hired to write high-quality, no-loophole code (and therefore, no testing). Why would a sane person take the time to write irrelevant tests to verify that the code works? Some things in life like-the sky is blue, the sun rises in the east, your code is able to run normally, so thank you very much. Go ahead (like a comment, if you're forced to test, be prepared to test the obvious and skip the rest)
  5. do not use the template engine. the template engine can help you differentiate between the business logic layer and the presentation layer. It can guarantee the maintainability of the code so you can't follow this rule. PHP's father Rasmus Lerdorf said: "PHP is the template engine." Even if you're forced to use a template engine, find a way to misuse it, such as putting some business code in the template, or carefully mixing HTML (and CSS and JavaScript) code in the database access layer.
    In general, try to mix your php,html,css and JavaScript code confidently in the same line of code as possible. Create JavaScript and HTML code with inline styles in your PHP code. If someone asks and tells them that this pattern is called "encapsulation," you are responsible for your code.

  6. version control. While this is hard to avoid, it's worth trying to get rid of any form or version control. You can demonstrate that this improves communication among team members, rather than relying on cold-blooded version control software. If you have not persuaded anyone, please do not despair. You can not commit all when you submit. Keep Some of your own code locally. That way, if someone outside of you tries to build and deploy, these small, lethal snippets will break the project. If caught, argue that the code is not suitable for presentation, after all, you have submitted high-quality code and excellent solutions that educate the junior team. These little boys and little girls will look up to you and be full of expectations!
  7. build a framework. then you inevitably become an architect, and your authority is beyond doubt. This way you can add a few secret conventions (most of which are sometimes self-contradictory), even the most experienced maintainers are unaware. Your framework will be responsible for all things, without anyone bothering to understand it, and everyone will be happy because you make development easier and increase the capacity of your entire company. Do not publish your framework in an open source, because a) this framework is the company's assets and the company invests a lot of money, b) The open source community will laugh at you, and this will be your bluff end.

Naming related

Your variable name should be a bit cryptic, preferably with just one letter. This makes it impossible for anyone to find what they need through a simple search.

Class names and methods are best defined using a single letter. If you really want to define a normal name, use it all the time-remember that the best way to hide information is to use it frequently. When you re-use the same name (called "Object-oriented Programming"), if you put parentheses and braces on a new line, it will help to improve the readability of your code, and you'll have to revisit the regular expression when your teammates look for anything in your code. Think about it:

    $noodles = 1;    class    noodles    {        var $noodles = 2;        function            noodles            ()        {            $noodles[‘noodles‘] = ‘noodles‘;        }    }    function        noodles() {            return new noodles;        }    $noodles = noodles();    var_dump($noodles);

You can also use a fancy character set to name variables. The Cyrillic alphabet is perfect because some letters look like the Roman alphabet, but they are not (all: Xopekacmebctakxoph). Then the following output is:

    $alert = 1;    $аlert = 2;    echo $alert;

2? If the second one alert starts with the Cyrillic letter "A", you can't!

Reference related

Even if you define something very normal, it doesn't mean that you can't use it in an interesting way. The main weapons are:

    • eval()
    • Variable variable
    • Variable classes, such as$strudels = "noodles"; $noo = new $strudels;
    • call_user_func()

Basically any language structure that treats code as a string is your best friend.

    // calling abc();    $z = ‘A‘;    call_user_func($z .‘bC‘);
Capital

The letter example, the function method name is not case-sensitive, abusing this feature.

function abc(){    echo "abc";}AbC();

On the other hand, the array's Kin (key) is sensitive to case sensitivity and also abuses this feature.

$a[‘UseConvetionsOnlyTobreakThem‘] = 1;if (isset($a[‘UseConvetionsOnlyToBreakThem‘])) {    // ?? 大写 B !!1!}
Rewrite

Overriding global variables, especially hyper-global variables, is not expected. Rewrite the $_GET attributes in the array as early as possible, and $_POST so on. On $_REQUEST top of doing some humble rewriting as embellishment. If you are on wtf-ed, you can explain the XSS attacks, injection attacks, and other virus attacks that prevent user input.

Control structure

Use, mix, match all the alternatives if ,,, while for foreach , switch syntax. If asked, all of this, please explain that you are training new employees to learn the real language.

if ($a > 5):  if ($a > 4) {      while ($a > 0):        echo --$a;      endwhile;  }endif;

Nested ternary operators, there is no better, more concise code.

// 猜猜这里输出什么echo true ? ‘true‘ : false ? ‘t‘ : ‘f‘;

In for the body of the loop, again increasing $i to keep everyone's attention. Or, by not using it $i to achieve the loop increment surprise. Never.

Nest loops, drill down, and then suddenly jump out of them (loops). Like break 2 and break 3 This code is for entertainment, especially when mixed with strange indentation code.

This is a start!

That's all for today. I want you to believe you can do it yourself, and you can write non-maintainable code. Now your future is in your hands! Of course, you can also write code that is relatively readable, but at the risk of being replaced.

For more modern PHP knowledge, go to the laravel/php knowledge community

Step-by-step teaching you to write non-maintainable PHP code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.