# coding style: #ifdef blocks and real C blocks

4 messages from 2009-03-01 to 2009-03-01. Participants: Tay Ray Chuan, Junio C Hamano.
Thread: https://gitlist.dev/t/18075

## Tay Ray Chuan, 2009-03-01 08:52

Subject: coding style: #ifdef blocks and real C blocks
Message-ID: <be6fef0d0903010052t50551f3w74352b69afdee620@mail.gmail.com>
URL: https://gitlist.dev/e/be6fef0d0903010052t50551f3w74352b69afdee620%40mail.gmail.com

```
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, 2009-03-01 09:10

Subject: Re: coding style: #ifdef blocks and real C blocks
Message-ID: <7vbpsl8v7r.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vbpsl8v7r.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <be6fef0d0903010052t50551f3w74352b69afdee620@mail.gmail.com>

```
Tay Ray Chuan <rctay89@gmail.com> writes:

> #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;
    
> #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, 2009-03-01 09:27

Subject: Re: coding style: #ifdef blocks and real C blocks
Message-ID: <be6fef0d0903010127q7b1491d6o8cdb48acdc674603@mail.gmail.com>
URL: https://gitlist.dev/e/be6fef0d0903010127q7b1491d6o8cdb48acdc674603%40mail.gmail.com
In-Reply-To: <7vbpsl8v7r.fsf@gitster.siamese.dyndns.org>

```
Hi,

On Sun, Mar 1, 2009 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:
> 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.

> 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, 2009-03-01 09:33

Subject: Re: coding style: #ifdef blocks and real C blocks
Message-ID: <be6fef0d0903010133s7343a96ekd8939695a65ba4a2@mail.gmail.com>
URL: https://gitlist.dev/e/be6fef0d0903010133s7343a96ekd8939695a65ba4a2%40mail.gmail.com
In-Reply-To: <7vbpsl8v7r.fsf@gitster.siamese.dyndns.org>

```
On Sun, Mar 1, 2009 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:
>    #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

```
