- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Fist
Admin
Could you not just set up the parameters in the switch and make one call to registerEvent to have a compromise of both approaches?
Admin
project should have three states: open, closed and PROJECT_NOT_FOUND
Admin
project should have three states: open, closed and PROJECT_NOT_FOUND
Admin
Can you even imagine being in these code reviews? I'd rather drink tea with old ladies.
Admin
Nice pitfall: in the first parameter, they call the getStatus() function of the "projectDB" object and in the last, they call it on the "project" object. Just moving this blindly into the switch might break your behavior if not careful (assuming but variables are pointing to different instances)
Admin
Nice pitfall: in the first parameter, they call the getStatus() function of the "projectDB" object and in the last, they call it on the "project" object. Just moving this blindly into the switch might break your behavior if not careful (assuming but variables are pointing to different instances)
Admin
At my stage of life, I'd rather be drinking tea with old ladies than doing anything to do with software.
Admin
the original is hardly DRY anyway as its calling "project.getDescription()" twice..... How I hate idiots who try to cram as much as they can into "one line" of code. Its not like we're still having to use line numbers and having to re-number them when we want to refactor...
Admin
+1, I'd say the chosen solution wasn't DRY either.
Admin
"A foolish consistency is the hobgoblin of little minds" - Emerson
Seems like Grenk hasn't quite made the intellectual switch from coder to developer yet; the best he can do is imitate the pattern without understanding when it is (and more importantly, is not) appropriate.
Admin
The original code is an unreadable mess, to be sure. On the other hand, "Grenk" does have a point about DRY. There are other options worth exploring here. Simply moving each "registerEvent" argument to a separate line would do wonders, for instance. As would storing the condition we keep checking.
bool isClosed = projectDB.getStatus().equals(StatusProjectEnum.CLOSED);
if (isClosed) { //snip: re-open the project } else { //snip: close the project }
registerEvent( isClosed ? TypeEventProjectEnum.ENABLED : TypeEventProjectEnum.DISABLED, project.getId(), sessionUser, (isClosed ? "Enabled project " : "Disabled project ") + project.getDescription());
getDao().update(project);
Admin
I was surprised Grenk was a name, and not "set the language to green and the color to Greek".
Admin
If the internet is good for one thing, it's for taking a joke that was funny years ago and beating it to death until only an uncle could love it.
Admin
Or, and I think this is what "that other guy" was suggesting: (and let's see if I understand correctly about Markdown being a thing here)
Admin
More readable, and more maintainable... what happens to the ternary when we try to add a third project status, let's say ONHOLD? Either you turn your just-messy-ternary into a OMG-WTF-uber-messy-nested-ternary, or you refactor at the risk of introducing bugs.
WRT emurphy's proposal... I would argue that for so simple parameter passing, I prefer to have the explicit values of the parameters directly at the point of call. I find it more difficult to read when I have to move my eyes up-and-down the code to find the actual values of the parameters.
When the values come from a somewhat complex calculation, there is no choice... but when passing simple values, explicitly writing them at call-site is better, even if DRYness is somewhat sacrificed. Of course, just MY opinion...
Admin
With just two possible cases, what’s the point of the switch that an if wouldn’t do just as well?
Admin
The switch may be for people who have trouble with the law of excluded middle, who think the following code is sensible:
if (isConnected == true) { ... } else if (isConnected == false) { ... } else { /* mysterious things happen here */ }
Admin
The original code isn't DRY either, because Grenk pissed all over it.
Admin
It may be possible that attempting to close or open the project fails for some reason. However, that can easily be handled by a second switch (or for-else) statement.
Admin
agreed. and a ternary statement should have three parts: true, false and wtf am i doing
Admin
That you cannot write """ case CLOSED: """
Admin
enum ProjectStatus { CLOSED, OPEN, PROJECT_NOT_FOUND };
A "default: assert(false);" would be a good idea here. Unless you specifically want nothing to happen in an unexpected state, you should always use a safety net.
Admin
"How I hate idiots who try to cram as much as they can into "one line" of code."
Besides, I've worked for companies that measure productivity by lines of code written per unit time. Cramming too much in one line makes it look like your productivity sucks.
Admin
Shops that count lines of code deserve what they get
Admin
I came here to say what emurphy said. Kurbein, I'd rather see the function call once and have it not take up 3 lines than have the arguments computed on the line where the function's called.
An advantage of using switches over conditionals here (assuming whatever language being used here behaves normally, this is true with C) is that if you forgot a case defined by the enum type, your compiler will yell at you. "if (condition A) ... else if (condition B)" doesn't complain when condition C is added.
Admin
I am surprised that noone has identified the REAL WTF: why tf is the state of the project stored redundantly twice, once as the event type, and then again in the description?