• Hey Guest. Check out your NeoGAF Wrapped 2025 results here!

GAF Devs Thread - a thread about programmer's rants

Status
Not open for further replies.
mugurumakensei said:
I think this is far more fascinating. It shows a lot of the pitfalls that developers unintentionally fall into when working with languages like C/C++.

I see what you mean. It's funny how tricky these things can get (I had to look at your changes for 3 minutes by eye and the output to even follow the changes you had made).

At the end of the day, you get a lot of programmers who throw C or C++ down on their resume as a known language; when, sometimes, they only know maybe Java and C# and figure "Oh, what the hell, C/C++ can't be THAT different from what I know." I would call programs like the original I posted and your version to be, sort of, a litmus test of whether or not these people really know what they claim they know.

At the end of the day, people are probably more interested in asking about linked lists, or avl trees, whatever -- but it's stuff like these smaller programs where the heavy lifting appears in one line that really tests comprehension. :D
 
Spoo said:
I suppose if you're hiring for C or C++ programmers, the following could be a fun question to ask. I read about it in Joel Spolsky's book and recognized it immediately since I've had more hands on time with C++ than anything else:

Code:
#include <iostream>

using namespace std;

int main()
{
	char text1[] = "hello";
	char text2[] = "jimmy";
	
	cout << "Before: \n" << text1 << '\n' << text2 << endl;
	
	char* p = text1;
	char* q = text2;
	
	// asking why and how this line works!
	while (*q++ = *p++);
	
	cout << "\nAfter: \n" << text1 << '\n' << text2 << endl;
	
	cin.get();
	return 0;
}

I realize that copying like this is actually kind of archaic-looking, but you have to know your stuff to follow the logic of that one line :\

ok im curious. what does it do? i know in c that would just compare the words minus X starting letters but i feel im missing something.

my guess:

loop 1. while hello = jimmy
2. while ello = immy
3. while llo = mmy

ya?
 
^ notice that's an assignment operator.

Edit: It also refers back to the question I posted earlier about what's the difference between ++i and i++ :)
 
-COOLIO- said:
ok im curious. what does it do? i know in c that would just compare the words minus X starting letters but i feel im missing something.

My advice is to never ask what it does. Take as long as you need to figure it out.

If you really need to, compile, run, and step through the code line-by-line.
 
Zoe said:
^ notice that's an assignment operator.

Hah I missed that the first time. Dunno how, in JavaScript we have to use three equal signs in a row to really get what we want.
 
Half-and-half said:
My advice is to never ask what it does. Take as long as you need to figure it out.

If you really need to, compile, run, and step through the code line-by-line.
my life is too fast, too furious.
 
Half-and-half said:
My advice is to never ask what it does. Take as long as you need to figure it out.

If you really need to, compile, run, and step through the code line-by-line.

I have barely any experience with C but I'm assuming it will assign both arrays to be hello in the end. I'm guessing the null terminator would make it evaluate to false in order to stop the loop.
 
Half-and-half said:
This is something I've been dealing with a lot lately:

Given this struct in C++ (assume Win32 and no #pragma packing):

What does sizeof( randomStruct ) return? And the follow-up, optimize the structure for size.

First, it's 24.

Simple. Group the short and the two bools together. On a 32-bit(4 byte) machine, this puts the some of the size of those fields on a word boundary (2 + 1 + 1). After that, the position of the 64-bit integer and 32 bit integer do not matter.

Code:
struct randomStruct
{
	int a;
	_int64 d;
	short c;
	bool b;
	bool e;
};

struct randomStruct2
{
	short c;
	bool b;
	bool e;
	int a;
	_int64 d;
};

struct randomStruct3
{
	_int64 d;
	short c;
	bool b;
	bool e;
	int a;
};

sizeof(randomStruct) returns the same as sizeof(randomStruct2) which is the same as sizeof(randomStruct3).

Oddly enough, structure size issues were a major pain when my company went from SCO to Linux. By default, the xenix cross compiler and the udk compiler both assume the structures are packed. With Linux and gcc, structures are not packed by default. You must add the packed attribute after the structure definition.
 
Wormdundee said:
I have barely any experience with C but I'm assuming it will assign both arrays to be hello in the end. I'm guessing the null terminator would make it evaluate to false in order to stop the loop.

that is correct. Funny enough, I think the real trick is that the null terminator acts as the truth value and not the assignment/incrementing of the pointer data/pointers. Though a lot of people do tend to screw up pointers.
 
mugurumakensei said:
Oddly enough, structure size issues were a major pain when my company went from SCO to Linux. By default, the xenix cross compiler and the udk compiler both assume the structures are packed. With Linux and gcc, structures are not packed by default. You must add the packed attribute after the structure definition.

In our case, we have limited memory and lot's of little objects during runtime. I found that rearranging a particular struct saved several hundred kilobytes. Also, it had the added benefit of improving our cache usage.
 
Spoo said:
I suppose if you're hiring for C or C++ programmers, the following could be a fun question to ask. I read about it in Joel Spolsky's book and recognized it immediately since I've had more hands on time with C++ than anything else:

Code:
#include <iostream>

using namespace std;

int main()
{
	char text1[] = "hello";
	char text2[] = "jimmy";
	
	cout << "Before: \n" << text1 << '\n' << text2 << endl;
	
	char* p = text1;
	char* q = text2;
	
	// asking why and how this line works!
	while (*q++ = *p++);
	
	cout << "\nAfter: \n" << text1 << '\n' << text2 << endl;
	
	cin.get();
	return 0;
}

I realize that copying like this is actually kind of archaic-looking, but you have to know your stuff to follow the logic of that one line :\
Hm? I'm only three months into C++ and this one doesn't seem that difficult. Its just demonstrating basic pointer functionality with arrays.
The while loop makes sense in what it does, copying the contents of text 1 to text 2, the only thing I don't get is what terminates it. Is there some automatic termination criteria I'm missing/don't know yet?
 
e: misread, editing to make sense
mugurumakensei said:
Oddly enough, structure size issues were a major pain when my company went from SCO to Linux. By default, the xenix cross compiler and the udk compiler both assume the structures are packed. With Linux and gcc, structures are not packed by default. You must add the packed attribute after the structure definition.
Right, gcc wants to align stuff on nice sizeof(long) boundaries for performance reasons.

malloc (and thus, stl_map) will get you the same way.
 
The_Technomancer said:
Hm? I'm only three months into C++ and this one doesn't seem that difficult. Its just demonstrating basic pointer functionality with arrays.
The while loop makes sense in what it does, copying the contents of p to q, the only thing I don't get is what terminates it. Is there some automatic termination criteria I'm missing/don't know yet?

String literals are null terminated in C.

When the null terminator occurs, *p = *q = '\0' = 0 = false.
 
mugurumakensei said:
String literals are null terminated in C.

When the null terminator occurs, *p = *q = '\0' = 0 = false.
Yet another shortcut to make note of! Before today I would have appended some additional counter to that loop based on the size of the largest input array.
 
The_Technomancer said:
Yet another shortcut to make note of! Before today I would have appended some additional counter to that loop based on the size of the largest input array.

Oh, I wouldn't ever use that kind of copying. It assumes certain things about the input that may or may not be true. It's how strcpy does things, and it's very unsafe. It's neat, but you'll fall into really nasty habits that cause buffer overflows.

With that being said, knowing such things is useful for testing if a string is empty or not.

Code:
int main(int argc, char **argv)
{
    char p[30];
    char q[30];

   prompt();

   getinput(p, 30);

   while(!*p && valid_input(p))
   {
      notify_badinput();
      prompt();
      getinput(p, 30);
   }

   strncpy(q, p, 30);

   operate_on_q(q);
}

Quick test to see if p is empty or not... if it is not null, test if it's a valid input. If one of these is false, notify that the input received was bad. Prompt again. Try to get input again. At the end, copy the input to q and operate on q.
 
nickcv said:
tomorrow i'll be interviewing a couple of people to fill a programmer position in my company.

Do you guys know any tricky question i could ask? (besides the fizzbuzz test i mean)
an evil suggestion is much appreciated :P
See here too:
http://www.aaronsw.com/weblog/hiring

There are three questions you have when you're hiring a programmer (or anyone, for that matter): Are they smart? Can they get stuff done? Can you work with them? [...]

The traditional programmer hiring process consists of: a) reading a resume, b) asking some hard questions on the phone, and c) giving them a programming problem in person. I think this is a terrible system for hiring people. You learn very little from a resume and people get real nervous when you ask them tough questions in an interview. Programming isn't typically a job done under pressure, so seeing how people perform when nervous is pretty useless. And the interview questions usually asked seem chosen just to be cruel. I think I'm a pretty good programmer, but I've never passed one of these interviews and I doubt I ever could.

