Note that there are some explanatory texts on larger screens.

plurals
  1. POClass Methods: Passing private data fields in private methods
    primarykey
    data
    text
    <p>I was wondering if which of the following patterns are considered to be more "correct".</p> <p>The first example sets the value of private data member length by calling a void member function that implicitly takes arguments. The second example sets the value of length by assigning it to the return value of the member function that explicitly takes an argument.</p> <p>It seems that the second method makes the code clearer, since you know when and how the private member is getting set, where the first requires a trace of the code to verify what and how a value is being assigned. The second method also seems like it would allow for better reuse down the road because it could operate in any class/context (since the arguments and return type are explicit).</p> <p>The first method is quicker, and if used throughout the entire class (for private member functions) can save some coding, however I'm not sure if this will bite me down the road ?</p> <p>Thank you for the insight and clearing this up for me.</p> <pre><code>class MyDataType { private int length; private string content; private char[] buffer; public MyDataType(str) { content = str; calculateLength(); buffer = new char[length+1]; for(int i=0; i &lt; length; i++) buffer[i] = content[i]; buffer[i] = NULL; } private void calculateLength() { int i = 0; while(content[i++] != NULL) {} // i know, buffer overflow... length = i; } } </code></pre> <hr> <pre><code>class MyDataType { private int length; private string content; private char[] buffer; public MyDataType(str) { content = str; length = calculateLength(content); buffer = new char[length+1]; for(int i=0; i &lt; length; i++) buffer[i] = content[i]; buffer[i] = NULL; } private int calculateLength(string s) { int i = 0; while(s[i++] != NULL) {} return i; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload