Friday, 7 September 2012

Coding Styles Again, or Getting LISPy?


Although in my previous post I argued that coding styles and formatting are only smoke and mirrors, latetly I found myself increasingly entangled in a dilemma concerning just that question. Namely, I suddenly found pleasure in writing my C++ statements using expressions rather than direct flow control constructs.
Want an expample? Here it is:
1
2
3
4
// housekeeping
(type == QAmmMeasModel::MeasChannelNode)
  ? removeItemRef(m_measDefEntries, m_measDefFilesSz, defFileName, defFileIndex)
  : removeItemRef(m_rangeDefEntries, m_rangeDefFilesSz, defFileName, defFileIndex);
instead of the more mundane (or, if you want, normal):
1
2
3
4
5
6
7
8
if(type == QAmmMeasModel::MeasChannelNode)
{
  //........
}
else
{
  //........
}
As such, it is rather to be seen as an innocent idiosyncracy of mine, but it didn't stopped there, culminating in the following substituton. Instead of the rather boring:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// houskeeping
if(type == QAmmMeasModel::MeasChannelNode)
{
  assert(m_measDefEntries.contains(key));
  assert(m_measDefFiles.contains(defFileName));
 
  m_measDefEntries.remove(key);
   
  if(m_measDefFiles[defFileName] == 1)
    m_measDefFiles.remove(defFileName);
  else
    m_measDefFiles[defFileName]--;
}
else
{
  assert(m_rangeDefEntries.contains(key));
  assert(m_rangeDefFiles.contains(defFileName));
 
  m_rangeDefEntries.remove(key);
  
  if(m_rangeDefFiles[defFileName] == 1)
    m_rangeDefFiles.remove(defFileName);
  else
   m_rangeDefFiles[defFileName]--;
}
I came to consider using something more funky, like:
1
2
3
4
5
6
7
8
// lispy?
(type == QAmmMeasModel::MeasChannelNode) ?
 (m_measDefEntries.remove(key),  
    ((m_measDefFiles[defFileName] == 1) ?
     m_measDefFiles.remove(defFileName) : m_measDefFiles[defFileName]--)) :
 (m_rangeDefEntries.remove(key),  
    ((m_rangeDefFiles[defFileName] == 1) ?
     m_rangeDefFiles.remove(defFileName) : m_rangeDefFiles[defFileName]--));
You must admit that the second version is more succint, looks better and transmits some indefinite "guru" feeling. Unfortunately I had to let the asserts go, but it didn't occur to me as a great loss. So why did I delete the second version in the end? Simple. It was written for my client, and should be readable to all C++ programmers. In that way coolness was sacrificed on the altar of mediocrity ;) for the filthy lucre's sake ;).

Later in the day I chose the simpler solution and just wrote the removeItemRef() function hiding the ugly if() clause, but if I didn't do it, what do you think, which of the two versions should I had included in the code?

No comments: