Is there a better way to write this code?

This is my first topic ever on your forum:

I just finished paying someone do about 10% of my project. Before I get
to the other 90% want to make sure I have the right person for the job.
The amount of code he used for this simple task seems unreasonable.
Task is:
format of output file:
yyyy.mmdd.hhmm 999999.9999
// The second column has no leading 0's except for first 0 left of '.'
// and show '-' left of most significant digit if negative.
// Decimal point of double should be vertically aligned as in his
// example output shown below.
// Assume that the abs value never > than 999999
// Have 4-5 spaces between columns assuming could be -999999.9999
// Assume output values are valid -- No validation required!
// One Question: Do you think his code would produce a '-' if
// if double is negative?
Values used to construct line of output:
double tm2_accMove; // Use for second column
int outYr, outMo, outDa, outHr, outMin;

This is an example of two lines of output produced by his code:
2014.0115.1329 0.0163
2014.0315.1529 0.2160

This is his code he wrote to format my output file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
           if (ofp != NULL) {
	      if (outYr < 0)
	         outYr = abs(outYr);
	      fprintf(ofp, "%i", outYr);
	      fprintf(ofp, "%s", ".");
	      if (outMo  > 9)
	         fprintf(ofp, "%i", outMo);
	      else {
	         fprintf(ofp, "%i", 0);
	         fprintf(ofp, "%i", outMo);
              }
	      if (outDa > 9)
	         fprintf(ofp, "%i", outDa);
	      else  {
	         fprintf(ofp, "%i", 0);
	         fprintf(ofp, "%i", outDa);
	      }
	      fprintf(ofp, "%s", ".");
	      if (outHr > 9)
	        fprintf(ofp, "%i", outHr);
	      else {
	         fprintf(ofp, "%i", 0);
	         fprintf(ofp, "%i", outHr);
	      }
	      if (outMin > 9)
	         fprintf(ofp, "%i", outMin);
	      else {
	         fprintf(ofp, "%i", 0);
	         fprintf(ofp, "%i", outMin);
	      }
	      fprintf(ofp, "%s", "              ");
	      fileFormatOffset = 0;
	      tempLong2 = 0;
	      if (tm2_accMove < 10)
	      {
	         fileFormatOffset = 5;
	      }
	      else if (tm2_accMove < 100)
	      {
	         fileFormatOffset = 4;
	      }
	      else if (tm2_accMove < 1000)
	      {
	         fileFormatOffset = 3;
	      }
	      else if (tm2_accMove < 10000)
	      {
	         fileFormatOffset = 2;
	      }
	      else if (tm2_accMove < 100000)
	      {
	         fileFormatOffset = 1;
	      }
	      else
              {
	         fileFormatOffset = 0;
	      }
	      for (int j = 0; j < fileFormatOffset; j++)
		fprintf(ofp, "%s", " ");
	      tempLong2 = floor(tm2_accMove * 10000) / 10000;
	      fprintf(ofp, "%.4f", tempLong2);
	      fprintf(ofp, "%s\n", "");
	   }
You are right, this is unnecessarily long.

But the real question is, why aren't you doing this yourself? This smells like homework, and paying someone to do your homework is pretty ridiculous.


EDIT:

And, quite frankly, anyone willing to accept money to do homework assignments is probably the wrong person for the job.

Most competent programmers are either too principled to endorse cheating -- or are so into what they do they'd do it for free -- or are too busy to do something so trivial.


EDIT 2:

I was bored. This code does the same thing his does:

1
2
3
4
5
if(ofp)
{
    fprintf(ofp, "%04d.%02d%02d.%02d%02d     %7.4f\n",
        outYr, outMo, outDa, outHr, outMin, tm2_accMove );
}
Last edited on
I am 71 years old and so this is definitely not homework. Now that you mention it, I suppose you do get those kind people post and that could get very old very soon. No I actually paid someone for a small project and this is the code he produced and I just want to know if I should use him for my next project which is much bigger.

Also I am not in any kind business or have any employees of any kind.

If someone not willing to show me the code then I would be happy to just know the least number of lines of code this could have taken.
Last edited on
Now that you mention it, I suppose you do get those kind people post and that could get very old very soon.


Yeah, sorry for making that assumption. I just see a whole lot of homework stuff on here, it kind of drives me mad.

No hard feelings? =)


No I actually paid someone for a small project and this is the code he produced and I just want to know if I should use him for my next project which is much bigger.


Well it all depends on what your needs are. While this guy might not have done things the most efficient way, the real question is, did he fill the requirements of the job? And would he be able to fulfill the requirements of the next?

That being said, there are other things to consider. Is code maintenance an issue? Or is this a "write it once then forget about it" thing? Are there other people on the programming team? Etc, etc, etc.


Honestly if this guy can get the job done then that's all that matters and I wouldn't say it's a bad idea to hire him again. Unless he's part of a larger programming team -- or this code is going to have to be worked on at a future date... in which case I might have some reservations. The code you posted does not seem very maintainable.
Your right. He did get the job done.

Thanks so much for your insights and especially your sharing of this code.




Topic archived. No new replies allowed.