Short Cut Code

View previous topic View next topic Go down

Short Cut Code

Post by BIT0102-Mohaimin on Tue Mar 02, 2010 9:35 pm

Readability of any text depends on the reader's mastery on the topic. While writing something, we should keep in mind that for whom we are writing.
The same is for codes. As we are going to be professional, our codes should be professional as well.
In this topic, I will post some examples of shortening codes.
I expect some examples from you guys also.

So... Lets start, minimise your codes, and make it more readable, more beautiful, more professional...
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Short Cut Code

Post by BIT0122-Amit on Tue Mar 02, 2010 11:03 pm

Razz nice topic. but I am confused.
What is the definition of shortening codes I wonder.

I am waiting for some examples which will clear up myconfusion study

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Short Cut Code

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 12:21 am

Here is the 1st example, very simple one, finding smallest of some few numbers...


Last edited by BIT0102-Mohaimin on Wed Mar 03, 2010 12:26 am; edited 1 time in total
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Short Cut Code

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 12:23 am

Code:
   /* Smaller between two */
   int getMin1(int a, int b) {
      if (a < b) {
         return a;
      } else {
         return b;
      }
   }

   // The else was redundant, kick it out...
   int getMin2(int a, int b) {
      if (a < b) {
         return a;
      }

      return b;
   }

   // Why not use question operator...
   // If you have problem in question operator, follow this link:
   /* http://iitdu.forumsmotion.com/others-f52/question-operator-t480.htm#1275 */
   int getMin3(int a, int b) {
      int min = a < b ? a : b;
      return min;
   }

   // Again, No need of that variable min
   int getMin4(int a, int b) {
      return a < b ? a : b;
   }

   /* Smallest of 3 */
   int getMinOfThree1(int a, int b, int c) {
      int min = a < b ? a : b;
      return min < c ? min : c;
   }

   // Again, no need to use an extra variable
   int getMinOfThree2(int a, int b, int c) {
      a = a < b ? a : b; // Smaller one is stored in a
      return a < c ? a : c;
   }

   // Now, check this one...
   int getMinOfThree0(int a, int b, int c) {
      return a < b ? (a < c ? a : c) : (b < c ? b : c);
      /*
       * Nested Question Operator is used here, each individual operations are
       * enclosed between parentheses
       */
   }


Last edited by BIT0102-Mohaimin on Wed Mar 03, 2010 1:15 am; edited 1 time in total
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Short Cut Code

Post by BIT0122-Amit on Wed Mar 03, 2010 12:45 am

Hey hey :p
I am requesting to use the "[ code]" and "[/code]" to bracket Programming codes.

that way, the indentations are stored. and are easier to understand.
Can you please edit the previous post and use the code bracket?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Short Cut Code

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 1:10 am

Yap, of course... [vuila gecilam]
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Short Cut Code

Post by BIT0122-Amit on Wed Mar 03, 2010 2:49 am

Thanks Smile it is much more understandable now.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Short Cut Code

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum