PIXNET Logo登入

Foxbrush

跳到主文

Where I Record Myself

部落格全站分類:財經政論

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 4月 05 週四 201221:30
  • [C/C++] 參數數量不固定的函數實作


在 c 程式裡要使用不定參數的作法如下:
1. 要含入 STDIO.H 和 STDARG.H :
#include
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(3,073)

  • 個人分類:Development in SW
▲top
  • 3月 26 週一 201223:41
  • [Test] How to Write 3v1L, Untestable Code


How to Write 3v1L, Untestable CodeThursday, July 24, 2008 5:03 PM

by Miško Hevery, Jonathan Wolter, Russ Ruffer, Brad Cross, and lots of other test infected Googlers
This guide lists principles that will help you write impossible to tests code. Or, avoiding these techniques will help you write code that can be tested.


  • Make Your Own Dependencies - Instantiate objects using new in the middle of methods, don't pass the object in. This is evil because whenever you new up an object inside a block of code and then use it, anyone that wants to test that code is also forced to use that concrete object you new'ed up. They can't "inject" a dummy, fake, or other mock in to simplify the behavior or make assertions about what you do to it.

  • Heavy Duty Constructors - Make constructors that do lots of work in them. The more work you do in the constructor, the hard it is to create your object in a test fixture. And if your constructor can construct other things that are hard themselves to construct, that's even better! You want the transitive dependencies of every constructor to be enormous. Enormous is hard to get under test.

  • Depend on Concrete Classes - Tie things down to concrete classes - avoid interfaces wherever possible. (They let people substitute the concrete classes you're using for their own classes which would implement the same contract in the interface or abstract class. Testing do-gooders might want to do this to get your code under a test harness - don't let them!)

  • Conditional Slalom - Always, always, feel good when writing lengthy if branches andswitch statements. These increase the number of possible execution paths that tests will need to cover when exercising the code under test. The higher the Cyclomatic complexity, the harder it is to test! When someone suggests to use polymorphism instead of conditionals, laugh at their thoughtfulness towards testing. Make the branching both deep and wide: if you're not consistently at least 5 conditionals deep, you're spoon feeding testable code to the TDD zealots.

  • Depend on Large Context Objects - Pass around ginormous context objects (or small ones with hard to construct contents). These will reduce the clarity of methods [myMethod(Context ctx) is less clear than myMethod(User user, Label label)]. For testing, the context objects will need to be created, populated, and passed around.

  • Use Statics - Statics, statics everywhere! They put a great big crunch in testability. They can't be mocked, and are a smell that you've got a method without a home. OO zealots will say that a static method is a sign that one of the parameters should own the method. But you're being 3v1L!

  • Use More Statics - Statics are a really powerful tool to bring TDD Infected engineers to their knees. Static methods can't be overridden in a subclass (sometimes subclassing a class and overriding methods is a technique for testing). When you use static methods, they can't be mocked using mocking libraries (nixing another trick up the pro-testing engineer's sleeve).

  • Use Global Flags - Why call a method explicitly? Just like L Ron Hubbard, use "mind over matter" to set a flag in one part of your code, in order to cause an effect in a totally different part of your application (it's even more fun when you do it concurrently in different threads!). The testers will go crazy trying to figure out why all of a sudden a conditional that was evaluating true one minute is all of a sudden evaluating to false.

  • Use Singletons Everywhere - Why pass in a dependency and make it obvious when you can use a singleton? It's hard to set up a test that requires singletons, and the TDDers will be in for a world of hurt when all their tests depend on each other's state.

  • Be Defensive - They're out to Get Your Code! - Defensively assert about the state of parameters passed in methods, constructors, and mid-method. If someone can pass in a null, you've left your guard down. You see, there are testing freaks out there that like to instantiate your object, or call a method under test and pass in nulls! Be aggressive in preventing this: rule your code with an iron fist! (And remember: it's not paranoia if they really are out to get you.)

  • Use Primitives Wherever Possible - Instead of using a "cookie object," pass in primitives and do all the parsing you need, every time you need a value. Primitives make people work harder by having to parse and massage them to get the data out -- where objects are mockable (gasp) and nullable, and encapsulate state (who'd want to do that?)

  • Look for Everything You Need - By Looking for things you are asserting your objects dominance as the object which knows where everything is. This will make the life of tester hard, since he will have to mimic the environment so that your code can get a hold of what it needs. And don't be afraid of how many objects you need to reach out to to, the more the harder it will be for test to mock them all out in unisin. If you are an InvoiceTaxCalculator, feel free to do things like: invoiceTaxCalculator.getUser().getDbManager().getCaRateTables().getSalesTaxRate(). Cover your ears when some testing weenie tells you about Dependency Injection, Law of Demeter, or not looking for things.

  • Use static initializes - Do as much work as possible when your class is loaded. Testing nuts will be so frustrated when they find out just loading your class causes nasty stuff like network or file access.

  • Couple functional code directly to the external systems it depends on If your product uses external systems such as a databases, file systems or a network, make sure your business logic is coded to reference as many low level implementation details of these systems as possible. This will prevent others from using your code in ways you don't intend, (like small tests that run in 2 ms instead of 5 minutes).

  • Mix Object Lifecycles - Have long lived objects reference short lived objects. This confuses people as the long lived object references the short lived object still after it's no longer valid or alive. This is especially insidious, both bad design, and evil, hard to test.

  • Side Effects are the Way to Go Your best bet is to perform a large number of side effect producing operations in your methods. This is especially true for setters. The more non-obvious the side effects better. Peculiar and seemingly irrational side effects are particularly helpful for unit testing. To add another layer of sweet creamy goodness on top, you want to make it possible to initialize your objects in an invalid state, with uninitialized member fields. Once you have achieved this, be sure to make calls on the methods of the uninitialized fields as side effects from your setters in order to cause SEGV's or NPE's, depending on your language's vernacular. Why go to all this trouble? Clean, readable, and testable code that works, that's why! Side effect free functions are for intellectual weaklings that think a function name should give some kind of an indication of what the function does.

  • Create Utility Classes and Functions/Methods - For instance, you have a String which is a URL you're passing around (obeying "Use Primitives Wherever Possible"). Create another class with static methods such as isValidUrl(String url). Don't let the OO police tell you to make that a method on a URL object. And if your static utility methods can call to external services as well, that's even better!

  • Create Managers and Controllers - all over the place have these Managers and Controllers meddling in the responsibilities of other objects. Don't bother trying to pull that responsibility into other individual objects. Look at a SomeObjectManager class and you have no idea what it's going to do.

  • Do Complicated Creation Work in Objects - Whenever someone suggests you to use a Factory to instantiate things, know that you are smarter than them. You're more intelligent than they must be, because your objects can have multiple responsibilities and be thousands of lines long.

  • Greenlight if-branches and switch statements - Go ahead, don't feel dirty about nesting if-branches. It's "more readable" that way. OO cowboys will want to have a whole polymorphic soup of collaborating objects. Say no to the OO-ist. When you nest and branch conditionals, all you need to do is read the code from top to bottom. Like a great novel, one simple linear prose of code. With the OO-overboard paradigm, it's like a terrible choose-your-own-adventure kid's book. You're constantly flipping between classes and juggling patterns and so many more complex concepts. Just if-things out and you'll be fine.

  • Utils, Utils, Utils! - Code smell? No way - code perfume! Litter about as many util and helper classes as you wish. These folks are helpful, and when you stick them off somewhere, someone else can use them too. That's code reuse, and good for everyone, right? Be forewarned, the OO-police will say that functionality belongs in some object, as that object's responsibility. Forget it, you're way to pragmatic to break things down like they want. You've got a product to ship after all!

  • Use "Refactoring" whenever you need to get away with something - This is a word that Test-Driven and OO-goons like. So if you want to do something far reaching, involving new functionality, without tests, just tell them you're "Refactoring." It'll trick them every time. No matter that they think you need to have tests around everything before you can refactor, and that it should never add new functionality. Ignore their hubbub, and do things your own way!

Java Specific

  • Final Methods - Use final classes and methods all the time. They can't be overridden for testing (-; But don't bother making fields final, or using value objects (without setters) - just let your objects' state be changed by anything and anyone. No sense in guaranteeing state, it'd make things too easy.

  • Handcuff your users to Specific Types - Use instanceof as much as possible. This will make Mocking a pain, and show people that you're in control of the objects allowed.

C++ Specific

  • Use non-virtual methods - Unless you need to override the method in a deep and scary inheritance hierarchy, just make the method non-virtual. If you make it virtual, a TDD zealot may mock your class! An even nicer trick is to keep your destructor non-virtual - then when the testing freaks try to subclass, whooooaoaaaaaa....

  • Never use pure abstract classes - Depending on pure abstract classes is a sure-fire way to let the TDD crazies inject stubs and mocks into your code, making it testable.

  • Macros are your friends - Always use #ifdef PROD and other compile-time switches to make sure the testies can't get at your really important blocks of code and test them. In fact, this code won't even run: until it gets to production!
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(9)

  • 個人分類:Development in SW
▲top
  • 3月 14 週三 201223:31
  • [Web] ResPonsive Mobile Nav Patterns


http://bradfrostweb.com/blog/web/responsive-nav-patterns/
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(5)

  • 個人分類:Development in SW
▲top
  • 3月 12 週一 201219:01
  • [Web] Simulate Ajax effect for file upload


Simulate Ajax effect for file upload
 
1) Create a simple HTML form for file upload
2) Set the target to an iFrame which is on the actual page but not visible
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(2)

  • 個人分類:Development in SW
▲top
  • 3月 11 週日 201221:47
  • [CSS] Corner Ribbon


HTML
    
NEWS

​
 
 
CSS
 
 .wrapper {
  margin: 50px auto;
  width: 280px;
  height: 370px;
  background: white;
  border-radius: 10px;
  -webkit-box-shadow: 0px 0px 8px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 8px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 8px rgba(0,0,0,0.3);
  position: relative;
  z-index: 90;
}
.ribbon-wrapper-green {
  width: 85px;
  height: 88px;
  overflow: hidden;
  position: absolute;
  top: -3px;
  right: -3px;
}
.ribbon-green {
  font: bold 15px Sans-Serif;
  color: #333;
  text-align: center;
  text-shadow: rgba(255,255,255,0.5) 0px 1px 0px;
  -webkit-transform: rotate(45deg);
  -moz-transform:    rotate(45deg);
  -ms-transform:     rotate(45deg);
  -o-transform:      rotate(45deg);
  position: relative;
  padding: 7px 0;
  left: -5px;
  top: 15px;
  width: 120px;
  background-color: #BFDC7A;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#BFDC7A), to(#8EBF45));
  background-image: -webkit-linear-gradient(top, #BFDC7A, #8EBF45);
  background-image:    -moz-linear-gradient(top, #BFDC7A, #8EBF45);
  background-image:     -ms-linear-gradient(top, #BFDC7A, #8EBF45);
  background-image:      -o-linear-gradient(top, #BFDC7A, #8EBF45);
  color: #6a6340;
  -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 3px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 3px rgba(0,0,0,0.3);
}
.ribbon-green:before, .ribbon-green:after {
  content: "";
  border-top:   3px solid #6e8900;
  border-left:  3px solid transparent;
  border-right: 3px solid transparent;
  position:absolute;
  bottom: -3px;
}
.ribbon-green:before {
  left: 0;
}
.ribbon-green:after {
  right: 0;
}​
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(3)

  • 個人分類:Development in SW
▲top
  • 1月 31 週二 201222:53
  • [Web] 如何善用雲端服務,加速網站產品開發


與網頁技術無關的服務Google Analytics我們利用 Google Analytics 來追蹤使用者在我們網站上的讚數、留言數、收藏數,另外我們也用來追蹤每日登入的會員數還有一部分的電子商務營收數據。使用 Google Analytics 的好處是它提供了強大的多維度報表分析功能,我們可以透過許多方式來檢討我們每天、每週的經營成效。另外,透過即時線上人數的統計,我們可以很直接的觀察到各類行銷活動的成效,這也非常有趣,當你看到同時在線人數有上百人的時候,真的會有一種既感動又興奮,同時願意再多熬夜拼個幾天的感覺XDMailchimp這是另外一個發信服務,Mailchimp 的好處是有完整的後台可以讓行銷人員自行編輯信件的內容,進而發送電子報。換句話說,我們目前不花費任何開發者的時間開發相關的功能,先外包出去,以後有時間、有更進階的需求,才拉回來自己做。Mailchimp 同樣也會提供統計數據,讓你了解發信量、開信量等,你甚至可以看到是哪些人有點開你的電子報,進而將這些人依不同的維度劃分成不同的群組,未來可以進行簡單的客製化電子報派送。就小規模的團隊與服務來說,這是顧客關係管理的第一步。
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(3)

  • 個人分類:Development in SW
▲top
  • 1月 18 週三 201212:05
  • [Web] .htaccess - Apache


Apache .htaccess manual 
Apache目錄保護 .htaccess 的制作
利用.htaccess來保護主機下的目錄與檔案
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(0)

  • 個人分類:Equipment in SW
▲top
  • 1月 16 週一 201222:23
  • [Web] Build a Responsive Table per Screen Size


http://filamentgroup.com/lab/responsive_design_approach_for_complex_multicolumn_data_tables/
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(9)

  • 個人分類:Development in SW
▲top
  • 1月 09 週一 201216:04
  • [JS] onmouseout for dropdown menu - 2


http://www.scriptiny.com/2008/04/sliding-javascript-dropdown-menu/
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(1)

  • 個人分類:Development in SW
▲top
  • 12月 26 週一 201113:37
  • [JS] Scroll Effect


Example 
 
(繼續閱讀...)
文章標籤

Foxbrush 發表在 痞客邦 留言(0) 人氣(3)

  • 個人分類:Development in SW
▲top
«1...16171819»

個人資訊

Foxbrush
暱稱:
Foxbrush
分類:
財經政論
好友:
累積中
地區:

熱門文章

  • (3,073)[C/C++] 參數數量不固定的函數實作
  • (301)[SCRUM] Scrum 是什麼(2):Scrum 的內涵
  • (72)微軟「失落的十年」
  • (46)富小孩、窮小孩... ...
  • (14)[SCRUM] Scrum 是什麼(14):好問題
  • (9)零肆柒捌
  • (8)一樣水,百百貓喝... ...
  • (5)[CS] 12 Unit Testing Tips for Software Engineers
  • (2)The Fall of Microsoft
  • (2)[Web] 如何挑選適合的 Hosting Plan?

文章分類

  • Investment in Finance (10)
  • Refinement in Design (1)
  • Knowledgement in CS (13)
  • Equipment in SW (25)
  • Development in SW (116)
  • Management in Business (4)
  • Enlightenment in Soul (5)
  • Amazement in Life (4)
  • Argument in Thoughts (3)
  • Sentiment in Whispers (1)
  • 未分類文章 (1)

最新文章

  • 科技業薪水大揭密
  • [Money] Introduction to T-Theory – Magic-T
  • [Stock] 到底要工作還是玩股票
  • [Server][NodeJS] Set-up SSL in NodeJS and Express using OpenSSL
  • [Server] How to get HTTPS working on your local development environment in 5 minutes
  • [MAC] How to open multiple Terminal windows or tabs unsing commands
  • [WEB] mitmproxy
  • [Server] Socket.io 的架構
  • [Server] Socket.io 架構
  • [Money] 教你如何找出轉強股票,不藏私大公開

最新留言

  • [23/06/22] 小克 於文章「[Linux] 讓 Linux 下的中文...」留言:
    您好,我是此篇文章的原作者 您已侵權 請尊重 CC ...
  • [23/05/14] 訪客 於文章「[Linux] 讓 Linux 下的中文...」留言:
    超感謝!...
  • [22/07/02] Sean 於文章「[Linux] 讓 Linux 下的中文...」留言:
    您好, 我跟著https://www.youtube....
  • [22/04/11] 訪客 於文章「[Stock] 如何最精準的掌握到關鍵得...」留言:
    原本在搜尋引擎找出一堆 Blog 文章,不知哪幾篇值得花時間...
  • [21/08/20] ANDRES 於文章「[Linux] 讓 Linux 下的中文...」留言:
    好人,造福LINUX新手,謝謝...
  • [21/08/02] Skyer 於文章「[Linux] 讓 Linux 下的中文...」留言:
    非常棒的設定,非常感謝!...
  • [12/11/29]  於文章「[Web] Install Apache...」留言:
    雲 青 青 兮 欲 雨 ,  水 澹 澹 兮 生 煙 。 ...
  • [09/09/15] liwayhuang 於文章「一樣水,百百貓喝... ......」發表了一則私密留言
  • [09/09/08] pheen 於文章「什麼是行銷!?...」留言:
    為什麼要鎖? 文章,呵。...
  • [09/08/08] liwayhuang 於文章「富小孩、窮小孩... ......」留言:
    mm...interesting...

文章精選

文章搜尋

誰來我家

參觀人氣

  • 本日人氣:
  • 累積人氣: