Several OffsetOuts and good coding practice

hey, i have picked up that its good coding practice not to declare all variables at the top. how would you go about a structure like this, where i would like to declare a variable triggers followed by other variables and i already use triggers to send it to a bus, map the masked triggers from the bus to \trigMask.ar and send the maskedTriggers to an fx with feedback, so i would like to apply the grain window to the fx afterwards, so i send it to a bus. All these OffsetOuts

var triggers, maskedTriggers, grainWindow;
triggers = Impulse.ar(tFreq.lag(0.02));
OffsetOut.ar(\trigOut.kr(0), triggers);
maskedTriggers = triggers * \trigMask.ar(1);
OffsetOut.ar(\maskedTrigOut.kr(0), maskedTriggers);
grainWindow = ...
OffsetOut.ar(\grainWindowOut.kr(0), grainWindow);

so the question is in two parts, first how to go about variables in the SynthDef and the second how to deal with these different OffsetOuts and the mapping of busses, is there a more elegant way. thanks

huh - I definitely DO declare all var at the top, so I’m wondering why there is the impression that this is a problem?

I DO try to keep all my variables closely scoped (where a var is declared only within a block that will use it). But the nature of SC, as a language, in this regard, you are encouraged to declare all vars at the top. I honestly dislike the assignment of values to vars at the same time. It makes it harder (for me) to read. But - I’m also very used to C which has this requirement as well. And I prefer it (personally)

hey, thanks for your reply i dont know anymore which thread it was where this was said.
But considering all these OffsetOuts it would be nice to have several blocks of code instead of just putting them one after the other. Im normally using enough space between these different things,so its more readable, but i guess there could be some adjustments.

Only if you’re stuck with C89 :slight_smile: Since C99 you can declare variables anywhere in a block.

I declare all vars at the top like Josh, not because of C, but precisely because of the issue you’re describing – in many cases it is clearest and most readable to intersperse assignments with non-assignments.

I certainly am. I also still use CommonLISP :smiley:

1 Like

…practice not to declare all variables at the top…

In the “traditional” context temporary declarations are something the “system” looks after.

When you “accept” (save) a method the system tells you what names aren’t declared and offers to declare them for you.

It also tells you what temporaries are declared but not referenced, and offers to delete them.

In that context the temporaries list is more or less “read-only”.

Not sure which, if any, of the Sc editors do this? (The Emacs mode doesn’t, or if it does it doesn’t for me!)

Writing names out twice for no particular reason isn’t super appealing, so I’m happy there’s the initializer form!

thanks for all your replies :slight_smile: