Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

10-28-2014 , 06:48 PM
omg, just wrote a huge long post about not getting why this bool kept always returning true, and then I happened to see that one of my if() statements was using the assign = operator instead of ==.

Why would that not throw a compiler error?

I demand digital blood!
Programming homework and newbie help thread Quote
10-28-2014 , 08:03 PM
Quote:
Originally Posted by Anais
omg, just wrote a huge long post about not getting why this bool kept always returning true, and then I happened to see that one of my if() statements was using the assign = operator instead of ==.

Why would that not throw a compiler error?

I demand digital blood!
Some compilers probably will for some languages.
Programming homework and newbie help thread Quote
10-28-2014 , 08:20 PM
Quote:
Originally Posted by Anais
omg, just wrote a huge long post about not getting why this bool kept always returning true, and then I happened to see that one of my if() statements was using the assign = operator instead of ==.

Why would that not throw a compiler error?

I demand digital blood!
Probably you are using a scripting language like JavaScript?

In most C-style languages, an assignment operation also produces a value. This is occasionally mildly useful in constructs like this:

Quote:
string line;
while ((line = streamReader.ReadLine()) != null) {
....
}
Or in chained assignments:

Quote:
a = b = c = d = e = 1;
For the amount of grief it causes, IDK if it's worth it, but here we are. Nobody wants to make an alteration to C-style languages that might break existing code.

The problem in weakly-typed languages like JavaScript is that everything is "truthy", so things like 3 or 'banana' are true. (The only things that are not true in JavaScript are false, 0, '', null, NaN and undefined). Therefore the value produced by the assignment is silently cast to true.

In C#, which is mostly what I write in, something like this will not compile:

Quote:
int x = 1;
int y = 2;
if(x = y) {
...
}
But this will, because the assignment produces the desired bool type:

Quote:
bool x = true;
bool y = false;
if(x = y) {
...
}
But it does throw this compiler warning:

