[code review] - ryojiIkeda style code - indentation, layout, format, code semanthics

feedback appreciated:

  • indentation
  • layout
  • format
  • code semanthics
(

~synthIkeda
=
{
	~fundamentalFreqMul=rrand(0.5,2.0);
	~pulseRate=[rrand(1,10),rrand(1,10)];

	~sig={Mix.fill(n: 9,
		function: {SinOsc.ar(
			freq: [[33,32], [322,333], [3222,3333]].choose
			* ~fundamentalFreqMul,
			mul: HenonN.ar(freq: ~pulseRate*0.5)
			*0.1)})};

	Out.ar(bus: 0,
		channelsArray: Pan2.ar(
			in: ~sig
	)*0.1);
};

~synthMasterIkeda={Mix.fill(n: 3, function: {~synthIkeda})};

~tiagoIkedaRoutine = Routine(
	func: {loop {
		Server.freeAll;
		~synthMasterIkeda.play;
		rrand(10,10*3).wait;
		}});
)

~tiagoIkedaRoutine.play;
~tiagoIkedaRoutine.stop; Server.freeAll;

Hi – good idea for a thread!

I would format it like this:

(
~synthIkeda = {
	~fundamentalFreqMul = rrand(0.5, 2.0);
	~pulseRate = [rrand(1, 10), rrand(1, 10)];

	~sig = {
		Mix.fill(
			n: 9,
			function: {
				SinOsc.ar(
					freq: [[33, 32], [322, 333], [3222, 3333]].choose
					* ~fundamentalFreqMul,
					mul: HenonN.ar(freq: ~pulseRate * 0.5) * 0.1
				)
			}
		)
	};

	Out.ar(0, Pan2.ar(in: ~sig) * 0.1);
};

~synthMasterIkeda = { Mix.fill(n: 3, function: { ~synthIkeda }) };

~tiagoIkedaRoutine = Routine {
	loop {
		Server.freeAll;
		~synthMasterIkeda.play;
		rrand(10, 10 * 3).wait;
	}
};
)

~tiagoIkedaRoutine.play;
~tiagoIkedaRoutine.stop; Server.freeAll;
  • Spaces. A lot of SC coders under-use spaces. Without spaces, it’s harder to distinguish tokens visually and the code doesn’t “breathe” on the screen. My preferences are:
    • No space around method dots.
    • A space after, but not before, , : ; – (midinote: 60), not (midinote:60). This is probably the #1 place where people leave out spaces, and the #1 place where including spaces improves readability.
    • Spaces before and after = and binary operators: ~pulseRate * 0.5, not ~pulseRate*0.5. (I sometimes make an exception for something like index-1.)
    • No spaces for [] and (). (SC prints arrays with spaces inside [] but – just IMO – I feel like that’s overkill.)
    • Spaces inside, but not outside, function braces: { ~synthIkeda }, not {~synthIkeda}. (To my eye, () and {} are close in appearance. If they are both un-spaced, then it’s harder to tell them apart. Quick: Where is the mistake here? {(({({}))})} :laughing: )
  • Indentation: If you open (), [] or {} at the end of the line, the corresponding closing brace should be at the same indentation level as the opening line. For example, in your original Out, the closing )*0.1) is at the same level as Out, but the first closing paren belongs to Pan2. That’s confusing.
  • It’s not a bad habit to use keyword arguments, but some objects are so common (Out, Routine) that it’s a bit pedantic to write Routine(func: { ... }) IMO.
  • If it’s a fairly short expression, no harm to collapse it onto one line. (You were a bit inconsistent here: both Out and ~synthMasterIkeda are short; I can’t see a reason why one is expanded and the other is collapsed.)

Hope that helps –
hjh

1 Like

Hello. I have a question about spaces that is not strictly germane to OP but I’ll ask anyway.

Why do spaces get interjected if you run this code:

4.collect({arg n;("text"+n).scramble.postln});

Some results:

tx0e t
text 1
xe 2tt
3t ext
-> [ tx0e t, text 1, xe 2tt, 3t ext ]


t x0te
1xte t
 2txet
3e ttx
-> [ t x0te, 1xte t,  2txet, 3e ttx ]

Is it possible that when sc creates an array it includes spaces for formatting but then treats them (the spaces) as part of the value of each array member. The example above is trivial, but might there be other situations where things do not go as expected…

The string concatenation operator you’re looking for is ++. The behavior of ++ and + with Strings is documented here.

So for example:

4.collect({|n| ("text" ++ n).scramble })
-> [ txet0, xtt1e, 2ttex, te3xt ]
1 Like