Re: Had an interview



Andrey Tarasevich wrote:
andrew.nesterov@xxxxxxxxxxxx wrote:
...

Implement a block that inputs either 1 or 2 and outputs the opposite:
1 -> 2, 2 -> 1
The author continue with three hypotetical solutions:
1. A programming class instructor:
if ( x == 1) x = 2;
if ( x == 2) x = 1;

I don't know why you call it "solution", since it's obviously incorrect.

2. A programmer:
if ( x == 1) x = 2;
else if ( x == 2) x = 1;

That would be a beginner/inexperienced programmer.

A "programmer" would most likely opt for the '?:' operator in this case.

3. A mathematitian:
x = 3 - x;

In reality that would the solution for a _good_ programmer.

I vote for:
if (1 == x) x = 2;
else x = 1;

which is clear, and has no undefined states. Self-healing code.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
.



Relevant Pages

  • Re: SafeArray with VB and C++, row major/column major order question
    ... SAFEARRAY ... data is always in the order that's opposite of what a C/C++ programmer ...
    (microsoft.public.vc.atl)
  • Re: C elements of style
    ... I am opposite here. ... meaning for both is obvious. ... An experienced C programmer will of course be familiar with both, ... best you can do is say that it would be pointless if it worked the ...
    (comp.lang.c)
  • Re: Vb.Net or C#.Net
    ... A die hard c# programmer will argue C# is better, ... opposite. ... > of develoing in C# because mostly companies require developing in C#.Net. ...
    (microsoft.public.dotnet.framework.aspnet)