wiki:EFLWebKitCodingStyle

Version 9 (modified by gyuyoung.kim@samsung.com, 9 years ago) (diff)

Add new coding style related to double not operator(!!)

Summary

  • EFL port should adhere to the WebKit Coding Style basically.(http://www.webkit.org/coding/coding-style.html)
  • Do not use abbreviation except for public APIs
  • Do not use Eina_Bool type except for public APIs
  • Place '*' operator to data type
  • Use C++ casting for type casting. For example, static_cast<...>, const_cast<...>, reinterpret_cast<...>
  • Use smart pointers
  • Use c++ new/delete operators
  • Use const keyword instead of #define when you define constant variable.
  • Use NULL in public C headers instead of 0
  • Use double not operator(!!) when you modify a Eina Bool member variable which stores a value in a bit field.

Example

Do not use abbreviation except for public APIs

Public APIs

 EAPI void ewk_view_bg_color_set(Evas_Object *o, int r, int g, int b, int a);

Implementation

 void ewk_view_bg_color_set(Evas_Object* ewkView, int red, int green, int blue, int alpha)
 {
     EWK_VIEW_SD_GET_OR_RETURN(ewkView, smartData);
     EINA_SAFETY_ON_NULL_RETURN(smartData->api);
     EINA_SAFETY_ON_NULL_RETURN(smartData->api->bg_color_set);
 
     if (alpha < 0) {
         WRN("Alpha less than zero (%d).", alpha);
         alpha = 0;
     } else if (alpha > 255) {
         WRN("Alpha is larger than 255 (%d).", alpha);
         alpha = 255;
     }
  ...
 }

Do not use Eina_Bool type except for public APIs

Public APIs

EAPI Eina_Bool ewk_view_scale_set(Evas_Object *o, float scale_factor, Evas_Coord cx, Evas_Coord cy);

Implementation

Eina_Bool ewk_view_scale_set(Evas_Object* ewkView, float scaleFactor, Evas_Coord centerX, Evas_Coord centerY)
{
    EWK_VIEW_SD_GET_OR_RETURN(ewkView, smartData, false);
    EWK_VIEW_PRIV_GET_OR_RETURN(smartData, priv, false);
 
    float currentScaleFactor = ewk_view_scale_get(ewkView);
    if (currentScaleFactor == -1)
        return false;
    int x, y;
    ewk_frame_scroll_pos_get(smartData->main_frame, &x, &y);
 
    x = static_cast<int>(((x + centerX) / currentScaleFactor) * scaleFactor) - centerX;
    y = static_cast<int>(((y + centerY) / currentScaleFactor) * scaleFactor) - centerY;
    priv->page->setPageScaleFactor(scaleFactor, WebCore::LayoutPoint(x, y));
    return true;
}

Use "void" for empty parameter of public APIs

EAPI Ewk_Context *ewk_context_new(void);

Wrong

EAPI Ewk_Context *ewk_context_new();

Place '*' operator to data type

Right

static void _ewk_view_smart_show(Evas_Object* ewkView)

Wrong

static void _ewk_view_smart_show(Evas_Object *ewkView)

Use C++ casting for type casting

Right

 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(data);

Wrong

 Ewk_View_Smart_Data* smartData = (Ewk_View_Smart_Data*)data;

Use smart pointers

Right

 RefPtr<cairo_region_t> dirtyRegion = adoptRef(cairo_region_create_rectangle(&rect));

 or 

 OwnPtr<WebCore::Page> page = adoptPtr(new WebCore::Page(pageClients));

Wrong

 cairo_region_t* dirtyRegion = cairo_region_create_rectangle(&rect);
 ...
 cairo_region_destroy(dirtyRegion);

 or

 WebCore::Page* page = new WebCore::Page(pageClients);
 ...
 delete page;

Use c++ new/delete operators

Right

Where it is not possible to use smart pointers. Use new/delete operators.

 unusedCacheEntry = new Ewk_Tile_Unused_Cache_Entry;
 ...
 delete unusedCacheEntry;

Wrong

 unusedCacheEntry = static_cast<Ewk_Tile_Unused_Cache_Entry*>(malloc(sizeof(Ewk_Tile_Unused_Cache_Entry)));
 ...
 free(unusedCacheEntry);

Use const keyword instead of #define when you define constant variable

Right

 const int defaultTileWidth = 256;
 const int defaultTileHeigth = 256;

Wrong

 #define DEFAULT_TILE_W (256) 
 #define DEFAULT_TILE_H (256)

Use NULL in public C headers instead of 0 (but keep using 0 in implementations and in private headers).

Right

/**
 * Query action for this intent.
 *
 * @param intent intent object to query.
 *
 * @return the intent action if successful or @c NULL otherwise.
 */
EAPI const char *ewk_intent_action_get(const Ewk_Intent *intent);

Wrong

/**
 * Query action for this intent.
 *
 * @param intent intent object to query.
 *
 * @return the intent action if successful or @c 0 otherwise.
 */
EAPI const char *ewk_intent_action_get(const Ewk_Intent *intent);

Use double not operator(!!) when you modify a Eina Bool member variable which stores a value in a bit field.

Right

struct {
    ...
    bool pageCache : 1;
    ...
}

Eina_Bool ewk_view_setting_page_cache_set(Evas_Object* ewkView, Eina_Bool enable)
{
    EWK_VIEW_SD_GET_OR_RETURN(ewkView, smartData, false);
    EWK_VIEW_PRIV_GET_OR_RETURN(smartData, priv, false);
    enable = !!enable;
    if (priv->settings.pageCache != enable) {
        priv->pageSettings->setUsesPageCache(enable);
        priv->settings.pageCache = enable;
    }
    return true;
}

Wrong

Eina_Bool ewk_settings_auto_load_images_set(Ewk_Settings* settings, Eina_Bool automatic)
{
    EINA_SAFETY_ON_NULL_RETURN_VAL(settings, false);

    automatic = !!automatic;
    WKPreferencesSetLoadsImagesAutomatically(settings->preferences.get(), automatic);

    return true;
}