(see also Spolsky: http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html )
 
I think the snippet I posted has its bread and butter in why the statement terminates; it's quite subtle.

I agree with an above poster who says it makes a lot of assumptions about the data, and is extremely unsafe. It's only useful as a 3 in 1 test (pointer, increment, assignment).

Also, ronito, I'm not sure if you're reffering to the code being poorly written, or Spolsky's stuff?
 
The thing that drives me crazy about Spolsky, is that he makes demands of programmers -- learning programmers, anyway -- that if they don't understand archaic lines like the one I posted, they have no business in programming at all. I think that kind of attitude is bullshit; I know many programmers who couldn't make heads or tails of that while statement but are quite good at what they do.

His books are just, really, "ok" -- there's good advice in there, it's just mixed with a lot of untruths.
 
The hiring process for the job I just started was a remote pairing session where I talked someone through implementing a set (I did zero programming myself) in a language they don't use. Then they flew me down and I paired on two real projects doing real work for a day. No sit down and grill me with questions for an hour, simply see how I work. The two people I paired with ultimately determined I was a fit.

I also interviewed with Amazon, and that was more standard phone interview about sorting algorithms and OO questions. Their turn around on interviewing was too slow, that I had done all the above, and been offered a job, in the time between my first and second interview with Amazon.
 
The_Technomancer said:
Alright, so the basic three modulus check version of Fizzbuzz that iapetus posted looks perfectly straightforward. How would you optimize it to less code?

Code:
#include <iostream>

const char* fizzBuzzOutput =
"1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz"
"\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\n"
"FizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n"
"43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56"
"\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\n"
"Buzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83"
"\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz"
"\n97\n98\nFizz\nBuzz";

int main(int argc, const char** argv)
{
	std::cout << fizzBuzzOutput << std::endl;
	return 0;
}

Edit: Made the code block a little shorter at the expense of clarity.
 
Slavik81 said:
Code:
#include <iostream>

const char* fizzBuzzOutput =
"1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz"
"\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\n"
"FizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n"
"43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56"
"\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\n"
"Buzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83"
"\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz"
"\n97\n98\nFizz\nBuzz";

int main(int argc, const char** argv)
{
	std::cout << fizzBuzzOutput << std::endl;
	return 0;
}

Edit: Made the code block a little shorter at the expense of clarity.

Ok. I lol'd -- goes to show that programs like FizzBuzz are maybe academically "smart" but realistically stupid. It does outline an important concept though -- but hats off to Slavik; that is the most optimized fizzbuzz code ever.
 
I have a 2nd round interview on Thursday; I've already done a phone interview where I was asked questions about the projects I listed on my resume and they got a general idea of my level of knowledge.

I'm not really sure how to prepare for the interview. I guess I could brush up on some of the basic stuff? It's for a startup and they use a variety of languages and technologies.
 
