Team-BHP > Shifting gears


Reply
  Search this Thread
187,908 views
Old 22nd August 2013, 03:50   #601
Senior - BHPian
 
vivekiny2k's Avatar
 
Join Date: Feb 2005
Location: cincinnati, jabalpur,chennai
Posts: 1,264
Thanked: 209 Times
re: IT Industry and Employability of Technical Graduates

My solution would be similar to TSK's. When I started coding, one of my friends (years ahead of me) told me to optimize the load, not the source code. Didn't realize it's meaning until I joined a COBOL shop much later.

We write programs very high on maintainability and readability. Anybody should be able to pick up a program 20 years old and make changes. Programs that need hours of explaining for a few lines of code are looked down upon.

Actually a few years back I was asked to write a pick logic from different warehouses for minimum number of shipments and minimum shipping cost (e.g. multiple items ordered from amazon which are found in different warehouses). In order to keep the readability and maintainability, I removed the permutation logic (120 permutations for 5 warehouses), and hard coded in array in program. This was on request from my manager that I should avoid mathematical calculation as far as possible and make it easy to understand.

That program would probably get the lowest possible rating in a techie convention but that's what the business needed. I was given that after somebody else had failed to come up with that logic.
vivekiny2k is offline  
Old 22nd August 2013, 09:31   #602
Team-BHP Support
 
Samurai's Avatar
 
Join Date: Jan 2005
Location: Bangalore/Udupi
Posts: 25,813
Thanked: 45,434 Times
re: IT Industry and Employability of Technical Graduates

I hope you are not saying my code is not readable. Any good C/C++ programmer should be able to read it effortlessly. The simple fact that amitk26 and I came up with the same logic indicates that it is neither complex nor rare.

While Tanveer's code is functional, it is clearly not efficient. Since he is not a career programmer, that is alright.

The code should be both readable and efficient. One should not come at the cost of other. Writing dirty code in the name of efficiency or in the name of readability is not good policy. You might have to make a compromise sometimes due to lack of time, but you should never accept it as general policy. Writing readable inefficient code, or writing efficient unreadable code, should both be highly discouraged. Both are examples of bad design.

Consider Steve Jobs design principles, something IT guys rarely understand or care about, but should. In fact, some of his ideas about "simple is beautiful" is something I have practiced in my software design. I always try to simplify the design until cows come home. Simple is also more readable. I have often torn down months of work just because I got a better idea to simplify a logic. Whenever I am forced to put a band-aid solution, it becomes a TODO in my list, something to revisit when I have time. Good design has lot longer shelf life than bad design, and will be highly reusable and extensible.

Some of the unofficial C++ code I had written back in mid 90s, are still being used in my company, even for new projects. That is possible only because it is readable by newbie programmers, and usable because of good design. I personally improved and reused the socket classes I wrote for the code generation tool for 15 years before switching to Boost C++ socket classes. Yup, when something better comes along, I don't stay attached to my old code.

Last edited by Samurai : 22nd August 2013 at 09:42.
Samurai is offline  
Old 22nd August 2013, 10:27   #603
Senior - BHPian
 
anujmishra's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 1,290
Thanked: 492 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post
Since you tried, I'll have to put a C++ version.

int main(int argc, _TCHAR* argv[])
{
for(long long i = 1; i <= 100; i++)
{
std::cout << (i%15 ? (i%5 ? (i%3 ? std::to_string(i) : "Fizz") : "Buzz") : "FizzBuzz") << std::endl;
}
return 0;
}
Quote:
Originally Posted by Samurai View Post
I guess this is the most natural way of solving it.
There is inherent problem in this solution. For this implementation, you are wasting precious memory by taking your counter i as "long long". What is the need of taking "long long" here when you can achieve it even with 2 or 4 bytes of counter? Basically this type of programming is called "evasive programming" where programmer takes unlimited space without calculating actual requirement, just to make sure (assumption), it will never fail, if some contents increases or decreases.
Be it taking mind boggling size of static array in place of dynamic memory allocation or declaring variable types more than required. Such solution definitely work but at the cost of memory.

