Placeholder Image

字幕列表 影片播放

  • Hey, Tak, lead here and welcome back to another episode today.

    嗨!照過來,歡迎收看今天的最新一集

  • I wanted to talk to you about my seven favor.

    今天我想要談談七個我最喜歡的反設計模式。我們先來喝杯星巴克冰咖啡

  • Anti patterns were drinking Starbucks iced coffee today.

    還不差。為什麼了解反設計模式很重要?

  • Not too shabby now.

    我想到的主要有兩個原因但也許更多

  • Why our anti patterns important to know about.

    第一個是它對工作保障很有用。你可以用反設計模式從根本上模糊掉程式碼讓它難以理解

  • And there's two main reasons I can think about, but there may be plenty more.

    將程式碼變成一坨垃圾

  • The first is very useful for job security.

    品質差到除了你以外沒有人懂原始碼所以你就變成唯一一個懂的人

  • You can use anti patterns to essentially obvious Kate.

    你會變成唯一了解程式碼的人然後很快地團隊中每個人就會跑來找你問問題

  • The code so much make it so incomprehensible.

    這段程式碼做什麼的?它怎麼跑的?因為它看起來迴圈很多而且越執行越奇怪

  • Turned the coat into such garbage quality that nobody else is able to understand.

    這個策略可以保住你的飯碗、甚至幫你爬到技術領導人的等級、就跟我一樣

  • And you'll be the only person who is able to understand the code.

    反設計模式很明顯的第二個用途是復仇

  • You'll be the only one who can read it.

    當你身為一個不爽的員工所以想藉由毀掉程式碼復仇

  • Everyone on the team will soon become into you to ask, How does the code function?

    或者你只是想看著周遭失火,那反設計模式就是一很棒的方法

  • How does that work?

    原因是如果程式碼很明顯地太爛,大多人都不會批准

  • Because it is so loopy and so strange.

    你必須把程式碼寫得至少看起來合理以通過程式碼審核

  • Practiced over time.

    反設計模式具有一定的品質讓它看起來合情合理

  • The strategy can help you secure your job and even help you obtain the row of tack.

    看起來這段程式碼很不錯、但隨著時間過去會把原始碼變得一團糟

  • Lied like it did for myself.

    而且這種對程式碼健康度的傷害

  • The second obvious usage for anti patterns is revenge.

    基本上是無法恢復的,對人們來說會耗費很長的時間和精力

  • If you're just out to destroy a code base because you're a disgruntled employee, or maybe you just want to watch the world burn then and time partners are an excellent way to do it.

    去重構以及寫出比較簡潔的程式碼。有一件會在這影片討論到的事情是很多團隊

  • The reason is that many people will not approve code that is obviously bad.

    乾脆就不花時間重構讓程式碼看起來清爽點

  • You need the code to look at least legitimate to at least get through code reviews.

    為了簡潔起見。這種對原始碼的結構性傷害一旦發生一次就變得非常難以恢復

  • Anti patterns have that certain quality, where it looks like the code is legitimate.

    你知道反設計模式只是一種工具,它可用在好也可用在壞的地方

  • Looks like the code is good, and yet it will turn the code base into absolute mush.

    在這影片中我想解釋如何用它

  • Overtime in this sort of damage to code health is essentially irreversible.

    來看看第一點訣竅:建立一堆函數。如果你有一個運作中的函數

  • It will take a long time and a lot of effort for people to re factor and make a cleaner.

    把它拆解成三個或四個函數

  • And one thing that would discuss here is that many teams simply will not take the time to re factor Coach Bass to make it cleaner just for cleanliness sake.

    再把每一個拆解成更多函數,很快地

  • That sort of structural damage to a code base, once it's done, is very difficult to reverse.

    要看程式碼的唯一途徑就是在函數之間跳來跳去

  • You know, an anti pattern is just a tool.

    它骨子裡其實是一坨義大利麵程式碼但讀起來不像

  • It can be used for good or bad.

    看起來也不像因為每個東西都被拆解成小小的函數

  • I'm just here to explain how to use them.

    所以你很容易通過程式碼審核

  • Let's get into it.

    今天如果有人問你為什麼要這麼做

  • Tip number one, Create the tons of functions.

    你就可以說這比較容易做單元測試

  • If you already have a function that's doing something, break it down into three or four other functions each and for each of those, break them down into other functions to.

    然後每個函數你都寫一個單元測試這樣就可以解釋為什麼需要這麼多函數

  • And pretty soon, the only way to read your code is the jump from function to function to function.

    因為你讓它們更好測試。另外你可以做的事情是試著把任何一行重複的程式碼拿掉

  • It's essentially spec Eddie code, and yet it's not going to read like it is not going to look like it, because everything is broken down into neat little functions and you'll be able to easily get this sort of code passed through code review.

    你可以說嘿這加法運算已經重複出現兩個地方

  • Now, if anybody ever asks you, why are you doing this?

    所以你建立一個函數這樣就不用到處複製貼上

  • You could just say that you're making it more units testable.

    你可以寫個函數像是加兩個數值,這就是函數的名字、就只是加上兩個數值

  • And so for each function, you write a unit test, and you could help justify why you need so many different functions.

    你也可以建立另一個函數像是「建構函數」或是建構迴圈或建構 switch 陳敘式函數

  • You're making it easily testable.

    如果你整個原始碼看過一遍

  • Thea Other thing that you can do is you can try removing any lines of duplicate coat.

    你幾乎一定可以在某些地方找到一模一樣的程式碼

  • You can just say, Hey, this ad, the operation is being done in two different places in the code, so you're just going to make a function so that you don't have to duplicate that all over the place.

    大多數的程式設計師都被訓練成不該重複程式碼

  • You can create a function like adds two numbers.

    所以你可以很容易地通過程式碼審核。第二點訣竅是:盡量使用單行和撲朔迷離的程式碼

  • That's the name of the function, and it just ask two different numbers.

    有一些多行的程式碼

  • You can create another function like creates function or create four loop create switch statement.

    你可以輕易的把他們連結起來變成單行程式碼像是用 and 或 or 運算子

  • If you look through a code base, you almost certainly be able to find some area where there's some duplicate coat being used.

    我也喜歡用位元操作,取代一堆布林值

  • Most programmers are trained to believe that you should not be duplicating code.

    你可以輕鬆地合併變成單一位元組陣列然後做位元運算

  • You're easily be able to get this through code review.

    為了搞懂這些布林值其他人在問你時你就可以說這作法更有效率,每個人都喜歡

  • The second tip is to use as many one liners and cryptic code as possible.

    位元操作

  • If there are multiple lines of code, you can usually join them all together into a single line using, say, the end or the or operator.

    他們覺得這比較進階。還有一個我在我團隊中看過一些人在用的技巧是只要有個 if 陳敘式

  • I also love using bit manipulations.

    你就可以在條件式中放兩個驚嘆號,這樣你就有「假假值」了

  • Instead of using multiple bullies, you can combine them, are into a single bite array and then do bit manipulation to figure out the bullion values.

    技術上來說這比較正確,因為在進入 if 陳敘式判斷之前它把一個數值轉成真正的布林值

  • When people ask, you can just say that this is going to be more efficient.

    這看起來超撲朔迷離的

  • Everyone loves but manipulations.

    如果你想要檢查你的所有輸入參數

  • They think it's more advanced.

    把錯誤檢查放在每個地方,你就可以說你只是在保護程式碼

  • Another trick you can do, which I've seen some people on my team, do is any time.

    不只是針對惡意使用者檢查

  • There's an if statement in the condition you put a double exclamation mark.

    同時也在針對那些潛在的惡意開發人員,他們或許想要駭入或不正確地使用你的程式

  • So you're taking the not not of a value and technically, this is more correct because it turns the value into an actual bullion.

    甚至可能會有伽瑪射線隨機干涉變數的值,或你的程式碼有可能在

  • Before you compare in the F statement, it looks extra cryptic.

    多執行緒的環境下執行而你的變數

  • You wanna be our checking all of your parameters for our checking all over the place, and you could just say they were trends of safe your guard your code against not only malicious users but potentially malicious the flippers as well.

    可能隨著每一行程式碼而改變,所以你想要每一行都檢查並驗證變數

  • Who may be trying to hack your program, who may be using the program incorrectly?

    你也想替每個地方加上同步鎖。第三個訣竅是使用遞迴

  • You know, there could even be gamma raised are flipping the valleys of variables randomly, or your coat could potentially be running in the multi threaded environment in which the variables could be changing every single line, and so that you want to check and validate the variable values each line you want to add and synchronization locks all over the place.

    我們知道任何遞迴都可以用迭代的方式實作,反過來也是

  • Tip number three is to use Riker JH in Now.

    你可以把任何迭代函數變成遞迴

  • While we all know that any recursive function can be made intuitive, the reverse is also true.

    你要做的只是建立一個遞迴函數並加上額外的參數

  • You can take any intuitive function and make it recursive as well.

    那個參數便是迭代中每次隨迴圈遞增的計數器,這本質上就是在

  • What you do is you just create a recursive function at an extra parameter.

    模擬一個 for 迴圈。所以任何你需要使用迴圈列舉時

  • That's the adoration count a increment that each cycle this essentially simulates a four loop.

    你可以改建立一個遞迴,大家都愛這些遞迴函數,因為它們很進階很酷

  • And so any time you need to reiterate through loop instead, you can create a recursive function, and everybody loves these recursive functions.

    但現實中一旦人們在讀原始碼,每次遇到這些函數都會使他們停下來,他們不得不心裡想

  • They're going to look so advanced, so cool.

    「這遞迴到底在幹什麼的啊?」

  • In reality, when people are reading through, the code is going to stop them every time they hit this function.

    你知道的,你的腦袋只是被一堆迴圈佔據想著那些程式在幹嘛罷了

  • And they need to mentally think, What is this Ryker shin doing?

    但這類程式碼都可以順利通過審核

  • It's just loopy in your head, trying to figure out was trying to do but the serve coast.

    技術而言另一件你可以做的事是把整個程式都放在遞迴函數中

  • Just have no problem getting through could review, you know, technically, the other thing you can even do is you could take your entire program and put the inside the recursive function.

    你可以建立一個需要整數參數的函數,這個參數代表哪一行程式碼該被執行,每次遞迴都加一

  • You'll create a function, accepts an integer that's a line of code.

    然後整個函數的內容都是一堆 if 陳敘式檢查該執行哪一行

  • I should be executed increments each recursive integration.

    執行那一行程式碼後再次呼叫自己進到下一輪遞迴

  • And so the body of the function would essentially be a bunch of if statements checking which line of code to execute it would then execute that line of code, and they recursive Lee call itself again.

    你可以把你的整個程式完全用像這樣的遞迴寫

  • You could essentially right your entire program in the recursive manner like this.

    遞迴越多越好。理所當然每所學校

  • You know, the more workers in the better every college course teachers Rikers in you want put this knowledge to good to use, safeguard your career this way.

    都會教你遞迴,所以你會想把這方面的知識充分利用以確保你的職場生涯

  • Tip number four is to make good use of your comments.

    第四個訣竅是充分利用你的註解

  • I love writing huge paragraphs of comments that describe exactly what the code is doing.

    我喜歡寫一大篇的註解以精確地解釋這段程式碼在幹嘛

  • One great thing about this is that it makes it such that the coat becomes incredibly difficult to change.

    有一點很棒的是這會讓程式碼變得難以置信地

  • Because wouldn't you start modifying that you need to modify a huge paragraphs of text?

    難以改動,因為一旦你改了你就必須連同一大篇的註解一起改

  • The more comments you write, the more you can solidify this chunk of code, such that no one else is able to modify.

    你寫的註解越多你的程式碼片段就越穩固,因為其他人改不動它

  • One great low trick you can do is at the end.

    還有一個你可以用的小技巧是在註解的結尾

  • You could just say, Please do not modify this piece of code.

    說「請不要改這段程式碼,它非常嬌嫩」

  • It is very delicate.

    「改了會影響到重要系統功能和一堆檔案,這是一段前人留下的程式碼,不該碰的」

  • It is affecting critical system functions, a bunch of different files.

    因為它不該改所以沒有人可以改那段程式碼

  • It is legacy code, and they should not be touched.

    除了你以外,即便那段程式碼隨著時間過去不再扮演關鍵角色

  • It should not be modified.

    沒有人知道該怎麼改那段程式碼

  • And then nobody was going to be able to modify this piece of code except you, even though it may not be doing any critical functions.

    所以當你離開公司後那段程式碼就會變得古老、擺在那邊然後開始發爛

  • Overtime.

    一大篇註解還有一很棒的一點

  • No one else will know how to touch this piece of code, and then when you leave, is going to stay there and remain ancient code and schooling to just sit there and run the other great them by having lots of comments, especially at the top of a file.

    尤其檔案的開頭有一大串註解時,那就是當人們在讀程式碼時

  • If you have a huge page of comments, as people are reading the code, they need to be jumping back and forth between the code and the comments to try to understand what the program is doing.

    他們必須上上下下在程式碼和註解間跳來跳去試著理解程式碼在做什麼

  • Things get especially funding when the comments don't exactly match what the code is doing.

    尤其是註解和程式碼真正在做的事不太一致但註解卻說別改程式碼時,情況會變得特別有趣

  • And yet the common say to not modify the code.

    超棒的!

  • That's great.

    我的第五點訣竅是加一堆你可能會用到、但從來沒用到的程式碼

  • My fifth tip is to start adding code that you might need but never will.

    這跟加了一大坨額外的功能和額外的設定選項有關

  • And so this is about at the inn tons of additional functionality, extra features, lots of extra configuration options that nobody's ever going to use, the coast not even being run.

    沒有人會需要用到那些東西、甚至執行時也不會執行到、沒有人會用到

  • No one's using it, but you know you can say, Well, hey, this might be used for one day.

    但你可以說這些有一天會很有用、也許某人會用到它。

  • Someone might be using it.

    你也可以過度設計類別達到同樣效果,與其是一個簡單的類別像是「狗狗」,你可以說這狗狗繼承自

  • You can also do this by over engineering classes, So instead of credit and simple class for like a dog.

    犬科、犬科又繼承自哺乳類然後人類比照做。你可以導入

  • You can say that this dog object inherits from Ah canine object, which inherits from mammal object and the animal object than the person.

    額外的協定跟介面,那個物件就會有額外的行為。另外與其簡單到爆的類別

  • You can also have a conformed to additional protocols and interfaces such that this object and 1/2 additional behaviors, he said.

    你可以建立大概五個不同的類別、介面和協定

  • That credit and single class you can create, like five different classes, interfaces, protocols for each thing that you're trying to create.

    加在每個你正在做的事情。而如果你很行

  • And if you're good, you're going to name each of the's something different.

    你就會替他們取不同的名字。像是「狗狗 View」、「犬科 Controller」、「寵物類別」

  • It could be like dog view, canine controller, pet class, just all sorts of random names.

    就一堆隨便取的名字,人們在追你的程式碼時就會變得超困難因為每個東西都

  • And people are going to have a really hard time trying to follow your code because everything is named something differently.

    都命名得不一樣。訣竅六是用一大堆變數、越多越好

  • Tip number six is to use lots of variables.

    也許你只需要一個變數,但你大可以說為了更好的效能所以需要快取變數

  • The more the better.

    然後建立大約十個不同的變數全都相依在單一變數上

  • You may only need one variable, but you could just say that you're trying to cash the values, be more performance and create, like 10 different variables that all depend on this one single variables value.

    這是一個替類別加上額外複雜度的絕佳方法,而且特別棒的是當你不更新那些變數時

  • This is a great way to add additional complexity to a class, and it's especially great if you don't keep the valleys of thes variables updated like you might have a variable called is done, but you have another very broke off is not done.

    例如你有個變數叫「已完成」

  • And sometimes these values are going to be the same.

    但你有另一個變數叫「尚未完成」

  • And that's simply because you didn't update that you only updated the initialization or something like that, and then later it changes and you don't update it.

    有時候這這兩個變數的值一樣,因為你沒更新其中一個或只在初始化時更新...等等

  • That's going to cause a lot of confusion.

    接下來你不更新那個變數,這可以造成一堆混亂

  • You can also start Mon to find the value of these variables in places people aren't expecting, like in get her methods.

    你也可以在人們意想不到的地方更新那些變數,例如

  • When people are getting the value of a parameter or something like that, you can change the value like you could have a function called get temperature.

    用來給人們取得參數的值的屬性存取子...諸如此類

  • But every time you call it another variable, value changes like, say, the location changes functional test.

    你可以改一個值用方式像你可以有個函數叫「取得溫度」

  • They're not going to be able to catch this sort of thing because they may only call them function once.

    但每次你呼叫那個函數,另一個變數也跟著變,例如位置變動

  • If they were to call the function twice, it might fail.

    至於功能性測試

  • But usually tests are only made to call Once my last and seventh IP is to just start re factoring the code because you think it's messy and you know chances are people are going to agree with you and just tell people that you're going to make it cleaner.

    人們不會知道這類事情會發生,因為他們也許只會呼叫一次

  • You're going clean things up and make it look better for you.

    如果他們連續呼叫兩次

  • It's always fun to see when engineers start re factoring code to make their own personal lives.

    也許就會出錯但

  • Better to solidify the current feature set when they have no understanding of the future road map or current business needs.

    通常單元測試只會呼叫這個函數一次。我的最後一個也是第七個訣竅是開始動手重構因為

  • So that'll do for me.

    你覺得它亂七八糟

  • Let me know what your favorite anti patterns are.

    我們知道重構的機會是當人們一致同意它很亂後你就跟說你會把程式碼弄乾淨點

  • If you like the video, give the like and subscribe and see you next time bye.

    你會開始大掃除、讓程式碼看起來漂亮些

Hey, Tak, lead here and welcome back to another episode today.

嗨!照過來,歡迎收看今天的最新一集

字幕與單字

影片操作 你可以在這邊進行「影片」的調整,以及「字幕」的顯示

B1 中級 中文 程式碼 函數 變數 訣竅 類別 呼叫

軟件反模式。如何摧毀開發者的代碼庫? (Software Anti-Patterns: How to destroy a codebase for developers)

  • 13 1
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字