Slavik81 said:
Code:
#include <iostream>

const char* fizzBuzzOutput =
"1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz"
"\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\n"
"FizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n"
"43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56"
"\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\n"
"Buzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83"
"\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz"
"\n97\n98\nFizz\nBuzz";

int main(int argc, const char** argv)
{
	std::cout << fizzBuzzOutput << std::endl;
	return 0;
}

Edit: Made the code block a little shorter at the expense of clarity.
Now, what would you do on a system that has no cache and registers only 4 byte in size ?
 
Slavik81 said:
Code:
#include <iostream>

const char* fizzBuzzOutput =
"1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz"
"\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\n"
"FizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n"
"43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56"
"\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\n"
"Buzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83"
"\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz"
"\n97\n98\nFizz\nBuzz";

int main(int argc, const char** argv)
{
	std::cout << fizzBuzzOutput << std::endl;
	return 0;
}

Edit: Made the code block a little shorter at the expense of clarity.
haha awesome.
 
My boss wants me to look into using WinDev Mobile to create a portable version of our app. Has anyone used or heard of this before? It claims to be able to make building mobile apps easy, but I'm naturally skeptical.

Looking at the Express version, my initial impressions are negative. Their W-language reminds me of VB and the IDE seems cluttered and very ehh, but I've worked with worse. However, it seems I cannot create an Android project to mess around with in the Express version, which is a huge downside when trying it out, and I could only run one test application before the Express version locked doing that out. Is this supposed to make me want to use this?
 
Drkirby said:
Now, what would you do on a system that has no cache and registers only 4 byte in size ?
A simple, but extraordinarily optimized algorithm on a weird target platform architecture?

Assembly.
 
The_Technomancer said:
Yet another shortcut to make note of! Before today I would have appended some additional counter to that loop based on the size of the largest input array.
Not that I know much about C++, but shortcuts like that would be frowned on in most places.

Maybe it had a place in C++ if it actually runs faster, but if not, I'd simply consider it poor coding practice.

So, in C++, I take it that assignments return Boolean values? That doesn't make much sense. :/
 
TheExodu5 said:
So, in C++, I take it that assignments return Boolean values? That doesn't make much sense. :/

Pretty sure the assignment operator is performed and then the conditional statement simply evaluates the left-hand variable by itself. So the assignment isn't "returning" anything, it's simply doing what it's supposed to do and the conditional statement just does its own thing afterwards.
 
TheExodu5 said:
Not that I know much about C++, but shortcuts like that would be frowned on in most places.

Maybe it had a place in C++ if it actually runs faster, but if not, I'd simply consider it poor coding practice.

So, in C++, I take it that assignments return Boolean values? That doesn't make much sense. :/
No, it returns the value that was assigned. In this case, the null terminator is going to eventually be assigned, and when null is returned to the loop logic, it fails and the loop ends. You are right, though, this should not be done in production code unless speed is of the utmost importance, as it is nearly illegible.
 
Oh, so when while () is given a random integer (address space in this case), it just evaluates it as true?

If you can put an integer as a while parameter, what else can you put there? It seems odd to allow anything other than a boolean. I'm guessing while was made to work with 1 (true) and 0 (false), and this is just unintended functionality?
 
Zoe said:
Non-zero integers evaluate as true.

Gotcha.

I need to mess around with C++ a bit to get the hang of it. I was never a fan of pointers when I used C in microcontroller programming.

Is there a half decent IDE out there that might be comparable to Eclipse? The one we used didn't even have real-time syntax checking. We would compile code for 5 minutes to put onto the microcontroller, only to find out we made a syntax error somewhere. Drove me insane.
 
TheExodu5 said:
Gotcha.

I need to mess around with C++ a bit to get the hang of it. I was never a fan of pointers when I used C in microcontroller programming.

