<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[was ist besserer Stil !?!?]]></title><description><![CDATA[<p>ich habe mal eine kurze frage in bezug auf programmier-stil.<br />
welchen stil fuer die funktion foo(..) wuerdet ihr als besser betrachten (ein Beispiel):<br />
1.</p>
<pre><code>long foo(long pSomeLong)
{
  if( Bedingung( pSomeLong ) )
    return;

  DoBerechnung( pSomeLong );
  DoThis();
  DoThat(); 
  MaybeThisToo();
  return CalcResult( pSomeLong );
}
</code></pre>
<p>oder 2.</p>
<pre><code>long foo(long pSomeLong)
{
  if(!Bedingung( pSomeLong ) )
  {

   DoBerechnung( pSomeLong );
   DoThis();
   DoThat(); 
   MaybeThisToo();
  }
  else
    return 0;
  return CalcResult( pSomeLong );
}
</code></pre>
<p>ich denke 1. ist besser.<br />
was denkt ihr?<br />
gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/192972/was-ist-besserer-stil</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 18:20:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/192972.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 20 Sep 2007 14:41:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 14:41:24 GMT]]></title><description><![CDATA[<p>ich habe mal eine kurze frage in bezug auf programmier-stil.<br />
welchen stil fuer die funktion foo(..) wuerdet ihr als besser betrachten (ein Beispiel):<br />
1.</p>
<pre><code>long foo(long pSomeLong)
{
  if( Bedingung( pSomeLong ) )
    return;

  DoBerechnung( pSomeLong );
  DoThis();
  DoThat(); 
  MaybeThisToo();
  return CalcResult( pSomeLong );
}
</code></pre>
<p>oder 2.</p>
<pre><code>long foo(long pSomeLong)
{
  if(!Bedingung( pSomeLong ) )
  {

   DoBerechnung( pSomeLong );
   DoThis();
   DoThat(); 
   MaybeThisToo();
  }
  else
    return 0;
  return CalcResult( pSomeLong );
}
</code></pre>
<p>ich denke 1. ist besser.<br />
was denkt ihr?<br />
gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369425</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369425</guid><dc:creator><![CDATA[tuktuk]]></dc:creator><pubDate>Thu, 20 Sep 2007 14:41:24 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 14:44:00 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ganz subjektiv: Ich kann Ersteres besser/schneller verstehen und würde das deswegen vorziehen.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369426</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369426</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 20 Sep 2007 14:44:00 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 14:44:48 GMT]]></title><description><![CDATA[<p>Ich würde wohl ersteres verwenden (aber bitte keine leere return-Anweisung in einer long-Funktion ;)). Aber ich fürchte, das hier könnte zu einem Galubenskrieg ausarten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369427</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 20 Sep 2007 14:44:48 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 14:44:49 GMT]]></title><description><![CDATA[<p>Wie sagt man so schön &quot;halt den Ball flach&quot;. Dasselbe gilt auch für Funktionen, daher präferiere ich ebenfalls die erste Methode. Dank RAII hat man auch keine Probleme mit dem Aufräumen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369428</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369428</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 20 Sep 2007 14:44:49 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 14:49:59 GMT]]></title><description><![CDATA[<p>Ich würde die erste Methode bevorzugen (wenn das retun statement noch korrekt wäre :p). Aus den schon genannten Gründen: Man sieht sofort, was passiert und die Einrückungsebene wird nicht so tief.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369433</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369433</guid><dc:creator><![CDATA[rüdiger]]></dc:creator><pubDate>Thu, 20 Sep 2007 14:49:59 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 14:50:48 GMT]]></title><description><![CDATA[<p>sorry fuer das leere return. compiler haette es sowieso nicht gefressen .. es war natuerlich ...</p>
<pre><code>return 0
</code></pre>
<p>... gemeint<br />
ich ziehe auch erstere variante vor. aber bei mir am arbeitsplatz gibt es ein paar herren, die das fruehe ermitteln von ausstiegskriterien und dann ein unmittelbares return nicht so schoen finden.<br />
ich glaubte mich irgendwo zu erinnern, dass ich sowas mal gelesen habe dass variante 1. besser ist.<br />
Wer einen hinweis hierauf (auf obige problematik des stils) weiss, bitte link oder url schicken.</p>
<p>vielen dank.<br />
gruss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369434</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369434</guid><dc:creator><![CDATA[tuktuk]]></dc:creator><pubDate>Thu, 20 Sep 2007 14:50:48 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 17:23:30 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Aber ich fürchte, das hier könnte zu einem Galubenskrieg ausarten.</p>
</blockquote>
<p>In der Tat, wobei ich denke die <strong>Notwendigkeit</strong> für Methode 2 ist dank RAII nicht mehr gegeben. Notorische goto-Meider hatten vorher quasi keine andere Wahl (mal abgesehen von der Aufspaltung in Mikrofunktionen) <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<pre><code class="language-cpp">int ret = OK;
FILE* f = fopen(...);
if (f) {
    Struk* s = read_and_create(f);
    if (s) {
        ....
        free(s); // &lt;-- (2)
    } else
        ret = FEHLER;
    fclose(f); // &lt;-- (1)
} else
    ret = FEHLER;

return ret;
</code></pre>
<p>vs.</p>
<pre><code class="language-cpp">fstream f(...);
if (!f) return FEHLER;

auto_ptr&lt; Struk &gt; s( new Struk );
if (!s-&gt;read(f)) return FEHLER;

return OK;
// &lt;-- (1) und (2), falls jemals erzeugt
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1369435</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369435</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 20 Sep 2007 17:23:30 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 15:25:48 GMT]]></title><description><![CDATA[<p>ersteres ist besserer stil, da es den code leichter verständlich macht.</p>
<p>sobald man irgendwo, an irgendeiner stelle, ein return sieht, dann kann man sich 100% sicher sein, dass die aktuelle methode mit dem wert verlassen wird, der an der stelle steht.</p>
<p>sonst man sich erstmal durch einen wust an if/else oder womöglich noch diverse gotos quälen muss, bis man feststellt, was der ganze krempel soll.</p>
<p>man sollte als faustformel weiterhin im kopf behalten: mehr als drei tabs einrückungstiefe sind meist schon zuviel spaghettierung und ab fünf tabs sollte man den code einstampfen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369454</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369454</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Thu, 20 Sep 2007 15:25:48 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 15:42:35 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10648">@LordJaxom</a>: Beim ersten Codebeispiel wird die Datei gar nicht geschlossen, wenn read_and_create() einen ungueltigen Pointer zurueckliefert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369461</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369461</guid><dc:creator><![CDATA[DEvent]]></dc:creator><pubDate>Thu, 20 Sep 2007 15:42:35 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 17:23:52 GMT]]></title><description><![CDATA[<p>DEvent schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10648">@LordJaxom</a>: Beim ersten Codebeispiel wird die Datei gar nicht geschlossen, wenn read_and_create() einen ungueltigen Pointer zurueckliefert.</p>
</blockquote>
<p>Hast recht, ich arbeite schon zu lange nicht mehr mit C. Da könnte man dann einer Returnvariablen in den else-Zweigen Fehlercodes zuweisen und das Return als letzte Anweisung plazieren. Aber das Beispiel ändere ich jetzt nicht mehr ab, ich denke der Sinn ist klar <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>EDIT: Doch noch geändert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369555</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369555</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 20 Sep 2007 17:23:52 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 17:57:53 GMT]]></title><description><![CDATA[<p>weiss keiner hier ein nachschlagewerk (url oder link) in welchem programmierstil mit obiger problematik besprochen und dargestellt wird!?!?</p>
<p>vielen dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369580</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369580</guid><dc:creator><![CDATA[tuktuk]]></dc:creator><pubDate>Thu, 20 Sep 2007 17:57:53 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Thu, 20 Sep 2007 21:52:52 GMT]]></title><description><![CDATA[<p>Entweder die 1. Variante, oder so:</p>
<pre><code class="language-cpp">long foo(long pSomeLong)
{
    return Bedingung(pSomeLong) ? 0 : foo2(pSomeLong);
}

long foo2(long pSomeLong) 
{
    // ggf. das assert weglassen, kommt drauf an was der Code eigentlich macht, und ob
    // &quot;!Bedingung(pSomeLong)&quot; eine &quot;precondition&quot; für den folgenden Code ist
    assert(!Bedingung(pSomeLong));

    DoBerechnung(pSomeLong);
    DoThis();
    DoThat();
    MaybeThisToo();
    return CalcResult(pSomeLong);
}
</code></pre>
<p>EDIT: BTW: diese prefixes (&quot;p&quot;) sind ganz ekelig, und &quot;p&quot; verwendet man normalerweise für Zeiger, und nicht für Parameter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369758</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 20 Sep 2007 21:52:52 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Fri, 21 Sep 2007 05:36:10 GMT]]></title><description><![CDATA[<p>würd dann aber nen aussagekräftigeren bezeichner wählen. fooImpl oder so</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369817</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369817</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Fri, 21 Sep 2007 05:36:10 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Fri, 21 Sep 2007 05:38:27 GMT]]></title><description><![CDATA[<p>thordk schrieb:</p>
<blockquote>
<p>würd dann aber nen aussagekräftigeren bezeichner wählen. <strong>fooImpl</strong> oder so</p>
</blockquote>
<p>ja, sehr aussagekräftig <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369820</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369820</guid><dc:creator><![CDATA[Apeman]]></dc:creator><pubDate>Fri, 21 Sep 2007 05:38:27 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Fri, 21 Sep 2007 05:41:29 GMT]]></title><description><![CDATA[<p>na besser als foo2 <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<pre><code class="language-cpp">long foo(long someLong)
{
  if(Bedingung(someLong))
   return fooImpl(someLong);
  return 0;
}

long fooImpl(long someLong)
{
  ...
}
</code></pre>
<p>abgesehen vom bezeichner hat diese methode den vorteil, dass man eine funktion schreiben kann, die garantiert mit einem &quot;korrekten&quot; wert aufgerufen wird. macht das ganze übersichtlicher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369821</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Fri, 21 Sep 2007 05:41:29 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Fri, 21 Sep 2007 07:02:53 GMT]]></title><description><![CDATA[<p>Ich habe letztens irgendwo (= vermutlich in einem Python-Buch/Internetseite/oder Forum) gelesen: &quot;Exit as soon as possible&quot;</p>
<p>Alleine vom Codeverständnis ist es <em>mir</em> angenehmer eine Funktion (oder einen Block) dann zu verlassen, wenn es möglich/nötig ist.</p>
<p>Vom Laufzeitverhalten dürften sich beide Varianten nicht viel tun (wenn ich mal so an meine &quot;Assembler-Zeit&quot; zurückdenke). Um ein CMP und ein JE, JNE (x86) etc. kommt man ja ohnehin nicht herum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369857</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369857</guid><dc:creator><![CDATA[bwbg]]></dc:creator><pubDate>Fri, 21 Sep 2007 07:02:53 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Fri, 21 Sep 2007 09:56:50 GMT]]></title><description><![CDATA[<p>Die 2. Variante kommt von Sprachen wie C. Wenn man nur einen Eintritt- und einen Austrittspunkt hat, hat man genau definierte Punkte an denen man Ressource de/allokieren kann. In C++ ist das ganze wegen Exceptions hinfällig. Jede Codezeile ist jetzt ein möglicher Austrittspunkt und damit die Objekte per RAII zu schützen. Da das Konzept also nicht paßt und die erste Variante übersichtlicher ist nehme ich diese.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369973</guid><dc:creator><![CDATA[Tyrdal]]></dc:creator><pubDate>Fri, 21 Sep 2007 09:56:50 GMT</pubDate></item><item><title><![CDATA[Reply to was ist besserer Stil !?!? on Fri, 21 Sep 2007 10:25:38 GMT]]></title><description><![CDATA[<p>Hm. Auf der einen Seite find ich persoenlich die erste Variante uebersichtlicher -wenn nix zu tun ist gleich return. Andererseits hab ich vor kurzer Zeit irgendwo eine Richtlinie a la &quot;avoid multiple return statements&quot; gesehen, ausgegeben von Herb Sutter. Da wuerde das so aussehen:</p>
<pre><code class="language-cpp">long foo(long pSomeLong)
{
  long ret = 0;
  if(!Bedingung( pSomeLong ) )
  {
   DoBerechnung( pSomeLong );
   DoThis();
   DoThat();
   MaybeThisToo();
   ret = CalcResult( pSomeLong );
  }

  return ret;
}
</code></pre>
<p>/edit: hab mich halbwegs vertan, er hat diese Regel zwar mal aufgestellt, sie aber wohl wieder verworfen: <a href="http://www.gotw.ca/gotw/002.htm" rel="nofollow">http://www.gotw.ca/gotw/002.htm</a> Punkt 5.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1369992</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1369992</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 21 Sep 2007 10:25:38 GMT</pubDate></item></channel></rss>