The environment where we work, we are not allowed to waste precious memory like this and every bit counts when we need to deliver products with good performance.

Yes, this is natural way to solve it, but we also need to take care of memory footprint.

Quote:
Originally Posted by vivekiny2k View Post
That program would probably get the lowest possible rating in a techie convention but that's what the business needed. I was given that after somebody else had failed to come up with that logic.
I absolutely not agreeing with this. In most of the business performance matters to extent that performance would be the main criteria for marketing. One thing is just to make it work and another is make it work better than benchmarking data.

Also, regarding readability, if you hop through some opensource projects code (e.g linux kernel code), you will not find any of their code readable. But this is also skill, which comes with experience, what I feel.

Last edited by anujmishra : 22nd August 2013 at 10:39.
anujmishra is offline  
Old 22nd August 2013, 10:59   #604
Senior - BHPian
 
Join Date: Jul 2008
Location: Bangalore
Posts: 2,089
Thanked: 715 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post
I hope you are not saying my code is not readable. Any good C/C++ programmer should be able to read it effortlessly. The simple fact that amitk26 and I came up with the same logic indicates that it is neither complex nor rare.

While Tanveer's code is functional, it is clearly n.
Well there is some merit in the readability argument. It depends on the overall coding environment.


In fact years ago when I was in Motorola there was a clear coding guideline against ternary operator and it was frowned upon in code reviews. In any practical code always the conditional clauses are too big and it is easy to misplace a brace and create hard to debug issue.

However personally I do not think there is anything wrong in using a ternary operator for simple cases like this.

In the compiled code weather you use ternary or if you use if , else if clauses the number of JMP will be same, By eliminating few local variables there is a possibility of few less LDR and register usage but then if you use compiler option like -O2 of GCC all such optimizations are done by compiler itself.


It takes immense time and put strain on resources to debug and fix specially in our kind of environment when releases are scheduled daily and missing a build deadline means hell breaking loose.



@Anuj : Noticed that long long too but ignored it in favour of general direction. In fact few years back it was really hard to find use of long long in whole platform code except for clock ticks.

Last edited by amitk26 : 22nd August 2013 at 11:01.
amitk26 is offline  
Old 22nd August 2013, 11:32   #605
Senior - BHPian
 
anujmishra's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 1,290
Thanked: 492 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by amitk26 View Post
However personally I do not think there is anything wrong in using a ternary operator for simple cases like this.
It is simple case but it is nested 3 times. This is very error prone and one cannot debug it. I prefer this ternary operator only for simple non-nested case.


Quote:
Originally Posted by amitk26 View Post
@Anuj : Noticed that long long too but ignored it in favour of general direction. In fact few years back it was really hard to find use of long long in whole platform code except for clock ticks.
OT: Only few system might be using long long for clock ticks, but many including linux kernel does not use it. That is why we are expecting Y2038 issue. One fine day in Y2038 we will move to past.
anujmishra is offline  
Old 22nd August 2013, 11:33   #606
Team-BHP Support
 
tsk1979's Avatar
 
Join Date: Feb 2005
Location: San Jose, CA
Posts: 23,717
Thanked: 22,809 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post
While Tanveer's code is functional, it is clearly not efficient. Since he is not a career programmer, that is alright..
BTW, your code was a revelation for me. I did not understand much, except for the meaty line.
I did not know a?b:c exists in C. I thought it was verilog only. In hindsight, if I had written verilog for it, it would have looked much similar to you code.

In hardware or VLSI logic, it would be actually quite simple.
You would need a /3 divider and /5 tester (no actual division needed) (Simple approach)


If you want better logic, all you need to do is an adder.
Add the digits. till you get a single digit number. Largest you can get is 9
So instead of an expensive divider all you need to see is whether addition output is 3,6,9

For 5, just see of the last digit is 0 or 5

No dividers no pain

The output of division condition is of interest., so R3 =1 if its divide by 3 and R5 is 1 if its divide by 5



