Whenever I hear “Look at all the code I wrote!”, I flinch. It’s because it’s almost always a #CowboyDeveloper saying this. And what he's actually saying is “Look at all the problems I’ve created!”.





#sam reid#interview with the vampire#the vampire lestat#iwtv

seen from Switzerland

seen from Japan
seen from Switzerland
seen from Switzerland
seen from Switzerland
seen from Bolivia
seen from Switzerland
seen from Finland
seen from Russia

seen from United States
seen from China
seen from South Korea
seen from Russia
seen from Switzerland
seen from South Korea
seen from Belgium
seen from United States
seen from China
seen from Thailand
seen from United States
Whenever I hear “Look at all the code I wrote!”, I flinch. It’s because it’s almost always a #CowboyDeveloper saying this. And what he's actually saying is “Look at all the problems I’ve created!”.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
The fewer conditions you test for, the better your code “tastes”.
To the best of my ability to discern, the crux of the “good taste” requirement is the elimination of edge cases, which tend to reveal themselves as conditional statements.
Applying the Linus Torvalds “Good Taste” Coding Requirement [ Brian Barto ]
Complexity & Decisions
How many decisions is does your code make?
Software is a complex thing, and it will always be difficult to write and maintain if its complexity isn't well managed. Complexity can be measured in the problems we attempt to solve, or in the software we write to solve them.
In today's blog post I wanted to take a look at one particular software metric that I feel provides a reliable insight into how complex our code is. It measures the logical complexity of code as opposed to its time complexity.
Cyclomatic complexity
This fancy term refers to the measurement of the number of decisions made by a program. It is best applied to functions or methods to gain a quantitative understanding of how complex they are, from one perspective.
So when is our code making a decision?
If conditions
Loop conditions
Switch cases
Catch statements
Simply by counting the occurrences of these, plus one, we can determine the cyclomatic complexity of a program.
It is suggested that a complexity of 10 or less is acceptable as a simple, testable function or method without much risk.
Let's take a look at an example PHP method.
Python: Radon でコードの可読性を可視化する
「コードが汚い」と一口に言っても、何がどう汚いのかを自然言語で説明していくのは大変だと思う。 コーディングスタイルに関してはチームでルールを決めたり、あるいはフォーマッタを使って対応できるけど、コードの複雑さに関してはどうだろうか? 今回紹介する Radon は Python で書かれたコードの可読性を CC (Cyclomatic Complexity) や MI (Maintainability Index) といった数値を使って定量的に示してくれる。 まずは PIP を使って Radon をインストールする。
$ pip install radon
測定する対象は、折角なので Radon 自体にしてみよう。
$ git clone https://github.com/rubik/radon.git $ cd radon/
CC (Cyclomatic Complexity) まずは CC から測ってみる。 詳細な説明な省くけど、この数値は関数やメソッド毎に個別に計算されて、それが小さければ小さいほど単純で読みやすいコードを示す。 測定は radon cc コマンドで。 テストの測定はいらないので -e オプションで除外している。 デフォルトで結果は A ~ F の 6 段階評価で示されるが、実際の数値も表示したい場合には -s オプションを使う。
$ radon cc -s -e "tests/*" . radon/__init__.py F 4:0 main - A (2) radon/cli.py F 236:0 raw - B (10) F 177:0 cc - B (7) F 145:0 mi - B (6) F 80:0 _print_cc_results - A (5) F 105:0 analyze_cc - A (4) F 129:0 analyze_raw - A (4) F 43:0 log - A (2) F 56:0 log_list - A (2) F 67:0 _format_line - A (2) F 62:0 log_error - A (1) ...(省略)
結果の最も左側にある一文字は F が関数、M がメソッド、C がクラスを表す。 ランクが低い (最も右側の数値が大きい) ものはリファクタリング対象と言える。 リファクタリング対象の閾値を何処に設けるか (あるいは設けないか) はプロジェクト次第だろうね。 特定のランク以下だけを表示したいときは -n オプションが使える。 これでリファクタリング対象を素早く見つけられる。
$ radon cc -s -e "tests/*" -n "b" . radon/cli.py F 236:0 raw - B (10) F 177:0 cc - B (7) F 145:0 mi - B (6) radon/raw.py F 71:0 _get_all_tokens - B (6) radon/tools.py F 51:0 cc_to_dict - B (7) F 40:0 build_custom - B (6) radon/visitors.py M 169:4 ComplexityVisitor.generic_visit - B (7) radon/pathfinder/__init__.py F 15:0 walk_and_filter_generator - C (19) F 76:0 find_paths - B (6) radon/pathfinder/filters.py M 118:4 SizeFilter.accepts - B (6)
MI (Maintainability Index) 次に MI を測ってみる。 この数値はコードのメンテナンス性を示すもので、今度は 0 ~ 100 の間で数値が高ければ高いほど良い。 測定は radon mi コマンドで。 オプションについては CC のときと同様。
$ radon mi -s -e "tests/*" . setup.py - A (100.00) radon/__init__.py - A (73.04) radon/cli.py - A (56.45) radon/complexity.py - A (81.03) radon/metrics.py - A (71.95) radon/raw.py - A (65.58) radon/tools.py - A (67.53) radon/visitors.py - A (54.43) radon/pathfinder/__init__.py - A (68.85) radon/pathfinder/filters.py - A (58.31) docs/conf.py - A (92.88)
モジュール単位で当たりを付けられるのは便利そう。 以上、Radon を使うことで Python で書かれたコードの可読性を定量的に測ることができた。 今回扱ったコードを計測するという概念は、コードメトリクスとかソフトウェアメトリクスって分野かな。 めでたしめでたし。

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Cyclomatic complexity
This is one of those terms programmers use that make other people look dumbfounded at them. Even when you talk about it to other programmers you get a weird look, like they think you were doing some voodoo.
Although people give you strange looks when talking about it, cyclomatic complexity is one of the most intuitive thoughts we can have when creating algorithms.
It always is.