Good programming practice question

  • Thread starter Thread starter alex
  • Start date Start date
A

alex

Hi,

I was just wandering which one is correct, better, or better programming
practice?

//Case #1
public class Main
{
ClassA a = new ClassA();
a.Value = "some value";
a = ModifyClassA(a);
Console.WriteLine(a.Value);
}
private ClassA ModifyClassA(ClassA a)
{
a.Value += "something";
return a;
}

Or this:

//Case #2
public class Main
{
ClassA a = new ClassA();
a.Value = "some value";
ModifyClassA(a);
Console.WriteLine(a.Value);
}
private void ModifyClassA(ClassA a)
{
a.Value += "something";
}

//Where CassA is:

public class ClassA
{
string m_value;
public ClassA(){ m_value = "";}
public string Value
{
get{return m_value;}
set{m_value = value;}
}
}

I'm writing this on the fly so sorry if it doesn't compile.
I would appriciage your oppinion.

Thanks,
Alex
 
This is subjective, but your Case #2 example seems to be
more clear. If I was going to write it, I'd write
something like #2.
 
What colour of panties should I wear.

Whats everybody else wearing, I wana copy.

*bleet**bleet**bleet**bleet*
 
Good code practise is quite a big issue when your working in a team, so its
not as if the question deserves that sort of response.
 
A third solution (to consolidate functionality) is this:

//Case #1
public class Main
{
ClassA a = new ClassA();
a.Value = "some value";
a.Modify();
Console.WriteLine(a.Value);
}

//Where ClassA is:

public class ClassA
{
string m_value;
public ClassA(){ m_value = "";}
public string Value
{
get{return m_value;}
set{m_value = value;}
}
public void Modify()
{
m_value += "something";
}
}
 
Kevin is right #2 is clearer - it avoids a completely non-functional
reference copy, as you have in Case #1. In fact Case #2 could be said to be
a refactor of Case #1 with the non-functional code removed. #1 implies you
are changing out the reference to a for some reason, when in fact you are
always re-assigning it to the same thing again - pointless, IMO.

Richard
 
Then discuss it with ur team.


Arran Pearce said:
Good code practise is quite a big issue when your working in a team, so its
not as if the question deserves that sort of response.
 
What is good for youre team may not work for our team. Its a subjective
matter.

If it where the case we wouldnt need software engineers, we would have that
ONE PERFECT APP, or one perect API call.

But we dont.
 
Richard, thanks for your post. I also think that case #2 is better because
there is no reassignment of a reference. I just wan't sure how intuitive it
would be for people who would later read the code.
 
I agree. But in my case I'm passing SqlParameter object to a function. I'm
too so sure if I want create my own derivative of SqlParameter class.

Thanks,
Alex
 
Thanks for your post Kevin. I agree.

Kevin Carter said:
This is subjective, but your Case #2 example seems to be
more clear. If I was going to write it, I'd write
something like #2.
 
Back
Top