We would need a 2 bit register to represent 4 states/
So 11 = FIzzbuzz
01 = fizz
10 = buzz
00 = just use the number
So our mux condition would be

result = (R3&R5)?2'b11:(R3?2'b01:(R5?2'b10:00))

So if result = 00 output number, else output Fizz,Buzz or Fizzbuzz
tsk1979 is offline  
Old 22nd August 2013, 12:14   #607
Team-BHP Support
 
Samurai's Avatar
 
Join Date: Jan 2005
Location: Bangalore/Udupi
Posts: 25,813
Thanked: 45,434 Times

Quote:
Originally Posted by anujmishra View Post
There is inherent problem in this solution. For this implementation, you are wasting precious memory by taking your counter i as "long long". What is the need of taking "long long" here when you can achieve it even with 2 or 4 bytes of counter? Basically this type of programming is called "evasive programming" where programmer takes unlimited space without calculating actual requirement, just to make sure (assumption), it will never fail, if some contents increases or decreases.
Be it taking mind boggling size of static array in place of dynamic memory allocation or declaring variable types more than required. Such solution definitely work but at the cost of memory.

The environment where we work, we are not allowed to waste precious memory like this and every bit counts when we need to deliver products with good performance.
Ok, smiley limit reached. You remind me of my boss from 1991 who insisted we all use unsigned variables and pass parameters via global variables instead of stack.

Instead of all this analysis, you could have just asked me why I used long long here. The trouble is std::to_string() implementation in VisualStudio10 doesn't support anything smaller than long long. Since this is a C++11 feature, VS10 has limited support. If I had tested the code in gcc, then I could have used int and made you happy.

IT Industry and Employability of Technical Graduates-fullscreen-capture-8222013-112822-am.bmp.jpg

For this reason I don't use std::to_string() in my usual code. I use boost::lexical_cast<std::string> instead. But I didn't want to introduce boost code here. May be once I move to VS13, I can start using std::to_string.

Quote:
Originally Posted by tsk1979 View Post
We would need a 2 bit register to represent 4 states/
So 11 = FIzzbuzz
01 = fizz
10 = buzz
00 = just use the number
So our mux condition would be

result = (R3&R5)?2'b11:(R3?2'b01:(R5?2'b10:00))

So if result = 00 output number, else output Fizz,Buzz or Fizzbuzz
Well, I don't optimize to this level unless the requirements demand very high performance level. It reduces readability too. Besides, bit-wise operations will puzzle most of the recent CS graduates who barely understand boolean algebra.
Samurai is offline  
Old 22nd August 2013, 12:21   #608
Team-BHP Support
 
tsk1979's Avatar
 
Join Date: Feb 2005
Location: San Jose, CA
Posts: 23,717
Thanked: 22,809 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post

Well, I don't optimize to this level unless the requirements demand very high performance level. It reduces readability too. Besides, bit-wise operations will puzzle most of the recent CS graduates who barely understand boolean algebra.
You do not have to. Software (C++, boost etc.,) can be optimized. Unfortunately, when you code VLSI, every small "leeway" cascades, and results in actual die area. Over a chip with millions and millions of gates, such small coding differences can result in a chip which is 10% more expensive with lower performance.

Verilog, though behavioural, is still structural to a degree. You can write a divider, and a good tool and lib can give you an efficient divider, but for such simple case where all you need are 3 comparators (3,6,9) its not acceptable to use a divider.

But we have some leeway in choice of gates. What you wrote is a multiplexer. A good tool can analyze the library, and at the time of actual synthesis into gates may decide to use NAND/AND/OR gates if that means lower area and faster signal transition speed.
tsk1979 is offline  
Old 22nd August 2013, 12:25   #609
Team-BHP Support
 
Samurai's Avatar
 
Join Date: Jan 2005
Location: Bangalore/Udupi
Posts: 25,813
Thanked: 45,434 Times
re: IT Industry and Employability of Technical Graduates

My world is different. Simple/standard instructions are always optimized by the compiler before the binary is generated. So I don't bother about micro optimization. But the compiler can't optimize the design. So I focus more on design.
Samurai is offline  
Old 22nd August 2013, 12:33   #610
BANNED
 
Join Date: Mar 2007
Location: Kolhapur
Posts: 1,717
Thanked: 1,901 Times
Infractions: 0/1 (7)
re: IT Industry and Employability of Technical Graduates

In programming, premature optimization is the root of all evil. First of all, a modern self respecting C++ compiler optimizes the hell out of your code - the final code generated would be unrecognizable from your original code - loop unrolling, tail recursion optimisation, using registers, sub expression elimination, strength reduction, code alignment for pipelining, prefetching and tons and tons of other stuff. We aren't in an era where most people need to code something like Duff's Device (very clever and terse loop unrolling).

Another thing is that you cannot tell by looking at code as to what needs to be optimized or what can cause performance issues. In most cases, it should be enough to follow some standard best practices.
- Optimize the algorithm - eg. O(nlogn) rather than O(n2) where it's possible.
- Profile your code to find out the performance issues and then optimize.
- Spend time on optimizing the inner loops.

However, I think we are getting far off-topic for this thread.

Last edited by carboy : 22nd August 2013 at 12:50.
carboy is offline  
Old 22nd August 2013, 12:52   #611
Senior - BHPian
 
anujmishra's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 1,290
Thanked: 492 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post
Ok, smiley limit reached.


Quote:
Originally Posted by Samurai View Post
You remind me of my boss from 1991 who insisted we all use unsigned variables and pass parameters via global variables instead of stack.
I didn't get you. How your boss' example of passing parameter is relevant to this discussion? Otherwise, many times whatever your boss said works very well.

Quote:
Originally Posted by Samurai View Post
Instead of all this analysis, you could have just asked me why I used long long here.
I need not to ask anybody also excuses does not works everywhere. I just wanted to give basic mistakes programmers make. For such demo code, you could have used normal C++ syntax than VS one. Moreover, long long is also not part of C++, it is just compiler extension. I have seen numerous examples like this. If at the max in an array only 10 data will be filled, still it is declared as array of 100.



Quote:
Originally Posted by Samurai View Post
My world is different. Simple/standard instructions are always optimized by the compiler before the binary is generated. So I don't bother about micro optimization. But the compiler can't optimize the design. So I focus more on design.
What do you do, when your program take 10 seconds to load in memory? In that case, would compiler optimization helps on which you rely? After some point design will not need much attention rather performance, load time, frequent io usage, cache etc. matters.


PS: I feel this thread is going on wrong direction and everyone here started proving that they are fit to get employment in Indian Tech Companies by writing code, re-writing code, using heavy techie words, ultra optimization etc. In lighter sense, thread owner is not asking how much you & I know, his intention is general.

Last edited by anujmishra : 22nd August 2013 at 12:53.
anujmishra is offline  
Old 22nd August 2013, 13:50   #612
Team-BHP Support
 
Samurai's Avatar
 
Join Date: Jan 2005
Location: Bangalore/Udupi
Posts: 25,813
Thanked: 45,434 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by anujmishra View Post
I didn't get you. How your boss' example of passing parameter is relevant to this discussion? Otherwise, many times whatever your boss said works very well.
Made the code less readable, and lot less modular.

Quote:
Originally Posted by anujmishra View Post
I need not to ask anybody also excuses does not works everywhere. I just wanted to give basic mistakes programmers make. For such demo code, you could have used normal C++ syntax than VS one. Moreover, long long is also not part of C++, it is just compiler extension. I have seen numerous examples like this. If at the max in an array only 10 data will be filled, still it is declared as array of 100.

What do you do, when your program take 10 seconds to load in memory? In that case, would compiler optimization helps on which you rely? After some point design will not need much attention rather performance, load time, frequent io usage, cache etc. matters.
Really? Using long long adds 10 seconds more? Anyway, if my earlier explanation didn't satisfy you, then I suppose I am an evasive programmer.

Quote:
Originally Posted by anujmishra View Post
PS: I feel this thread is going on wrong direction and everyone here started proving that they are fit to get employment in Indian Tech Companies
So why are you doing it?

Quote:
Originally Posted by anujmishra View Post
In lighter sense, thread owner is not asking how much you & I know, his intention is general.
What thread owner? You mean the thread starter? He started the thread by quoting a news article, about MBAs working as clerks and about Toyota attempt to make ITI trained technicians more employable . Then I responded...

Quote:
Originally Posted by Samurai View Post
I don't know about MBA graduates, but I would put the employability of fresh IT graduates of Karnataka at 10%, not more.
Then everybody started talking about employability of IT graduates. Two years later I renamed the thread to the current title. Even I don't remember the original title. In fact, the thread starter's post is now OT, it must be moved to a MBA or Industrial training thread. Not a priority though.

This thread is discussing competency of technical graduates, so occasional technical discussion is normal. It just shows that we are passionate about it. We will return to the depressing topic of IT graduates as always.
Samurai is offline  
Old 22nd August 2013, 14:07   #613
Senior - BHPian
 
anujmishra's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 1,290
Thanked: 492 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post
Really? Using long long adds 10 seconds more? Anyway, if my earlier explanation didn't satisfy you, then I suppose I am an evasive programmer.
There was paragraph change and context change clearly. Hope you relook again the previous post. Later part, I was discussing about whole performance not about long long.

Never I said, you are evasive programmer. I just quoted this as generic example of mistakes committed by many techies.

I was just trying to say that, renting a big wearhouse is not necessary for parking Maruti 800. Hope you got my point.

Quote:
Originally Posted by Samurai View Post
So why are you doing it?
Well, I did not write any code here, to prove my programming ability.


Quote:
Originally Posted by Samurai View Post
This thread is discussing competency of technical graduates, so occasional technical discussion is normal. It just shows that we are passionate about it. We will return to the depressing topic of IT graduates as always.
Well, this is healthy topic to discuss.
anujmishra is offline  
Old 22nd August 2013, 14:23   #614
Senior - BHPian
 
msdivy's Avatar
 
Join Date: Aug 2006
Location: Bangalore
Posts: 1,815
Thanked: 2,826 Times
re: IT Industry and Employability of Technical Graduates

Quote:
Originally Posted by Samurai View Post
Since you tried, I'll have to put a C++ version.
I made a C version (to simplify getting assembly code):

#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 100; i++)
{
i%15 ? (i%5 ? (i%3 ? printf("%d\n", i) : printf("FizzBuzz\n")) : printf("Buzz\n")) : printf("FizzBuzz\n");
}
return 0;
}
Quote:
Originally Posted by amitk26 View Post
Well there is some merit in the readability argument. It depends on the overall coding environment.
....
In any practical code always the conditional clauses are too big and it is easy to misplace a brace and create hard to debug issue.
I removed the ternary operator & used if else. I find this code to be more readable, more maintainable and in future even an intern (which is a possibility) can add a new condition & extend the code.

#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 100; i++)
{
if(i%15 == 0)
printf("FizzBuzz\n");
else if(i%5 == 0)
printf("Buzz\n");
else if(i%3 == 0)
printf("Fizz\n");
else
printf("%d\n", i);
}
return 0;
}

Note: Checking the assembly of either of code blocks (for instance here in http://assembly.ynh.io/ in GCC), the code without ternary operator produces 8 less assemble instructions !
So the moral is give preference to code readability & extensibility before other things.
msdivy is offline  
Old 22nd August 2013, 14:46   #615
Senior - BHPian
 
Join Date: Jul 2008
Location: Bangalore
Posts: 2,089
Thanked: 715 Times
re: IT Industry and Employability of Technical Graduates

Thanks MsDivy for experientially verifying it

Only if SQA and other qualityauditing folks who are responsible to push quality guidelines in companies do these kinds of experimental persuasion rather then parroting the guideline to young people it will be of great help.
amitk26 is offline  
Reply

Most Viewed
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Copyright ©2000 - 2024, Team-BHP.com
Proudly powered by E2E Networks