none
Ideas about using references and zero values innovatively. RRS feed

  • General discussion

  • I was hoping to get some of these ideas implemented in C# (perhaps in a special mode so as to not break existing code other people may have).

    All variables should have a value that is true and a value that is zero (in an if statement). Some variables should have a value that is false.  If a string has data in it, it is true.  If it is empty string or null, it is zero.  Logical variables should have 4 potential data values: true, false, zero, <msreadoutspan class="msreadout-line-highlight msreadout-inactive-highlight">and null.  If a logical variable is null, it is also zero, but if it is zero, it is not necessarily null.  I call them logical variables, because they are different than Boolean variables.  Numbers are true if they are positive, false if they are negative or zero if they are 0.  All other variables should be true if they have data in them or zero if they are null or do not have data in them.  This also extends to structures and objects which are true if they have any public data that is not zero (remember null is counted as zero) or zero if they don't.</msreadoutspan>

    Because all variables can be evaluated to logical values (true, false and zero), if statements could be made to work with any variables (not just Boolean).  This change would allow if statements to be much more compact and powerful.  For example:

    if (variable != null)

    <msreadoutspan class="msreadout-line-highlight msreadout-inactive-highlight">would become</msreadoutspan>

    if (variable)

    If statements operating on a logical variable (described earlier (not Boolean)) would execute the first branch if the if statement evaluated to true, otherwise (if the statement was false or zero) the else branch would be executed.  There also could be a special if statement that had a false and/or zero branch.

    Dereferencing a zero or null reference should not be an error or exception. If we are trying this on the left side of an assignment statement, it should simply do nothing.  If we are trying this on the right side of an assignment statement or outside of one, a zero value should be produced.  For example:

    string name = Employee.FirstName

    In the statement, we should get a zero value (empty string) for name if the Employee object is null. Making the changes described in the above paragraph allows some code to be more compact, to work better and to fail gracefully.  I realize that there is already an operator (the ? operator) that does something similar, but it doesn't work the way it should (it is only a partial solution)

    If we have an array and the array index is out of bounds, we should do nothing if we are attempting to assign a value to that index and if we are trying to get the value of that index we should always get a zero. Any time there is a reference to memory such as a pointer, there should also be a corresponding window into memory that the reference can see (although the window may be null).  Any attempt to dereference the reference should be treated like the array whose index is out of bounds if the reference is outside of the window.  This would solve many problems such as buffer or stack overflows.

    All variables that are not specifically set to something else at the time of creation should be set to a zero and/or null value.  This would simplify the code and prevent the debugger from showing bogus values when we watch a variable before it is initialized.  I realize that there might be a very slight performance hit, but the benefits would be well worth it.

    It would be nice if arrays and other indexed structures could be used in such a way as to be an index rather than an offset.  An offset starts at zero and counts up from there, while an index should be initialized to zero and start counting at 1 and count up from there.  Having indexes that start counting elements in the array from zero means that we use a negative number when zero should be used.  I realize that some may say the ship has sailed with this one, but maybe there is a way for the compiler to remedy this situation in a special mode.  It may seem insignificant, but I think it is important in using zero correctly.

    In summary, I think that making these changes available in the C# language would greatly improve the way the language works and eliminate much boiler-plate code, needless if statements, and unnecessary exceptions. I welcome any feedback anyone might have.  Also, I think that this information could be used outside of C#, but I wanted to start here because that is the language I use most.

    Tuesday, April 17, 2018 12:18 PM

All replies

  • In programming it seems easy to change, but it is not. A change makes a chance on a breaking change. Which means that previous code does not run anymore but gives wrong results. 

    However, moreover is what you describe is a complete different language then C#. Like telling that in the English language should be used in future Chinese characters.

    It seems you want to make things for you easier. But strange enough, then your offset starts at zero. In your kind of program languages it starts like all human counting at the first (1th) not at 0th. 

    What your proposal does is showing ideas from somebody who thinks C# is the first and only program language on earth and start introducing very vintage idea's.

    Your text is what you can call a proof that "new" does not always mean better (although it are for many more nostalgic already often thrown away ideas which are proven not to work) . 


    Success
    Cor






    Tuesday, April 17, 2018 1:07 PM
  • C++ does the auto conversion the way you describe in your initial idea and it causes more problems than it solves. Here's the typical reason why I would be against such a change.

    if (x = 10)
    {
    }
    

    See the problem here? x = 10 is an expression of type int that has a value of 10. That is true so the if statement executes. What I meant to say was x == 10 but the compiler is going to happily compile it anyway. One of the more common bugs in C++. However I could see the compiler allowing some options through (defaulting ref values for example).

    Nevertheless the forums isn't where language suggestions should go. Please post your language ideas in GitHub where the team can look and evaluate it.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 17, 2018 1:48 PM
    Moderator
  • You may be right about implementing the changes in a different programming language.  I think it would be worth it even if I have to do it myself. I proposed the article as changes to C# because it is my language of choice and I think a language just like it, but with the proposed changes would make it much better.  I really didn't realize that my ideas were vintage, they are completely revolutionary as far as I can tell (when taken as a whole).

    It may seem a small thing to start counting at 1 rather than zero, but does that mean when you initialize the index that it should be initialized to -1?  Variables should be initialized to zero which represents an absence of data.  If you initialize the index to zero and it points to the first element in the array it can work, but it is inconsistent with how zero works everywhere else.  If you are assigning an index and the value you are assigning it becomes zero, because lets say you were trying to dereference a null pointer to get the value you wanted to assign it to, it now points to the first element in the array instead of referencing no element.  Should we introduce special logic to make the index a -1 instead? In any case, this is the least important change that I proposed.

    Tuesday, April 17, 2018 2:02 PM