Is there a half decent IDE out there that might be comparable to Eclipse? The one we used didn't even have real-time syntax checking. We would compile code for 5 minutes to put onto the microcontroller, only to find out we made a syntax error somewhere. Drove me insane.
Visual C++ Express should be free for home usage.
 
TheExodu5 said:
Gotcha.

I need to mess around with C++ a bit to get the hang of it. I was never a fan of pointers when I used C in microcontroller programming.

Is there a half decent IDE out there that might be comparable to Eclipse? The one we used didn't even have real-time syntax checking. We would compile code for 5 minutes to put onto the microcontroller, only to find out we made a syntax error somewhere. Drove me insane.
Eclipse offers C++ extensions: http://www.eclipse.org/cdt/

Also, as mentioned, Visual C++ Express is also free.
 
What do you guys think of leaving old code lying around but commented out. I don't get it.

Just now I ran across about 40 lines commented out with a comment at the top saying something like "This code is no longer necessary <brief explanation why>". It's like ok, why didn't you just delete it? Especially since we have version control if you find you need it again you can just go back and get it.
 
Wormdundee said:
What do you guys think of leaving old code lying around but commented out. I don't get it.

Just now I ran across about 40 lines commented out with a comment at the top saying something like "This code is no longer necessary <brief explanation why>". It's like ok, why didn't you just delete it? Especially since we have version control if you find you need it again you can just go back and get it.

Agreed.
 
Wormdundee said:
What do you guys think of leaving old code lying around but commented out. I don't get it.

Just now I ran across about 40 lines commented out with a comment at the top saying something like "This code is no longer necessary <brief explanation why>". It's like ok, why didn't you just delete it? Especially since we have version control if you find you need it again you can just go back and get it.

I'm facing this exact problem right now, a need to use a class that has hundreds of lines commented. It's really difficult for most coders, myself included, to let go code done previously, so when is time to eliminate it some people prefer to just comment it.
 
More rantish stuff. Why the hell do people return out of void functions?!? It doesn't make any sense. In general I don't like a return statement anywhere except at the end of the function. Each return statement makes it more difficult to read and understand what the actual result is going to be and I hate it. Haaaaate it. It's especially nonsensical in void functions though.

Basically what's happening in many places in our code is people doing if checks on some sort of flag and then returning early out of the function if the flag is 'wrong'. What I prefer is a check to see if the flag is 'right'.

So instead of

Code:
public void someFunction()
{
   if(!thingIsEnabled)
      return;

   doSomeStuff;
}

I prefer to have it this way

Code:
public void someFunction()
{
   if(thingIsEnabled)
   {
      doSomeStuff;
   }
}

This way you can naturally see what's driving the flow and it's much easier to tell when something actually executes, especially when you get multiple flag checks. And there are no stupid return statements.
 
Sometimes it's more logical to stop the execution of a function at a certain point.
 
Wormdundee said:
More rantish stuff. Why the hell do people return out of void functions?!? It doesn't make any sense. In general I don't like a return statement anywhere except at the end of the function. Each return statement makes it more difficult to read and understand what the actual result is going to be and I hate it. Haaaaate it. It's especially nonsensical in void functions though.

Basically what's happening in many places in our code is people doing if checks on some sort of flag and then returning early out of the function if the flag is 'wrong'. What I prefer is a check to see if the flag is 'right'.

So instead of

Code:
public void someFunction()
{
   if(!thingIsEnabled)
      return;

   doSomeStuff;
}

I prefer to have it this way

Code:
public void someFunction()
{
   if(thingIsEnabled)
   {
      doSomeStuff;
   }
}

This way you can naturally see what's driving the flow and it's much easier to tell when something actually executes, especially when you get multiple flag checks. And there are no stupid return statements.

Most programmers I have run into prefer it your way, but it doesn't particularly annoy me when I see it. The same kind of situation occurs when, say, you which to break out of a loop. Most people want a natural break, without any keywords that disrupt flow.

Another thing people frown upon is something like the following:

Code:
void someFunction(...)
{
     if (someCondition)
          doSomething();
     else
          doSomethingElse();
}

vs:


Code:
void someFunction(...)
{
     if (someCondition)
     { 
          doSomething();
     }
     else
     {
          doSomethingElse();
     }
}

I prefer the former, as it is more compact, but believe it or not I had a friend fail an interview because he was told he was "missing brackets" -- when it's not really needed for a one line statement.

That said, it's just me, but if I need to revisit some block and use brackets I don't sweat it if I have to insert them. Just a choice between practices.
 
I don't mind the brackets thing as long as it's consistent. If I have a 2 line if followed by a 1 line if I'll use brackets on both of them.
 
Spoo: When some genius decides that doSomething(); is better implemented as a two-statement macro like this:

#define doSomething foo(); bar

then braces become important. Admittedly, not so much in your case where there's an else clause, because you'll at least get a compile error; but old hands who have been bitten by this shit before are rightfully defensive about it.
 
Wormdundee said:
This way you can naturally see what's driving the flow and it's much easier to tell when something actually executes, especially when you get multiple flag checks. And there are no stupid return statements.

I find returning early sometimes just makes it more readable, and other times it just makes more sense. For example, If I'm doing an equals() check, I'll return false if the passed parameter is null, thus avoiding a bunch of null checks down the line.

If all the return clauses are right at the start of the method, it's easy to understand. It's actually easier to see that "oh, it does nothing in these cases" than having to scroll down to see just how far that encompassing if statement goes.
 
Wormdundee said:
What do you guys think of leaving old code lying around but commented out. I don't get it.

Just now I ran across about 40 lines commented out with a comment at the top saying something like "This code is no longer necessary <brief explanation why>". It's like ok, why didn't you just delete it? Especially since we have version control if you find you need it again you can just go back and get it.
Its pointless because you should be using source control. If you need to look at the old code, just sync to it.
 
Spoo said:
Most programmers I have run into prefer it your way, but it doesn't particularly annoy me when I see it. The same kind of situation occurs when, say, you which to break out of a loop. Most people want a natural break, without any keywords that disrupt flow.

Another thing people frown upon is something like the following:

Code:
void someFunction(...)
{
     if (someCondition)
          doSomething();
     else
          doSomethingElse();
}

vs:


Code:
void someFunction(...)
{
     if (someCondition)
     { 
          doSomething();
     }
     else
     {
          doSomethingElse();
     }
}

I prefer the former, as it is more compact, but believe it or not I had a friend fail an interview because he was told he was "missing brackets" -- when it's not really needed for a one line statement.

That said, it's just me, but if I need to revisit some block and use brackets I don't sweat it if I have to insert them. Just a choice between practices.

I tend to do the following on such thing
Code:
if (someCondition) doSomething();

Though I won't do it if there is any else branches to the if statment, but if it is just a one off if statement (if (hour == 12) hour = 0;), I'll leave it on one line.


On a tangent, any kinds of problems with a function like this?

Code:
int ConvertTime(char *Time, char *Midday)
{
	//Converts a field in the format of ##:## PM into the time in minutes
	int time;

	time = atoi(strtok(Time,": \n"));

	if(time == 12) time = 0;
	time *= 60;

	time += atoi(strtok(NULL,": \n"));
	if (strcmp("PM",Midday) == 0) time += 720;
	return time;
}
 
Drkirby said:
On a tangent, any kinds of problems with a function like this?

"You should never, ever, use strtok() in any serious application"

ConvertTime() can potentially crash if you call it on a string literal, depending on your architecture

ConvertTime() will modify the contents of Time

ConvertTime() is not thread safe due to use of strtok()

edit: Sorry if this was not what you were actually asking about. But yeah. It is 2011 and nobody should ever use strtok() ever again.
 
Status
Not open for further replies.
Top Bottom