Quote:
Test.cs(50,17,50,25): warning CS0665: Assignment in conditional expression is always constant; did you mean to use == instead of = ?
I also have the Resharper addon for Visual Studio, which underlines that "x = y" in the IDE and warns me about it when I mouseover.
Programming homework and newbie help thread Quote
10-28-2014 , 10:05 PM
Its c++ using Microsoft Visual Basic 2013
Programming homework and newbie help thread Quote
10-29-2014 , 01:43 AM
Yeah, C++ allows implicit conversions between, for example, int and bool. Because lol C++. They finally got around to introducing a way to fix it (sort of) in C++11, like 28 years after the language came out.
Programming homework and newbie help thread Quote
10-29-2014 , 07:46 AM
A 'good practice' (that I've never actually used) is to reverse the way you usually do equality comparison so that the constant is first.

So if you wanted:

Code:
if (foo == 3) { //
You would switch it to:

Code:
if (3 == foo) {
Because then if you messed up and forgot an = sign, the statement "3 = foo" is invalid and will throw an error.
Programming homework and newbie help thread Quote
10-29-2014 , 07:58 AM
An even better practice would be avoiding languages that set traps for you to fall into.
Programming homework and newbie help thread Quote
10-29-2014 , 09:14 AM
That seems more like an impossible practice.

Also like a very difficult way to get a degree or a job.
Programming homework and newbie help thread Quote
10-29-2014 , 09:40 AM
Getting a degree is one thing and C++ is a reasonable language for that. I would not recommend anyone but a masochist actually get a job writing C++
Programming homework and newbie help thread Quote
10-29-2014 , 09:56 AM
My point was more that C++ isn't the only language that has 'traps' or even this particular trap. Off the top of my head I believe Ruby/Java/JS let you use an assignment operator incorrectly in a comparison.
Programming homework and newbie help thread Quote
10-29-2014 , 01:17 PM
Quote:
Originally Posted by jjshabado
A 'good practice' (that I've never actually used) is to reverse the way you usually do equality comparison so that the constant is first.

So if you wanted:

Code:
if (foo == 3) { //
You would switch it to:

Code:
if (3 == foo) {
Because then if you messed up and forgot an = sign, the statement "3 = foo" is invalid and will throw an error.
Oh, that's quite brilliant! I'll have to remember that, thank you!

Is it normal to see it that way? I assume I'll be using c++ for the majority of my time in school over the next 18 months or so.
Programming homework and newbie help thread Quote
10-29-2014 , 01:50 PM
Quote:
Originally Posted by ChrisV
Getting a degree is one thing and C++ is a reasonable language for that. I would not recommend anyone but a masochist actually get a job writing C++
Programming homework and newbie help thread Quote
10-30-2014 , 01:24 AM
why do comp sci programs teach people to program in languages that are harder to use? tradition? seems dumb.
Programming homework and newbie help thread Quote
10-30-2014 , 01:40 AM
weed out all the people who aren't willing to smash their heads against the wall for a couple of years for the chance of a better career?

Our small class isn't going terribly well. I nearly aced our last exam, but we had as many As as we did Fs. (over a third of the class)

I'm glad I'm doing well, but I always feel like I have roughly no clue what's going on. I think of it a lot like playing StarCraft. In SC, says as terran, I like to wait to wipe out an enemy until I have like 5 nukes and the resource cap of units. I don't want to win, I want to deftly destroy my opponents with a staggering display of overpower. In other words, I like to be extremely over-prepared. And I don't feel that way at all, no matter how much I keep reading and rereading, doing practice work, self-tests, assignments, labs, projects, etc.

Our teacher, who's been doing CS stuff for a couple decades, just writes this perfect, concise code with no superfluous text. Really can't wait till I get to that point.

/rant
Programming homework and newbie help thread Quote
10-30-2014 , 01:48 AM
Yoda conditions ftw!
Programming homework and newbie help thread Quote
10-30-2014 , 02:00 AM
Quote:
Originally Posted by Your Mom
why do comp sci programs teach people to program in languages that are harder to use? tradition? seems dumb.
Sometimes the concepts are easier to teach in that language? Sometimes the language is quirky and weird which makes it interesting.

Either way, you have to realize that computer science isn't about learning how to program. Maybe moreso for the lower division classes, but at the end of the day, once you go through all those weird abstract languages, I feel like it should be easy to pick up any trendy language and learn.
Programming homework and newbie help thread Quote
10-30-2014 , 07:08 AM
It varies but frequently it's that the person responsible for writing the course either a) just uses what they know or b) is convinced that it's IMPORTANT for noobs to learn various esoteric concepts made explicit in the language. Academic computer scientists generally think it's important for people to know how computers work at a basic level, for some reason.

I had a horrible CS course. We started out learning to use a language probably nobody here has heard of, called Ada. Mercifully, we switched to Java later on. I was doing second year CS in the year 2000 and had a one-semester subject called "Database and Information Systems". We spent the first entire term, I **** you not, learning COBOL. Because the lecturer, who must have talked like 10 times about how he did his Ph. D. on punch cards, thought "COBOL is a good way to learn database fundamentals". Anyone who thinks COBOL is a better way to learn database fundamentals than SQL isn't competent to teach CS, it's really that simple.

I remember we spent time learning about Turing machines and learning assembly language and **** as well. Nothing could be less relevant to modern software engineering. We'd have been better off with a subject teaching us how to use various models of coffee machines.

I definitely think C++ is a suboptimal choice for teaching noobs. Why would you turn beginners loose on a language with no memory management? OH BUT IT TEACHES YOU THE FUNDAMENTALS OF HOW MEMORY ALLOCATION WORKS. Herpitty derp.
Programming homework and newbie help thread Quote
11-04-2014 , 06:03 PM
Quote:
Originally Posted by Anais
Oh, that's quite brilliant! I'll have to remember that, thank you!

Is it normal to see it that way? I assume I'll be using c++ for the majority of my time in school over the next 18 months or so.
It's a debated topic. The counter-argument is that it makes your code less readable.
Programming homework and newbie help thread Quote
11-04-2014 , 06:35 PM
Code readability trumps almost everything else for me. For instance, except in special cases, it trumps performance. CPU cycles are cheap these days. Software engineering is not.

If everyone coded their equality checks Yoda-style in C++ then it would be OK - just another syntactic feature of the language to learn. But they don't, and writing in a way that other people will understand is very important.
Programming homework and newbie help thread Quote
11-04-2014 , 06:38 PM
a guy I used to work with (one of the best programmers I have worked with actually) used that style in java. Mainly with null checks. It took a little bit to get used to but in the vast majority of cases the if blocks are short enough it didn't really feel less readable to me, past the initial novelty.

I'm not sure I have a point. I agree with you that generally readability trumps all, it just doesn't seem to me like (variableName == null) is so much less readable than (null == variableName) after like 5 minutes. ymmv? I don't actually use the style myself
Programming homework and newbie help thread Quote
11-04-2014 , 07:23 PM
I wouldn't mind personally but a lot of people find it important to have code that follows the way they naturally think. "Check if thing x is null" is a coherent thought, "check if null is thing x" is not. Witness the growing popularity of fluent APIs to see how important code that comports with natural thought processes is to people.

For me, I don't care about that, but if things are incorrectly named it really messes with my thinking. If you created a 2+2 app and had a class called "Post" that actually represented whole threads, it would seriously affect my productivity if I had to work on the app. Everyone has their annoyances that derail their thinking, its best to write code that is as clearly expressive as possible.
Programming homework and newbie help thread Quote
11-05-2014 , 09:08 AM
Quote:
Originally Posted by well named
I'm not sure I have a point. I agree with you that generally readability trumps all, it just doesn't seem to me like (variableName == null) is so much less readable than (null == variableName) after like 5 minutes. ymmv? I don't actually use the style myself
+1.

Also, "readable code" depends on a lot of factors and I'd argue that "readable code" for a student is very different than readable code for a professional. When you're learning it can be useful to have 'training wheel' type tips like the yoda condition to make your life easier.

In fact I just finished a code review where I asked our intern to delete a bunch of comments. They were useful to him, but they were explaining common practices and Java functionality that anybody with experience wouldn't need explained and so they were just adding a lot of noise to the file.
Programming homework and newbie help thread Quote
11-05-2014 , 09:42 AM
Commenting code is a sickness. I write comments whenever I'm doing anything particularly complicated, or to explain why I've done something weird as a workaround. Aside from that I hope my code speaks for itself. When was the last time you read a comment and were like "thank God for that comment, I'd never have understood this code otherwise"?

I have the misfortune to spend a bunch of my time improving an app that was originally written by a completely incompetent offshore team and I delete all their comments on sight without reading them. Either they're comments like "A method to save the Derp" on a method called SaveDerp(), or, even worse, they're comments like "A method to save the Derp" on a method called SavePineapples() that has nothing to do with Derp.
Programming homework and newbie help thread Quote
11-05-2014 , 09:52 AM
Quote:
Originally Posted by ChrisV
When was the last time you read a comment and were like "thank God for that comment, I'd never have understood this code otherwise"?
I'm lucky and work with a small group of really talented people - so our code comments are usually pretty useful.

So for example, we deal with a bunch of distributed processing and so a lot of times a comment will be explaining what assumptions are safe to make and what assumptions are not safe to make at a particular point in time.

Quote:
Originally Posted by ChrisV
Either they're comments like "A method to save the Derp" on a method called SaveDerp(), or, even worse, they're comments like "A method to save the Derp" on a method called SavePineapples() that has nothing to do with Derp.
This drives me crazy.
Programming homework and newbie help thread Quote
11-05-2014 , 10:09 AM
One thing that might actually merit its own thread is how quickly good software engineering degenerates into cargo-cult programming. (I'm reminded of this by how commenting code is actually good but in the hands of people who don't know what they're doing just makes things worse and less readable).

In this app that I work on that was written by an offshore team, there were some signs that someone in the team had read an article on dependency injection. Some stuff was divided off into Repositories, Services and so on in DI-ish fashion. The thing is, actual DI essentials (like programming to interfaces, a composition root, etc) were entirely absent, and even the Repository/Service thing was inconsistently applied. I stripped all that stuff out and converted it to direct calls, because many times there were 5 different methods doing the same thing and I would have to fight through 18 layers of abstraction before confirming this was the case. My strategy is: Make concrete, unify/simplify, then maybe we can talk abstraction.
Programming homework and newbie help thread Quote

      
m