[squeak-dev] How to really make portion of code optional

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] How to really make portion of code optional

cedreek
Hi,

I'm looking for a way to hide/show portion of code depending on if it's in development or production mode.

First idea is to wrap the code in question around a global productionMode var. But I was wondering if there is a better way. Somethink to really hide these portions of code for efficiency. I know this is not a big deal (for my project) but I'm quite sure something may exist maybe at the VM level, so ...

Thanks in advance,

Cédrick


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] How to really make portion of code optional

jgfoster

On Aug 16, 2008, at 1:57 PM, cdrick wrote:

> Hi,
>
> I'm looking for a way to hide/show portion of code depending on if  
> it's in development or production mode.

One approach would be to use the class hierarchy to do this. In your  
abstract superclass put the shared behavior and in the subclasses put  
the unique behavior. Then instantiate one or the other depending on  
whether you are in development or in production.

> First idea is to wrap the code in question around a global  
> productionMode var. But I was wondering if there is a better way.  
> Somethink to really hide these portions of code for efficiency.

The last thing I'd worry about is efficiency. How many times do you  
think you would execute the conditional? The thing you ought to worry  
about is making the code easy to understand and maintain. The subclass  
approach would mean that the conditional would be executed only once  
(when you instantiate the object) and the remaining code would be clear.

> I know this is not a big deal (for my project) but I'm quite sure  
> something may exist maybe at the VM level, so ...

That would be an unusual approach.

> Thanks in advance,
>
> Cédrick

James Fster


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] How to really make portion of code optional

laurent laffont
In reply to this post by cedreek


On Sat, Aug 16, 2008 at 10:57 PM, cdrick <[hidden email]> wrote:
Hi,

I'm looking for a way to hide/show portion of code depending on if it's in development or production mode.

First idea is to wrap the code in question around a global productionMode var. But I was wondering if there is a better way. Somethink to really hide these portions of code for efficiency. I know this is not a big deal (for my project) but I'm quite sure something may exist maybe at the VM level, so ...

May be you can implement a sort of Strategy pattern (http://en.wikipedia.org/wiki/Strategy_pattern) in your classes, with XXXDevelopmentStrategy classes subclass/hook/wrap your XXXProductionStrategy classes to reuse code.

Can you give more details ?

--
Laurent Laffont


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] How to really make portion of code optional

cedreek
Thanks James and Laurent,

Actually this question was more to learn is it was possible. I
remember a post about keeping or not comment in the source code...

To be more precise, it's for log output of the inference system
(http://nars.seasidehosting.st/). In each step, there can be something
like 10 lines processes depending on entities are infered. The thing
is this log output chunks are mixed in the code in several places. I
could maybe use a better trigger mechanism like sending a signal with
the entity in question, but still, there will be this message sent (or
the test). I used stream and it seems ok (regarding performance), just
wanted to know if we could deactivate some portion of code as
everything seems possible here ;). I'll remind the subclass tip
though.

Cheers,

Cédrick

2008/8/17 laurent laffont <[hidden email]>:

>
>
> On Sat, Aug 16, 2008 at 10:57 PM, cdrick <[hidden email]> wrote:
>>
>> Hi,
>>
>> I'm looking for a way to hide/show portion of code depending on if it's in
>> development or production mode.
>>
>> First idea is to wrap the code in question around a global productionMode
>> var. But I was wondering if there is a better way. Somethink to really hide
>> these portions of code for efficiency. I know this is not a big deal (for my
>> project) but I'm quite sure something may exist maybe at the VM level, so
>> ...
>
> May be you can implement a sort of Strategy pattern
> (http://en.wikipedia.org/wiki/Strategy_pattern) in your classes, with
> XXXDevelopmentStrategy classes subclass/hook/wrap your XXXProductionStrategy
> classes to reuse code.
>
> Can you give more details ?
>
> --
> Laurent Laffont
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] How to really make portion of code optional

Michael van der Gulik-2


On Sun, Aug 17, 2008 at 10:56 PM, cdrick <[hidden email]> wrote:
Thanks James and Laurent,

Actually this question was more to learn is it was possible. I
remember a post about keeping or not comment in the source code...

To be more precise, it's for log output of the inference system
(http://nars.seasidehosting.st/). In each step, there can be something
like 10 lines processes depending on entities are infered. The thing
is this log output chunks are mixed in the code in several places. I
could maybe use a better trigger mechanism like sending a signal with
the entity in question, but still, there will be this message sent (or
the test). I used stream and it seems ok (regarding performance), just
wanted to know if we could deactivate some portion of code as
everything seems possible here ;). I'll remind the subclass tip
though.


 Hi Cédrick.

For this sort of thing, I usually use:

self assert: [some code].

When you put your code into production, you can make the implementation of Object>>assert: be a "^self", which has very little overhead.

You could apply the same to logging, by implementing a similar method:

self log: [ 'Some error occured.' ]. " The return value of the block is logged. "

Otherwise Keith Hodges has a logging framework, or you could use Toothpick (http://www.metaprog.com/Toothpick/) which I believe has a Smalltalk implementation.

Gulik.

--
http://people.squeakfoundation.org/person/mikevdg
http://gulik.pbwiki.com/


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] How to really make portion of code optional

keith1y

> Otherwise Keith Hodges has a logging framework, or you could use
> Toothpick (http://www.metaprog.com/Toothpick/) which I believe has a
> Smalltalk implementation.
>
> Gulik.
FYI:

The 'Logging' framework serves as a standard interface to a number of
backends of which there are 5.

Null - Null Logger does nothing.
TranscriptLog - Logs thread safely to the Transcript (part of standard
Logging package)
SimpleLog - Adaptor onto the SLLog code from Gjallar, includes:
    - startMorph
    - startFile: filename
    - startSyslog
    - startTranscript
Toothpick - Adaptor onto the toothpick framework
LogEngine - Adaptor onto the LogEngine code (not yet implemented)

best regards

Keith