If it's a simple assignment, I'll occasionally use the ternary operator, such as when I get a string from the database which should be a boolean value...depends on the kind of day I'm having.
Now function pointers embedded in ternary operators written in macros...those are a pain to read.
In related news for Java developers, I'm working in Java again lately and I found out there's a community edition of IntelliJ, the expensive Java IDE.
I usually use Eclipse but giving IntelliJ a try and am liking it quite a bit for the most part.. some real annoyances on how it handles tabs and spaces and where I click (i.e. in Eclipse I click on the white space on a line and it places the cursor at the end of the line, in IntelliJ it puts it where I actually click, ANNOYING!).
__________________ Uncertainty is an uncomfortable position.
But certainty is an absurd one.
Personally I can't stand the conditional operator. It's wholly unreadable to me - and the way I approach coding is that if the code isn't readable, it's useless code.
I'd rather read some code that uses a verbose if/else statement then tries to be cute with an operator. Maybe my brain just can't process it, but when ever I encounter one, I have to break it out on paper just to figure out what the hell is going on.
I remember handing in an assignment in my VB class that I was pretty happy with. It was full of really neat ways to save lines of code and reduce typing, etc.
I got 100% because it worked and there were no faults in my syntax, etc... but he had a note on it about wanting to talk to me about "readability." His point was that it's one thing to be efficient, but it's quite another thing to be unreadable. The amount of time I was saving by typing less statements was nothing compared to the amount of time it was going to take other people to figure out wtf I was doing...
I probably learned more from that single 10 minute discussion after class, than I did in most of the in-class material we worked on (it was an "Intro to VB" class).
Edit: Here's the code from the assignment. He wasn't really saying that my code was specifically hard to read, just wanted to talk to me about readability since he could see I was going in a certain direction...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With cboMaker.Items
.Add("Honda")
.Add("Toyota")
.Add("Subaru")
.Add("KIA")
.Add("Hyundai")
End With
End Sub
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
lstLog.Items.Clear()
lstLog.Items.Add("Car is " & cboMaker.Text)
If radIntBlue.Checked Then lstLog.Items.Add("Interior color is Blue")
If radIntBlack.Checked Then lstLog.Items.Add("Interior color is Black")
If radExtEmerald.Checked Then lstLog.Items.Add("Exterior color is Emerald")
If radExtNavy.Checked Then lstLog.Items.Add("Exterior color is Navy")
lstLog.Items.Add("A/C is " & IIf(chkAC.Checked = False, "NOT ", "") & "desired")
lstLog.Items.Add("Sunroof is " & IIf(chkSun.Checked = False, "NOT ", "") & "desired")
lstLog.Items.Add("MAG wheels are " & IIf(chkMags.Checked = False, "NOT ", "") & "desired")
End Sub
End Class
I like what was said earlier about how not wanting to read new ways of doing things just kind of perpetuates the whole thing, though. That was a very good point.
^ I dunno, generally your code is pretty readable. I'd add some blank lines to break up the contents of the btnApply_Click procedure into some logical blocks and although the code is pretty self-explanatory, comment before each block. A lot of people hate the inline If...Then statements, but I love 'em.
Haha, so easy to critique someone else's code. Mine's perfect, of course. There's some stuff I'd do differently than how you did (such as data-driven or settings-driven control contents), but hey, it's all subjective.
The With block is nice. I don't use them enough, they're great for readability.
I don't like inline if then statements because each then will be at a different place so I have to scan each line fully even if I only care about what's in the then and want to ignore the if part.
The nice part about modern IDEs is if you don't like a code's format, just change the format settings and tell it to reformat the code for you!
__________________ Uncertainty is an uncomfortable position.
But certainty is an absurd one.
I don't like inline if then statements because each then will be at a different place so I have to scan each line fully even if I only care about what's in the then and want to ignore the if part.
You know, after I read that, I just realized that it annoys me too. And here I was perfectly happy before. Screw you!
I'd burn you alive if you ever did this in code that you got paid for.
You sound like one of those guys that would be a real blast to work with. Do you actually keep cauldrons of boiling oil handy for when you deal with your co-workers, or do you usually have to send out for them and then throw the guy in it at a later date?
You saw the part of my post that mentioned it was an assignment in an "intro to VB" class, right?
You sound like one of those guys that would be a real blast to work with. Do you actually keep cauldrons of boiling oil handy for when you deal with your co-workers, or do you usually have to send out for them and then throw the guy in it at a later date?
We keep various sticks around the office, from golf club shafts to table legs, just in case. The testing manager swears that is the only way to get through to developers...in some cases, I have to agree.