threads / discuss / 18075

coding style: #ifdef blocks and real C blocks

Subject: coding style: #ifdef blocks and real C blocks

## tl;dr

4 messages between Mar 1, 2009 and Mar 1, 2009.

replies: 3people: 2as markdown or json

Tay Ray Chuan· Mar 1, 2009, 08:52 UTC · lore
Hi,

I couldn't find any mention of #ifdef usage nor any example of how I'm thinking of using #ifdef, so I'm posing this here.

Right now here's what I have on my local branch:
---
#ifdef USE_CURL_MULTI
	slot = get_active_multi_slot();
#else
	slot = get_active_slot();
#endif
	slot->callback_func = process_response;
	slot->callback_data = request;
	request->slot = slot;
---

I want to implement an option that's stored in the variable "persistent_connection", which basically makes the code behave as if USE_CURL_MULTI isn't defined. (This would make git open/close less connections throughout the lifespan of its execution, which would remove/minimize the number of credential prompting if authentication is required, among other advantages.)

Here's what I thought of:
---
#ifdef USE_CURL_MULTI
	if (!persistent_connection)
		slot = get_active_multi_slot();
	else
		slot = get_active_slot();
#else
	slot = get_active_slot();
#endif
---
I thought of shortening this further to
---
#ifdef USE_CURL_MULTI
if (!persistent_connection)
	slot = get_active_multi_slot();
else
#else
	slot = get_active_slot();
#endif
	slot->callback_func = process_response;
	slot->callback_data = request;
	request->slot = slot;
---
What would you suggest?
-- 
Cheers,
Ray Chuan
Junio C Hamano· Mar 1, 2009, 09:10 UTC · re: Tay Ray Chuan · lore

Re: coding style: #ifdef blocks and real C blocks

Tay Ray Chuan <rctay89@gmail.com> writes:
Show 8 quoted lines
> #ifdef USE_CURL_MULTI
> 	slot = get_active_multi_slot();
> #else
> 	slot = get_active_slot();
> #endif
> 	slot->callback_func = process_response;
> 	slot->callback_data = request;
> 	request->slot = slot;
How about doing something like this:
    #ifdef USE_CURL_MULTI
    #define active_slot_get get_active_multi_slot
    #else
    #define active_slot_get get_active_slot
    #endif
so that the code itself would not have to have any #ifdef?
    slot = active_slot_get()
    slot->callback_func = process_response;
    slot->callback_data = request;
    request->slot = slot;
    
Show 8 quoted lines
> #ifdef USE_CURL_MULTI
> 	if (!persistent_connection)
> 		slot = get_active_multi_slot();
> 	else
> 		slot = get_active_slot();
> #else
> 	slot = get_active_slot();
> #endif
Similarly, with something like this:
    #ifdef USE_CURL_MULTI
    slot active_persistent_slot() {
            return persistent_connection ? get_active_slot() : get_active_multi_slot();
    }
    #else
    slot active_persistent_slot() {
            return get_active_slot();
    }
    #endif
the call site can be #ifdef free, no?
Tay Ray Chuan· Mar 1, 2009, 09:27 UTC · re: Junio C Hamano · lore

Re: coding style: #ifdef blocks and real C blocks

Hi,
On Sun, Mar 1, 2009 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 8 quoted lines
> How about doing something like this:
>
>    #ifdef USE_CURL_MULTI
>    #define active_slot_get get_active_multi_slot
>    #else
>    #define active_slot_get get_active_slot
>    #endif
>
Nice.
Show 14 quoted lines
> Similarly, with something like this:
>
>    #ifdef USE_CURL_MULTI
>    slot active_persistent_slot() {
>            return persistent_connection ? get_active_slot() : get_active_multi_slot();
>    }
>    #else
>    slot active_persistent_slot() {
>            return get_active_slot();
>    }
>    #endif
>
> the call site can be #ifdef free, no?
>
Hmm, so I just do "slot = active_persistent_slot()" ?
-- 
Cheers,
Ray Chuan
Tay Ray Chuan· Mar 1, 2009, 09:33 UTC · re: Junio C Hamano · lore

Re: coding style: #ifdef blocks and real C blocks

On Sun, Mar 1, 2009 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 7 quoted lines
>    #ifdef USE_CURL_MULTI
>    #define active_slot_get get_active_multi_slot
>    #else
>    #define active_slot_get get_active_slot
>    #endif
>
> so that the code itself would not have to have any #ifdef?

On further thought, wouldn't it be a better idea to make this uppercase, ie. GET_ACTIVE_SLOT, to make it more obvious this is a macro? Then we wouldn't have to jumble the name into "active_slot_get" to differentiate it from the two slot functions.

-- 
Cheers,
Ray Chuan

← back to recent threads