Results 1 to 5 of 5

Thread: help making code more concise and shorter

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    6

    help making code more concise and shorter

    I have a calculator ive made and it works and everything and you can add, subtract, divide up to 8 numbers like say put in the 1st number then 2nd etc...
    but its almost 1,000 lines of code and im trying to think of a way to shorten it i know theres an easier way i just need to know how?

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    6

    Re: help making code more concise and shorter

    So there is button 1 click that fires an event which tells a varible countclicks to add everytime its clicked...

    countclicks +=1

    if countclicks = 1 then
    firstnumber = 1
    end if

    if countclicks = 2 then
    firstnumber = 11
    end if

    and it keeps going all the way up to 1111111111

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: help making code more concise and shorter

    It is hard to tell due to the lack of info/code you are sharing... but if I have made the right assumptions, this single line will do the same as that section of code:
    Code:
    firstnumber = (firstnumber * 10) + 1

  4. #4
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: help making code more concise and shorter

    You haven't provide much information but if I understand what you are trying to do correctly this should work.

    Assuming the first number was 1, the following code would make it 11

    Code:
     firstnumber = firstnumber & 1
    The following code would make it 12

    Code:
     firstnumber = firstnumber & 2
    Putting "&" just adds the new number to the end of the current number

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: help making code more concise and shorter

    While that is slightly simpler than my version, it has some significant issues.

    Those issues are based around the fact that & works with strings, which means that there need to be 3 data type conversions (firstnumber to a string, 1 or 2 to a string, and the result back to the data type of firstnumber).

    Data type conversions are dubious due to the fact that various assumptions need to be made (which can cause bugs and/or errors depending on the settings of the computer that the code is running on, and the exact data being converted, and what types it is being converted to/from), so it is generally best to avoid them when you can.

    For code that needs to run quickly, the conversions will slow things down, perhaps significantly. In this particular case the speed probably isn't an issue though.

    Due to the above reasons (and others that aren't as important), for many people the code will not be allowed to run at all - because use of Option Strict stops you from using implied type conversions (to let you know where bugs are likely to appear, so you can fix them before they ever occur).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width