I have always wondered about this, especially in C/Java style languages. For example, consider the first 3 lines of this C# code:
lock (serviceLock)
using (var client = new ServiceClient())
try
{
// Do something
client.DoSomething();
}
catch (System.ServiceModel.FaultException ex)
{
// Handle error
addLog(ex.ToString());
}
This usage has always seemed somewhat questionable to me. However, the alternative is not very practical either (3+ levels deep of indentation for a service call). Apart from pure aesthetics, is there any practical reason to add or not to add the extra brackets in this case?
-
13What seems questionable here is making a service call while holding a lock!AakashM– AakashM12/18/2013 10:41:53Commented Dec 18, 2013 at 10:41
6 Answers 6
The lock
and using
blocks are for programmer convenience, but are not required. If you find less indented code easier to read. You can write the same thing using the non-code-block version of the same C# features.
Monitor.Enter(serviceLock);
var client = new ServiceClient();
try
{
client.DoSomething();
}
catch(FaultException ex)
{
addLog(ex.ToString());
}
finally
{
Monitor.Exit(serviceLock);
client.Dispose();
}
The above does the same thing, but there is no readability issue or confusion as to what it's doing.
The alternative approach is to use blocks and break everything into separate functions.
public function foo()
{
lock(serviceLock)
{
fooService();
}
}
private function fooService()
{
using(ServiceClient client = new ServiceClient())
{
fooClientTry(client);
}
}
private function fooClientTry(ServiceClient client)
{
try
{
fooClient(client);
}
catch(System.ServiceModel.FaultException ex)
{
addLog(ex.ToString());
}
}
private function fooClient(ServiceClient client)
{
// work....
}
I prefer the above approach. It looks like a lot more work/code but it's easier to maintain because each function performs a single task. When you get into multiple nested blocks this approach keeps everything only a few indentations.
-
1+1 for
Monitor
. It's less idiomatic but certainly good to have it up your sleeveKonrad Morawski– Konrad Morawski12/19/2013 08:04:33Commented Dec 19, 2013 at 8:04 -
The statement that your code "does the same thing" as a
lock
statement is incorrect. Section 8.12 of the C# language specification describes the code that is generated by the compiler: it calls theMonitor
methods in a specific way to avoid potential race conditions.Cole Campbell– Cole Campbell01/13/2014 21:09:39Commented Jan 13, 2014 at 21:09 -
1-1.
lock(x)
andusing
were created to eliminate the ugliness oftry...finally
. They handle the acquisition and release reliably in a single place. It is all too easy to forget to release a manually acquired resource.kevin cline– kevin cline01/13/2014 21:58:01Commented Jan 13, 2014 at 21:58 -
The
client.Dispose()
call can throw an exception, which would mean that theMonitor.Exit(serviceLock);
statement would not execute. I don't think this is equivalent to the original code. (I don't like the style of the original code, though, and would prefer the indentation.)Dr. Wily's Apprentice– Dr. Wily's Apprentice01/14/2014 15:41:26Commented Jan 14, 2014 at 15:41 -
Monitor
is what the MSDN says to use if you don't want to useusing
, and the code is their example. I never useMonitor
but I offered two options. Break it down into smaller functions or useMonitor
. msdn.microsoft.com/en-us/library/de0542zzReactgular– Reactgular01/14/2014 15:54:49Commented Jan 14, 2014 at 15:54
I would strongly suggested you use three levels of nested indentation. When I look at this code quickly, I only see the catch(){} block, see a match try{} block, and ignore the fact that there is a lock and using statement. Now, when I look at the code more carefully, I noticed that there are keywords hanging out above the try, but their scope may not be immediately apparent to me--are they just for the try block, or are they for the try/catch statement? If you need to add code at some point, I can almost guarantee someone will miss the lock or using statements (the same reason why most coding standards I've seen prohibit placing only one line after an if statement and not using curly braces). I would go with the most specific code, which is three levels of indentation using proper brackets so that the scope is immediately understandable.
If the indentation bothers you that much, I suggest using 2 spaces instead of tabs (which should be an option in your IDE). It's something I picked up at my job, and have really fallen for as I use it. It's much more concise than having a two inch gap because you indented four times.
-
I would suggest using caution regarding changing the tab spacing options in your IDE; all team members should use the same settings, otherwise your code will become a mess of different styles (some code indented with 2 spaces, some with 4, some with tabs). Otherwise, +1 for this post. I find alternative styles that attempt to condense code to be harder to read (i.e. takes longer for my brain to parse), since I can't take advantage of the semantic cues that conventional code structure/style provides.Dr. Wily's Apprentice– Dr. Wily's Apprentice01/14/2014 15:51:40Commented Jan 14, 2014 at 15:51
I'd say I'm against it. I'll come around and add some code after try...catch
without noticing that I should have extended the scope of lock
.
And, more importantly, this version doesn't really reduce indentation!
It only hides it. Deep nesting is primarily a logical problem, not an aesthetical one.
The fact that we see it as ugly is a result of it being a bad programming practice; not the other way round.
(Aesthetics by itself is relative - eg. this highly upvoted answer claims that "deeply nested structural statements – "arrowheads" – were, in languages like Pascal, once seen as beautiful code.")
Three levels is still fine, but if you reach - say - seven levels, does removing braces from lock
and using
, and de-indenting, address the issue really?
Quite the contrary, it desensitizes the developer to the problem, thus reducing the chance someone will go ahead and refactor the thing.
For a dissenting opinion, I rather like this practice with one significant change.
lock (serviceLock) using (var client = new ServiceClient()) try
{
// Do something
client.DoSomething();
}
catch (System.ServiceModel.FaultException ex)
{
// Handle error
addLog(ex.ToString());
}
By not stacking, it forces the eye to read all the way across as one long statement. If the line is too long or does not read well, then you know you want to reorganize the logic.
I often do this with double for-loops and for-loops with try catch blocks.
for (int i = 0; i < x.Length; ++i) for (int j = 0; j < x[i].Length; ++j) try
{
// each cell in a ragged matrix: x[i][j]
}
catch (/* ... */)
{
// exception in one cell allowing other cells to be processed.
}
Above all else, clarity is the most important thing. If you are rigid with any style, you will end up with ugly and unreadable code in some cases. Be flexible in a way that allows you to highlight the logic. If you don't know if something is clear, then have your co-developers to look at it. Ask them what the code does without explaining it to them.
-
5I like your first example but not your second.Pieter B– Pieter B12/19/2013 02:36:51Commented Dec 19, 2013 at 2:36
I have no problem with stacking the locks and usings, but the try-catch should be wrapped in braces. That will require you to think what the scope of the using
statement is in this context.
lock (serviceLock)
using (var client = new ServiceClient())
{
try
{
client.DoSomething();
}
catch (System.ServiceModel.FaultException ex)
{
addLog(ex.ToString());
}
}
I stack keywords all the time (mostly using
bit also stuff like fixed
and unsafe
) because sometimes I would be adding six or more braces. There's no way you can read that at a glance and tell which brace matches which. Also it increases the risk that the code for your method won't fit on the screen. That's not good for readability either.
The rule is simple. Indentation needs to be consistent with scope. The lock
is applying until the end of the catch
block and needs to look like it. I'm not familiar with the using
keyword in C# but from your code the same rule looks to apply.
-
1You confused the
using
directive (the equivalent of Java's imports - and in C# you can't put it anywhere else than the top of the file, either) with theusing
statement (which is what the OP used). Entirely different beasts. See msdn.microsoft.com/en-us/library/sf0df423.aspx and msdn.microsoft.com/en-us/library/yh598w02.aspx and stackoverflow.com/questions/2943542/using-keyword-in-javaKonrad Morawski– Konrad Morawski12/19/2013 08:52:35Commented Dec 19, 2013 at 8:52 -
Yeah, he is, but +1 for "The lock is applying until the end of the catch block and needs to look like it.", and for the fact that he probably would have said the same for
using
if it weren't for C#'s silly dual uses of the keyword.Ross Patterson– Ross Patterson01/13/2014 21:43:44Commented Jan 13, 2014 at 21:43 -
@KonradMorawski fixed (weeks later)djechlin– djechlin01/13/2014 21:48:09Commented Jan 13, 2014 at 21:48