VueJS & Laravel 5.3 - laravel-5

Laravel 5.3 + Vue JS + Vue-Resource
I am trying to do simple foreach loop with fetching data (with ajax).
my vuejs script file:
var vm = new Vue({
el:'#posts-app',
methods: {
fetchPosts: function(){
this.$http.get('/api/fetchPosts',function(data){
this.$set('allposts',data);
});
}
},
ready: function(){
this.fetchPosts();
console.log(this.allposts);
}});
Laravel Controller:
Route::get('api/fetchPosts', function(){
return App\Post::all();});
Blade template:
<div id="posts-app">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Posts</h1>
<hr />
<table class="table table-hover">
<thead>
<tr>
<td>ID</td>
<td>Posted</td>
<td>Title</td>
<td>Author</td>
</tr>
</thead>
<tbody>
<tr v-for="post in allposts">
<td>#{{ post.id }}</td>
<td>#{{ post.id }}</td>
<td>#{{ post.id }}</td>
<td>#{{ post.id }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
Laravel response:
[{"id":1,"created_at":"2016-09-12 00:00:00","updated_at":"2016-09-12 00:00:00","title":"Post #1","excerpt":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","content":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","author_id":1},{"id":2,"created_at":"2016-09-12 00:00:00","updated_at":"2016-09-12 00:00:00","title":"Post #2","excerpt":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","content":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","author_id":1},{"id":3,"created_at":"2016-09-12 00:00:00","updated_at":"2016-09-12 00:00:00","title":"Post #3","excerpt":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","content":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","author_id":1},{"id":4,"created_at":"2016-09-12 00:00:00","updated_at":"2016-09-12 00:00:00","title":"Post #4","excerpt":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","content":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","author_id":1},{"id":5,"created_at":"2016-09-12 00:00:00","updated_at":"2016-09-12 00:00:00","title":"Post #5","excerpt":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","content":"No? Alright, be that way. It's your meeting. Why don't you start talking and tell me what you want? Oh man! 50 G's? How you figure that? Partner? Oh yeah, I remember that little bitch! So you must be his daddy. Let me get this straight... I steal your dope, hmm? I beat the piss out of your mule boy, and then you walk in here, and you bring me more meth? That's a brilliant plan, ese. Brilliant. \r\n\r\nWhat? What do you want?! No. Don't even tell me you're hungry. Don't go there. Hahaha! Are you mad doggin' them, Tio? What, you don't like them? One ding. That means yes. Tio don't like you. Why don't you like them, Tio? You don't trust them? Why don't you trust them, Tio? BULLSHIT! MY TIO DOES NOT LIE! \r\n\r\nMr. Clean and his boy. I'm sorry that I had to tune you up. Respect, ese, you gotta give it to get it. What are we doing way the hell out there, what they close the mall or something? Heisenberg come on break it out. That's it? That's all you got? I thought you was a player. \r\n\r\nYou told me two pounds and now you waste my time with these Chiclets? Seventeen and a half, minus the half for wasting my time. What, you gonna argue? You got something to say? You're doing business like a couple little bitches. ","author_id":1}]
I get empty page, but I have ajax response with valid return code with all posts from database. What am I doing wrong?
Thanks

It looks like your vue resource call is not correct. The current version returns a promise with the first function being the success callback. As you don't mention the versions of vue and vue-resource you are using I reference the CDN versions.
A working reference would be.
<html>
<body>
<div id="posts-app">
<ul>
<li v-for="post in posts">{{post.title}}</li>
</ul>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.0.1/vue-resource.min.js"></script>
<script>
var vm = new Vue({
el:'#posts-app',
methods: {
fetchPosts: function () {
this.$http.get('/api/fetchPosts').then(function (response) {
this.$set('posts', response.body);
// or
// this.$set('posts', response.data);
});
}
},
ready: function () {
this.fetchPosts();
}
});
</script>
</body>
</html>
The Laravel api call piece.
Route::get('fetchPosts', function () {
return [
[
'id' => 1,
'title' => 'title one',
],
[
'id' => 2,
'title' => 'title two',
]
];
});

Well, I find out the problem.
When I call vue script from blade template it works fine, but if I call it from .js file it doesn't work....

return the data as json from laravel like this
Route::get('fetchPosts', function() {
$data = [
[
'id' => 1,
'title' => 'title one',
],
[
'id' => 2,
'title' => 'title two',
]
];
return response()->json($data);
});
and declare your posts as an empty array in the data object
<script>
var vm = new Vue({
el: '#posts-app',
data: {
posts: [],
},
methods: {
fetchPosts: function() {
this.$http.get('/api/fetchPosts').then(function(response) {
this.$set('posts', response.data);
});
}
},
ready: function() {
this.fetchPosts();
}
});
</script>

Related

An example to illustrate the: "Coding is just abstracted p‌r‌o‌b‌l‌e‌m solving" point to a complete layman?

I was talking to someone in a bar not too long ago, and the inevitable question of "What do you do?" Came up. My response "Oh, I just recently graduated; I work in company X. I'm a programmer" didn't quite satisfy her.
I've had this conversation a hundred times and people are never quite happy with the answer. They always have more questions. I think this is because, for the mostpart, people don't really know what a programmer does. The answer to this question normally gives you an insight into a person (sort of): Accountants might be analytical and good with numbers, lawyers might be good with words and debate etc. 'programmer' doesn't.
so I tried to give an answer that would give that sort of insight.
I tried to illustrate my point of "Well It's basically fancy problem solving" with an example of a classic programming problem.
The first thing that jumped into my head (for some reason) was the smallest subset problem... but I'd have to explain arrays, time complexity, etc
I thought of n-queens problem, but she'd never seen a chessboard in her life (yeah, I know)
Towers of hanoi was too hard to envision in general...
So I Was stuck and the topic changed.
Can you guys think of an example of a problem; that a complete lay(wo)man can understand, which would give someone an idea of the kind of thinking programmers have to do? (and by extension save me answering awkward questions about how I make my living)
I get asked what I do too, and basically, all I say is that You wouldn't understand much about it, which will get you one of two responses: "Try me" and shes interested ->move to next section of comment, or she will just shut down and think that you think she's not intelligent. USE WITH DISCRETION.
Now, if she says try me, you have a challenge. Obviously what you do is going to be over her head, but you know you can explain a little to her. Basically, what I try to explain is that I am a logician, kinda like a magician. When the computer needs to be told to do something, and no one else knows how to tell it, they call me up. Sometimes it's simple, other times, its likes climbing Mt. Everest with your fingers on some impossibly difficult task. Imagine the worlds largest word search, and you don't know what words you're looking for. That's what I do. I find those words. So as you could imagine, I'm quick to notice little things and remember them. ;)
That should elicit the kind of response out of any women worth your time.
Good Luck!

When debugging, how do you estimate if you should rewrite or keep looking?

You have all met this scenario. I am using a new algorithm for the first time, and I am sitting at my computer trying to find out if it is some syntax problem or if I have misunderstood the algorithm. In a scenario like this, I would sooner rewrite the program than spend time staring at the screen. But this raises the general question, that I am curious to hear from programmers more experienced than myself:
How do you judge when it is the right time to rewrite, or should you continue sitting there staring at your code looking for the bug?
Are there any useful heuristics that professional programmers use?
This doesn't sound right to me at all. As long as you don't understand the (required) algorithm you should not write code. That's called trial and error and is a pretty sure way to end up with poor and buggy code. Think before you act.
As food for thought a bit provocative statement:
Writing code is the last thing to do. Being a coder might sound like
you should be typing a lot but in fact if I count how many characters
an average programmer commits each day and ask some secretary to type
the same amount he/she would be done in under 30 minutes.
It's a very difficult question and really depends on your case.
Typically, there are two cases :
the algorithm is simple and you will find the bug fast. A rewrite is generally not necessary unless you want to optimize it.
the algorithm is really complex : it's difficult to find the bug. But It can be difficult to rewrite because you can miss some subtile features of the algorithm. The risk is to have a new algorithm but with new bugs !
I don't think there is a clean answer to that problem. I would say that it's better to find the bug than rewriting all. Rewriting is necessary when you need to optimize or clean the code (not because you don't find a bug).
That's my two cents.
You should never rewrite something that has already gone through testing more than once.. the theory being that if you went through it at least once, you've already ironed out bugs. Trying to recreate the end-result of those resolved bugs is very difficult. Joel has a very good article on this, and I tend to agree, even though I have been in the position you are, and my inclination was to just throw it away and rewrite it..
http://www.joelonsoftware.com/articles/fog0000000069.html

What to do when faced with a seemingly unsolvable situation with a time limit?

I am a computer sciences student and I usually have really tough programming assignments. I don't know if it is only happening to me but sometimes, particularly when deadline is approaching, I find myself in a harsh situation.
I cannot find my mistake in the code or come up with a another great idea. Then boredom comes in and the problem begins to seem unsolvable.
I would like to learn their ideas to cope with this situation. Is it better to focus on something else for a while? Or try again? Or try harder and harder and look for the solution on the net, etc?
I alway like talking about the solution with another programmer. Just talking makes me use a different part of my brain and most of the time I hear myself talk through a solution.
Sleep is good, or if not sleep then at least taking a break, going for a walk in the fresh air etc.
Brainstorming the problem with colleagues / fellow students can help. Even just explaining the issue to someone else can be enough to make the solution click in your brain.
Failing all the above, ask on Stackoverflow :-)
Try breaking down the problem into smaller, easier problems, and solve those. Don't try and tackle everything at once, and avoid trying to hack your way through.
If you're still stuck, taking a break can be good. Sometimes the answer is suddenly obvious when looking through a refreshed pair of eyes. Solutions to problems often come to me in my sleep, and I'll wake up knowing the answer.
For me, I encountered a couple of times where I took quite a bit of time (10 to 30 mins) to define the problem in writing as to submit the question on SO, and got ideas that led to the eventual solution while typing out the question.
I find that when I document your problem in a way that others can understand without having to understand the unrelated parts of the your entire application/project, I consciously break down the problem into isolated, independent parts which helps me or another developer analyze and decide the next course of action.
Just my two cents :)
In your case (school work) I would probably seek out the instructor/professor or TA. While they will certainly not "give" you the answer at the very least you might learn something else in the process.
Specifically I would explain to the the difficulty you are having, what you have done to try to solve it and any other things to show that you did work.
A lot of times while walking though this on your own you might come up with solutions. They can probably give you hints or suggestions as well.
Worst case scenario is that they tell you to go away and to leave them alone.
Others have posted sleep (#sjobe, &Vicky) and asking someone is good (#Christopher Altman). BTW, that is often referred to as "rubber-ducking".
My personal problem is wanting to see something through and getting consumed in getting to the finish, almost always to my own determent. What I've learned over the years if a little research doesn't help (< 30 minutes) and talking it through doesn't explain it and you can't or don't want to sleep on it, do something for the mind, body and spirit: Go outside!
Seriously, go for a 30-45 minute bike-ride, run, walk, swim, whatever. Try to think of something else. Tell yourself a story or mentally work on another problem if you must. Cool down and return. You'll be amazed at how refreshed you'll feel. The endorphins will help.
If you're embarking on career driving a desk, it's a great habit to get into as well.
-Cheers
The entire art of surviving or rather conquering in situations like this is about staying to have a solution oriented approach . By this I mean staying positive that even if a solution is not working it , have faith that your tries are getting you closer to it .
Yes I strongly agree that taking a break is an integral step of reaching your goals but take a break to return back with stronger spirits to solve a problem .
Involve yourself into different solution finding strategies along with a spirit to enjoy the same .
The solution finding strategies may involve :
Talking to your friends who dont understand the problem to a good level and help them understand . It will help you explore the ins and outs of the scenarios . Sometimes explaining other people helps us understand the problem in a much better scenarios .
Sit down with a paper and a pen or its better to have a diary where jot down all the ideas as they strike you . Take your diary with you always as it helps to jot down the ideas otherwise later we forget . Also sometimes the game is about connecting the dots . An idea from morning first half and evening time can be perfect mix to resolve the problem .
Go out on a brainstorming session with couple of friends and entertain all the ideas that they put on table for once and consider them . Remember no idea is an stupid idea . Either it is solution or a contributing step towards a solution .
There may be times when you need to visit an industry expert or a researcher to dig deeper into concepts of technology . Before you visit an industry expert keep all your research documents and brainstorming ideas collected . Share it with the researcher correctly . Also have a SWOT analysis of the person you are trying to meet so that you get to understand in which part is the person strong and can help you . Also carry a recorder with you such meetings because jotting down everything becomes difficult .
Dont believe in what ever is always suggested make sure to come home and do entire research on internet on whatever is shared . That will help you increase your knowledge .
Do some experiments . Some hits and tries randomly and based on results reach to conclusions .
Each of these steps plays a very important role in brainstorming and reaching to a solution . Looking forward to hear from you what were your experiences trying these out .
A similar question has already been asked here https://stackoverflow.com/questions/427532/what-do-you-do-when-youre-stuck.
Sleep is my personal favorite though, although if you're like most college students, you're probably doing a lot of last minute coding and you don't have enough time to sleep and submit your work on time [I was guilty of this too].
What I like to do when I'm stuck on a problem, I usually try to draw out my problems. I just get myself a piece of paper and write down the problems that I encouter. While doing this I like to make Class diagrams/Sequence Diagrams, just to clearify the situation in. Really helps to just get back to old skool pen and paper and not look at your screen for a while.
As a student I also face this problem from time to time. What helps me quite often is to get away from the computer, take a pencil and some paper and start to write down the code by hand. I don't know why but often it is easier for me to solve it on paper than by using an IDE/editor. Probably because your brain works differently then.

How to justify to your colleagues that they produce crappy code?

I am finding somewhat difficult to carry on working in my current job.
The codebase has become a bit wild lately (but definitely not the worse I've seen), and I'm having a hard time dealing with some parts of the code. I could be stupid, but most likely it's just that it demotivates me a lot to start working on something that is hard to reason about.
My boss is already aware of my thoughts - I expressed what it feels like to work like this. He asked me to provide examples of what was wrong. When I pointed out two or three small issues, he said "yeah, ok" but that refactoring costs him a lot of money, and that we have to get the product out (not the first time I hear this).
I have to admit that the examples were not the most compelling, but the problem is actually tough to explain. It's made up of a lot of tiny "bad decisions" throughout the codebase. (We also see this issue is absolutely subjective). For instance, bad naming, dealing with nulls, boilerplate, not making code reusable (or the opposite) and so on. It can be tiring to re-think someone else's code over again to justify I would have done it differently.
Do you have thoughts on how to deal with this?
I am a bit fed up of having to go hacking around a quick 'n dirty codebase every time!
Sometimes your fellow programmers do things very differently than you, and things you might feel are way wrong might actually have positive aspects. We all have our schools we come from. I think I've come across programmers who complain about things I don't understand equally as often that I myself have felt something needs to be complained about.
Make sure you can deduce what you complain about into a concrete disadvantage. If for no other reason so that you can motivate middle management about improvements to make. Things that are hard to deduce into measurable facts usually originates from difference in taste/style rather than quality (there are boooks to read about this subject). The answer posted by smacl have good and concrete advice!
If you can deduce your concern into a real disadvantage, then I really do not agree when people say that one have to "accept" situations like this. I've been exposed to this problem more than once, and let me tell you, refactoring is not the solution to the problem. Refactoring only fixes the symptoms.
Accepting a situation like this is the same as saying "bad quality product lines and expensive and frustrating maintenance is something my company can live with". This is ofcourse seldomly the case. However management (i.e. those with the go/no-go on what projects to prioritize) are very often not technically aware of what the problems are, or why development is expensive. They shouldn't have to be for that matter.
That's why you need a development organization with technical leads, chief architects, a good organisational structure and tiered model etc. Experienced software professionals who have seen where the road leads to if you ignore certain aspects of development. It's about changing the "culture" of your team(s).
Either you stick with your company and try to change how you do things from the roots, or you find another place to work and make sure you find out during the interview exactly how they work in every-day development.
Good luck
I recently faced a very similar problem and a friend gave me some advice that helped a great deal. He said: "keep yourself out of it."
What he meant was, that you must communicate the problems because they are real, costly problems with consequences in terms of time and money. But when you do communicate, talk only about the consequences for the organization. Do not mention the consequences to you, because then it just sounds like whining and will be ignored.
For example:
Not keeping yourself out of it:
"The other developers use these obscure, misleading identifiers and then I have to spend hours going over the code trying to discover what they meant. It's taking up a lot of my time."
Keeping yourself out of it:
"It would be very helpful and cost effective to do some refactoring of class and variable names and also establish some coding standards around identifiers. The immediate payoff will be an easier-to-understand codebase for everyone, leading to better productivity. The longer-term payoff will be that later we'll be able to modify the code and fix things faster. If a critical bug is discovered right before a release, an understandable codebase will be really important."
I hope that helps.
1) Make the problem more visible and get management buy-in
Keep a very detailed diary of the time spent on various coding tasks over the period of about a month. At the end of the month analyse and summarise the contents for your boss, i.e. time wasted and hence money wasted, to illustrate that change of some form is necessary.
2) Think of a cost effective way of moving forward
For example; Rather than refactoring the entire code base, seperate interfaces from implementations, and enforce tighter standards, including unit tests, naming conventions, etc.. at an interface layer. Thus each programmer can have confidence in using code that they have not written. While this is sweeping the crap under the carpet to a certain extent, it is a good way of preparing for larger scale refactoring.
It is important from a management perspective that workflow is not interrupted, and positive results are visible, so plan accordingly.
3) Agree longer terms improvements with your co-workers
Sit down and agree reasonable coding standards for future code with the other programmers.
Perhaps you could setup monthly meetings and at those meetings you could demonstrate good and bad code. Obviously you don't want to point fingers so you'd want to use generic code examples that are based off of stuff you saw in your project. This way you can constructively gather support from others in your style. You might want to compile these after the meetings so people can easily reference them.
I think it is real easy to point out issues, and complain but to mentor people and help them change requires effort. It isn't an easy task but if you are having trouble being motivated with your job perhaps this would give you a nice burst of motivation. You might learn some things a long the way.
You'll find that this is common-place. What you can do is accept that things are done differently by different people. As you fix bugs or add features, you'll get a brief window into a sub-section of the application that you can improve. When you work on the code, you can make it better, and they don't need to know that you're piecemeal improving the code.
Be very careful though. Sometimes code is written in a way that looks 'hacked', but solves a bug that is not easy to discern. Especially if it is older code which has been tried and tested.
On another note, complaining will only get you viewed as a complainer. Think about what outcome you want, and what actions will most likely produce that outcome. You will always hear the answer 'No' when you ask, 'Can I do X-days of work for absolutely no noticable result?'
You could quit and hope to find something better.
Or, you could stick it out and try improve the code that you can control, when you can control it. No matter how well intentioned the developers are, if there is more than one developer the code base will be "ugly" by a competent developers standards. Work with the other developers to improve their abilities and refactor code as you make enhancements.
For starters:
Enforce the use of static code analysis tools. Every language has a few well known tools.
Show some before and after refactored code examples, and explain why you think it's better. Try not to put any one person on the spot.
Code reviews by experienced developers.
keep in mind, some developers can't be helped no matter how much you try...
If someone critiques your code be polite and open minded, you might learn something.
Cyclomatic complexity / number of changesets/bugs. Complex code is more likely to break, cause more bugs which causes more changes, which cost more money!
99% of the time you never get to choose the people you work with. Not all relationships work out, be they work or otherwise.
It would be best if your project was broken up enough so that each developer can contribute to a spec of what the other needs, so programmers don't step on each other's toes.
Getting people to change their coding style is hard. It takes a cast iron technical lead committed to such things and will help when you bring it up. Management types can't do this, leadership needs to provide technical details.
It sounds to me like you don't have a problem with the code so much as your coworkers. It will probably be very difficult for you to force the changes you want to see. Your best bet would probably be to start updating your resume and keep your eyes open for other opportunities
I think that once you're in the middle of the weeds, you do not really have a good chance of getting things done right, you just have to get them done. I would say most developers do not like firefighting and want the ideal code base, but in my opinion this requires you to spend the time up front planning the system out.
I'd recommend trying to work with your manager to ensure that the areas you feel are lacking now are not lacking in the next project. Maybe its putting you on the lead, having more code reviews with peers, maybe it is further training for the entire team.
Either way, I think this is something that most of us go through. I do agree with the other person advising some caution on this. I know that code I wrote yesterday seemed great at the time and looking back on it, can probably find 10 other ways to do it and make it look cleaner.
Have you considered maybe adding fxcop to the automated builds to enforce coding style? Other than that, you could try suggesting TDD which would give the power to whomever writes the test to enfore that the interfaces for each class are structured in a particular way.
Off the top of my head, that's all that I can think of.
Things in life are not perfect and if you start nitpicking, feathers will be ruffled and relationships soured.
The best method is to pick your battles carefully. If something is small enough ignore it and live with it. If it is big and worthwhile (i.e. the management sees ROI in backing you) go for it.
This is apt for your situation...
God, grant me the serenity to accept the things I cannot change courage to change the things I can and the wisdom to know the difference.
One thing I try to do and it may help you. If a part of code is bad, and the idea you propose to fix it is agreed as best but "no time" excuse is given, why dont you rewrite it? say on your own time? If you decide on sticking around at that job for a while it will only help you. And only you will learn and become a better programmer.
Note that it is a good idea and I would even say required, to do a complete code review of that change before check-in and you should try to time the check-in so that it is before a complete regresion test cycle for a release. That way your refactoring is completely tested out. Over a period of 6 months or so, it will start showing a beneficial impact and you can then ask for time allocation for this, with proof to back it up.
The only thing that has a chance of convincing management is demonstrating that the things you are citing as perceived problems become actual problems.
To try to take advantage of this, try to keep the "complainer" tone down to a minimum, that is, focus on how this affects the bottom line rather than how it makes you feel. Point out possible consequences of poor decisions that you see being made. If those consequences come to pass, and they cost more than an up-front fix would have, gently remind management that you foresaw the difficulty and provide a helpful suggestion as to how future similar costs can be avoided with a little up-front effort.
The problem is, in many organizations, the problems will never cause enough of a problem for management to care, or if they do, they won't see the connection between your perception of the problem and the actual problem the way it occurs. In these cases, you end up seemin like a needlessly persnickety technical person, which isn't a reputation you want to have.
So my advice is, pick your battles. If there is something very egregious that others are about to let slip, then you can speak up and perhaps be vindicated later. For the little details that just grind away at you, I'm afraid there's not much you can do but put up with it.
Show them their own forgotten code disguised as yours for critique.
Take an old piece of their code they have forgotten about
Pretend you wrote it
Ask them to figure out something with it
Make sure they point out how bad the code is for whatever reason
Add your own items. Brainstorm what should be done since it's your fault.
Let them know you didn't know how to bring it up to offend them, but it's their code.
If they recall that they wrote it, they might catch on..
If you have a good relationship with your manager, you might be able to use this to work yourself into a "Senior" or "Lead" Developer role. You could propose that it would be best if one person on the team takes technical leadership of the code base. It would be your job to review the code of others and ask them to make improvements when you feel it is necessary. If you go this route, just make sure to take it slowly. If you ask for a lot very quickly, then you could end up pissing off all the other developers.

