#PARAN SILVERLIGHT#
  • Tistory
    • 관리자
    • 글쓰기
Carousel 01
Carousel 02
Previous Next

Ch8 Breaking Down Giant Expressions

SOFTWARE ENGINEERING/Art Of Readable Code 2012. 11. 23. 11:30

1. Another Creative Way to Simplify Expressions

 

void AddStats(const Stats& add_from, Stats* add_to) {


 add_to->set_total_memory(add_from.total_memory() + add_to->total_memory());
 add_to->set_free_memory(add_from.free_memory() + add_to->free_memory());
 add_to->set_swap_memory(add_from.swap_memory() + add_to->swap_memory());
 add_to->set_status_string(add_from.status_string() + add_to->status_string());
 add_to->set_num_processes(add_from.num_processes() + add_to->num_processes());
 ...

 

 }

 

 After ten seconds of careful scrutiny, you might realize that each line is doing the same thing,
 just to a different field each time:

 

 add_to->set_XXX(add_from.XXX() + add_to->XXX())

 

 In C++, we can define a macro to implement this:

 

 void AddStats(const Stats& add_from, Stats* add_to) {
 #define ADD_FIELD(field) add_to->set_##field(add_from.field() + add_to->field())
 ADD_FIELD(total_memory);
 ADD_FIELD(free_memory);
 ADD_FIELD(swap_memory);
 ADD_FIELD(status_string);
 ADD_FIELD(num_processes);
 ...
 #undef ADD_FIELD
 }

 

2. Breaking Down Giant Statements

 

var update_highlight = function (message_num) {


    if ($("#vote_value" + message_num).html() === "Up") {
        $("#thumbs_up" + message_num).addClass("highlighted");
        $("#thumbs_down" + message_num).removeClass("highlighted");
    } else if ($("#vote_value" + message_num).html() === "Down") {
        $("#thumbs_up" + message_num).removeClass("highlighted");
        $("#thumbs_down" + message_num).addClass("highlighted");
    } else {
        $("#thumbs_up" + message_num).removeClass("highighted");
        $("#thumbs_down" + message_num).removeClass("highlighted");
    }
};

 

//his is also an instance of the DRY—Don’t Repeat Yourself—principle):


var update_highlight = function (message_num) {

    var thumbs_up = $("#thumbs_up" + message_num);
    var thumbs_down = $("#thumbs_down" + message_num);
    var vote_value = $("#vote_value" + message_num).html();

    var hi = "highlighted";


    if (vote_value === "Up") {
        thumbs_up.addClass(hi);
        thumbs_down.removeClass(hi);
    } else if (vote_value === "Down") {
        thumbs_up.removeClass(hi);
        thumbs_down.addClass(hi);
    } else {
        thumbs_up.removeClass(hi);
        thumbs_down.removeClass(hi);
    }
};

 

3. Explaining Variables

 

Here is an example:
    if line.split(':')[0].strip() == "root":
...
Here is the same code, now with an explaining variable:
    username = line.split(':')[0].strip()
    if username == "root":

 

4. Summary Variables

 

var CleanReply = function (request,  reply)
{
    //For example, consider the expressions in this code:
    if (request.user.id == document.owner_id) {
        // user can edit this document...
    }

    if (request.user.id != document.owner_id) {
        // document is read-only...
    }

    //stated more clearly by adding a summary variable:
    var user_owns_document = (request.user.id == document.owner_id);

 

    if (user_owns_document) {
        // user can edit this document...
    }

    if (!user_owns_document) {
        // document is read-only...
    }

}

 

5.Using De Morgan’s Laws (positive is easy to readable)

 

var Contains = function (request,  reply)
{
/*    1) not (a or b or c) ⇔ (not a) and (not b) and (not c)
    2) not (a and b and c) ⇔ (not a) or (not b) or (not c)*/


    //You can sometimes use these laws to make a boolean expression more readable. For instance,
    //if your code is:
    if (!(file_exists && !is_protected)) Error("Sorry, could not read file.");

    //It can be rewritten to:
    if (!file_exists || is_protected) Error("Sorry, could not read file.");
}

 


 

 

저작자표시 비영리 (새창열림)
블로그 이미지

파란실버라이트

To remember the time when I started learning Silver Light!

,

카테고리

  • Inforamtion Technology (281)
    • DESIGN PATTERN (33)
      • 실용주의 디자인패턴 (29)
    • SOFTWARE ENGINEERING (21)
      • Art Of Readable Code (12)
      • Object Oriented Programming (6)
      • TDD (2)
    • FRAMEWORK (22)
      • Spring.net (2)
      • LightSwitch (20)
    • PROGRAMING (58)
      • C# (20)
      • .NET (6)
      • HTML5 (7)
      • ASP.NET (9)
      • SILVERLIGHT (7)
      • Ruby On Rails (6)
    • PROJECT MANAGEMENT (10)
      • SW Version Management (7)
      • Schedulring Management (1)
    • BOOKS (18)
    • MOBILE APP (1)
      • SENCHA TOUCH (1)
    • SECURITY (5)
    • MES (1)
    • B2B (14)
      • WEBMETHODS (4)
    • ERP (53)
      • SAP/R/3 (51)
    • ABOUT TOOLS (2)
    • FUNDAMENT CONCEPT (21)
    • SOA BPM (22)
    • PORTFOLIO (0)

태그목록

  • 프로그래밍
  • 병렬
  • 동시성

최근에 받은 트랙백

글 보관함

링크

파란실버라이트

블로그 이미지

To remember the time when I started learning Silver Light!

LATEST FROM OUR BLOG

RSS 구독하기

LATEST COMMENTS

BLOG VISITORS

  • Total :
  • Today :
  • Yesterday :

Copyright © 2015 Socialdev. All Rights Reserved.

티스토리툴바