Easier to read than one line of code?!



  • Sometimes, I just want to actually put that "Bang Head Here" sign on my desk, particularly when I'm looking at some code that the programmer knows isn't particularly right!!!

    <font face="courier new,courier">     ' I know you could + 1 - but easier to read here !!
    </font>

    <font face="courier new,courier">     Select Case Me.SSNPControl.Tab
            Case 0: MyTab = 1
            Case 1: MyTab = 2
            Case 2: MyTab = 3
            Case 3: MyTab = 4
            Case 4: MyTab = 5
            Case 5: MyTab = 6
            Case 6: MyTab = 7
            Case 7: MyTab = 8
            Case 8: MyTab = 9
            Case 9: MyTab = 10
            Case 10: MyTab = 11
            Case 11: MyTab = 12
        End Select</font>



  • heh, guilty as charged. One of my latest marvels, in Classic ASP (don't ask...)

    ' Run loop

    if 1=1 then ' Sorry about that, but this way, you can fold it in UltraEdit

    <Do the run logic, about 300 lines...>

    end if



  • @steenbergh said:

    if 1=1 then ' Sorry about that, but this way, you can fold it in UltraEdit

    God bless languages that allow you to put a begin...end block everywhere.



  •  May I suggest a default case? Maybe something like:

    MyTab = MyTab + 1

     ?



  • @Fabian said:

     May I suggest a default case? Maybe something like:

    MyTab = MyTab + 1

     ?

    I'd recommend this:

    <font face="courier new,courier">Case Else: MyTab = Me.SSNPControl.Tab + 1</font>



  • @steenbergh said:

    heh, guilty as charged. One of my latest marvels, in Classic ASP (don't ask...)

    ' Run loop

    if 1=1 then ' Sorry about that, but this way, you can fold it in UltraEdit

    <Do the run logic, about 300 lines...>

    end if

    I use GVim (even in Windows) and fold on indention level.  You can always just indent a block of code to fold it (unless you're working in Python, but then your code should already be folding appropriately)


  • @Fabian said:

     May I suggest a default case? Maybe something like:

    MyTab = MyTab + 1

     ?

     

     

    That's exactly what it now reads... with the following comment above it:

    ' Nick - Check http://forums.thedailywtf.com/forums/t/14558.aspx about this next line of code :-)



  •  @steenbergh said:

    heh, guilty as charged. One of my latest marvels, in Classic ASP (don't ask...)

    ' Run loop

    if 1=1 then ' Sorry about that, but this way, you can fold it in UltraEdit

    <Do the run logic, about 300 lines...>

    end if

     

    I'll ask why you didn't move the 300 lines into their own subroutine.

     



  • Hey, I once worked with a Ruby fanboy who kept saying he could do everything with less lines than with C# or Java. I once checked his code, and found something like...

    SomeObject.Method1().Method2(someParameter).Method3().Method4.Method5(anotherParameter). /* snip */ ...MethodN()

    ...In his code. All in all that line had almost 800 characters. So much for doing something multi-step task with just one line of code.



  • I'm just speculating, but this might be a piece of code which controls the tab order between some kind of input fields, in which case writing it out this way allows you to change the tab order if you wanted. The comment is odd, though. Perhaps written by someone else entirely, based on some third person's unclear explanation of what it does? "This makes it easier to change the tab order..." --> simple, forgetful mind --> "This is easier than adding 1."



  • [i]You want it in one line? Does it have to fit in 80 columns? :-)[/i]

    Larry Wall



  • @emurphy said:

     @steenbergh said:

    heh, guilty as charged. One of my latest marvels, in Classic ASP (don't ask...)

    ' Run loop

    if 1=1 then ' Sorry about that, but this way, you can fold it in UltraEdit

    <Do the run logic, about 300 lines...>

    end if

     

    I'll ask why you didn't move the 300 lines into their own subroutine.

     

     

    Subroutines are for reusability. If you have a part of code of 300 lines, or even of 10,000 lines, which is only used in once place, you should not put in its own subroutine, it will degrade performance. Unless your language has support for inline, of course, but that's a work-around for this.



  • @pbean said:

    Subroutines are for reusability. If you have a part of code of 300 lines, or even of 10,000 lines, which is only used in once place, you should not put in its own subroutine, it will degrade performance. Unless your language has support for inline, of course, but that's a work-around for this.

    Hell yeah, who cares about readability? If putting your 10K lines into a function increases the runtime by a femtosecond, then it is clearly not worth doing it just to make the code easier to maintain.

    Not.



  • @pbean said:

    Subroutines are for reusability. If you have a part of code of 300 lines, or even of 10,000 lines, which is only used in once place, you should not put in its own subroutine, it will degrade performance. Unless your language has support for inline, of course, but that's a work-around for this.
     

     The overhead for calling a function/subroutine is so minimal you should be punished for even thinking of it (unless ofcourse you do it 10 billion times, then you should inline indeed). You can also use subroutines to improve source readability, like instead of typing:

    blah();
    blah();
    blah();
    // Continue for x lines

     You can just say:

    do_something();

    What reads better do you think?...

    Didn't you read the project manual for your latest project? quote: "Keep your routines short, 30 linux max."



  • @pbean said:

    Subroutines are for reusability. If you have a part of code of 300 lines, or even of 10,000 lines, which is only used in once place, you should not put in its own subroutine, it will degrade performance. Unless your language has support for inline, of course, but that's a work-around for this.

    A card carrying member of the Cargo Cult of Programming, I see. Your username seems apt.



  •  So a poor man's version of:

    #region Run Logic

    #endregion

    In C#?



  • @blakeyrat said:

     So a poor man's version of:

    #region Run Logic

    #endregion

    In C#?



    and the similar option for VS's version of c++

    #pragma region Run Logic

    #pragma endregion



  • @MeesterTurner said:

    Sometimes, I just want to actually put that "Bang Head Here" sign on my desk, particularly when I'm looking at some code that the programmer knows isn't particularly right!!!

    Surely you should put that sign on his desk?  Like, just before slamming his head into it?


  • @pbean said:

    @emurphy said:

     @steenbergh said:

    heh, guilty as charged. One of my latest marvels, in Classic ASP (don't ask...)

    ' Run loop

    if 1=1 then ' Sorry about that, but this way, you can fold it in UltraEdit

    <Do the run logic, about 300 lines...>

    end if

     

    I'll ask why you didn't move the 300 lines into their own subroutine.

     

     

    Subroutines are for reusability. If you have a part of code of 300 lines, or even of 10,000 lines, which is only used in once place, you should not put in its own subroutine, it will degrade performance. Unless your language has support for inline, of course, but that's a work-around for this.

    If you value the performance increase offered by not making one function call for a 300 line block of code over the readability of your source code, I don't want to maintain your stuff.  At worst making it a function may introduce a 10% overhead if all 300 lines are simple XORs or something else that can be done in one assembly instruction.  Most likely the function call will have an unmeasurably small impact on performance.

    If you work in a domain where a 1% increase in performance is important, then, by all means, avoid the function.  But it makes horrible general programming advice.



  • @pbean said:

    Subroutines are for reusability. If you have a part of code of 300
    lines, or even of 10,000 lines, which is only used in once place, you
    should not put in its own subroutine, it will degrade performance.
    Unless your language has support for inline, of course, but that's a
    work-around for this.

    Is my sarcasm detector getting false positives or did you guys decide to take it seriously to protect a generation of innocent programmers?



  • @blakeyrat said:

     So a poor man's version of:

    #region Run Logic

    #endregion

    In C#?

     Yep, that's what I use it for. There's no such thing for Classic ASP, so you're stuck with this...

    And it isn't in a subroutine, because this code needs to run every time the page gets called. Sure you can define a sub RunThisStuff() and paste all this code inside, then call the sub. I however believe the differences between both ways rather insignificant.



  • @dtech said:

    Didn't you read the project manual for your latest project? quote: "Keep your routines short, 30 linux max."

     

    Yeah, you don't want too many of those nasty linux.



  • @pbean said:

    Subroutines are for reusability. If you have a part of code of 300 lines, or even of 10,000 lines, which is only used in once place, you should not put in its own subroutine, it will degrade performance. Unless your language has support for inline, of course, but that's a work-around for this.

    Subroutines are also for profiling, at least in some languages.  If you have everything in one big mass, it's difficult to tell where the most resource intensive stuff is without a lot of expertise.  And, everyone I've talked to with that expertise would far rather just run the code through the profiler, so they don't have to look at the 95% of the code that accounts for less than 5% of the (unoptimized) runtime.

    Yes, you *can* profile the stuff in one huge mass, but that requires putting benchmark code all over the place.  Not only is that ugly, it also degrades your performance, possibly worse than the subroutines that are only called once.

    Also, I've yet to encounter a 10k single routine block of code that didn't contain repetitive code that should've been in a subroutine.  However, when it's in a 10k+ mass, it can be difficult to spot (at least, for those who write such monstrosities.)



  • @tgape said:

    Subroutines are also for profiling, at least in some languages.  If you have everything in one big mass, it's difficult to tell where the most resource intensive stuff is without a lot of expertise.  And, everyone I've talked to with that expertise would far rather just run the code through the profiler, so they don't have to look at the 95% of the code that accounts for less than 5% of the (unoptimized) runtime.
    Testing is another case where breaking up the code becomes beneficial.


Log in to reply