Programming in a crunch

Ideally you want a schedule that's accommodating and flexible but when it comes to paying the bills and working in a business, that's rarely a luxury programmers have.
I have been fortunate to have the grace of Steve McConnell and Frederick Brooks to tell me what to do if I want to screw up my project and I take their work seriously.
And yet there are still times when your back is against the wall and you need to speed up the work. What are some "tweaks" to your process you've used to speed up delivery without sacrificing quality? Is this even possible?
I consider learning good practices to be of higher value than learning the actual code so no "be a better coder" answers. That's a given.
jpgs are faster than html pages
Without sacrificing quality, your best options are pretty much the same best options that you have when it's not crunch time: Eliminate unnecessary distractions, focus on the most important work first, obtain better hardware, and offload any work that you can reasonably offload.
Arrange my tasks in some sort of list of small independent tasks (or, tasks where the dependencies are already taken care of in the order). Then, just sit down and clear one task off my plate after another. Anything where I have to coordinate with someone else, ask a question, etc. is punted forward; I just sit and code with no distractions, until I get to a place where I need some sort of outside interaction. Then I deal with all of my coordination with other people, re-planning, and so on in one batch, and go heads down into straight coding again.
Also, prioritize and cut everything that's optional. If you get the required stuff done in time, then go back to the optional stuff, but do what is absolutely required first. This may be relaxed slightly if there are tasks that are easier to do when you're in the right context, but on the whole, try to keep the optional parts to a minimum.
Oh, and don't make these judgement calls while you're coding. When you're coding, just take one task off your list, do it, and move on to the next. As I said, batch up all of the non-coding work into chunks so it doesn't slow your coding down.
Anyhow, that's what I do when I'm up against a wall. Not sure how well it works for anyone else.
Usually, cutting features and work items is the best way to meet a tight deadline.
You can try to rush coding, but you'll almost always pay for it later - You'll spend much more time debugging and stabilizing the rushed code than you saved in the first place.
Actually, I think a lot of programmers and organizations have a lot of low-hanging fruit. Make sure you are using your time productively, know how to trade-off speed for quality appropriately, don't "gold plate" things beyond what your customer is asking for, etc.
Profile your organization's processes. What is the bottleneck slowing you down? Context-switching (i.e., multitasking) is often a real productivity-killer.
I suppose the problem with this is that when your back is up against the wall, organizational improvements like these seem like luxuries unto themselves, even though they are the things that could really help you speed up your work.
The biggest help (for me) is forcing myself to complete each single task before moving on to another one. If I need to get some bugs out of libfoo, or even write libfoo to go on to the next step, I force myself to stay on that task, then the next one, then the next one.
Most of the time that I find myself in a crunch, its because I was too much of a grasshopper, jumping around from thing to thing.
This morning I sat down and forced myself to write a bunch of unit tests, rather than working any more on the library being tested. It sucked, but now they're done, and I can really sail through finishing my library.
I'd suggest learning GTD or something like it before you get in the crunch, using to avoid a crunch if at all possible and then, if you must, get through the crunch by just doing it. Get in the zone and persevere.

Resources