• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 01:54
CEST 07:54
KST 14:54
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL21] Ro8 Preview Pt2: Progenitors8Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun13[ASL21] Ro8 Preview Pt1: Inheritors16[ASL21] Ro16 Preview Pt2: All Star10Team Liquid Map Contest #22 - The Finalists22
Community News
Weekly Cups (April 27-May 4): Clem takes triple0RSL Revival: Season 5 - Qualifiers and Main Event11Code S Season 1 (2026) - RO12 Results12026 GSL Season 1 Qualifiers25Maestros of the Game 2 announced9
StarCraft 2
General
Weekly Cups (April 27-May 4): Clem takes triple Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Code S Season 1 (2026) - RO12 Results Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun Team Liquid Map Contest #22 - The Finalists
Tourneys
RSL Revival: Season 5 - Qualifiers and Main Event StarCraft Evolution League (SC Evo Biweekly) 2026 GSL Season 2 Qualifiers Sparkling Tuna Cup - Weekly Open Tournament $1,400 SEL Season 3 Ladder Invitational
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players [M] (2) Frigid Storage
External Content
Mutation # 524 Death and Taxes The PondCast: SC2 News & Results Mutation # 523 Firewall Mutation # 522 Flip My Base
Brood War
General
[ASL21] Ro8 Preview Pt2: Progenitors ASL21 General Discussion Why there arent any 256x256 pro maps? BW General Discussion BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[ASL21] Ro8 Day 3 [ASL21] Ro8 Day 4 [Megathread] Daily Proleagues [ASL21] Ro8 Day 2
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates What's the deal with APM & what's its true value Any training maps people recommend?
Other Games
General Games
Dawn of War IV Stormgate/Frost Giant Megathread OutLive 25 (RTS Game) Daigo vs Menard Best of 10 Nintendo Switch Thread
Dota 2
The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread European Politico-economics QA Mega-thread 3D technology/software discussion Canadian Politics Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread Formula 1 Discussion McBoner: A hockey love story
World Cup 2022
Tech Support
streaming software Strange computer issues (software) [G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Movie Stars In Video Games: …
TrAiDoS
ramps on octagon
StaticNine
Broowar part 2
qwaykee
Funny Nicknames
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1270 users

The Big Programming Thread - Page 538

Forum Index > General Forum
Post a Reply
Prev 1 536 537 538 539 540 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
October 27 2014 17:52 GMT
#10741
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()


Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.
There is no one like you in the universe.
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
Last Edited: 2014-10-27 18:09:24
October 27 2014 18:02 GMT
#10742
My opinion on that is: whenever you feel the need to put a comment inside a method, you should extract that code into a new method with "the comment as it's name" (you know what I mean).

Sometimes it doesn't work so nicely because the new method would need too many parameters. In that case there's usually still hope that there is a different, better approach to the whole algorithm, which in turn allows for better method extractions.

I really don't care too much about reuse of my methods. I do avoid duplication, of course. If one of my small methods happens to be reusable, great. I also frequently apply a series of inline and extract method refactorings whenever I see some duplicate behavior but my methods aren't quite ready for the situation yet.
If you have a good reason to disagree with the above, please tell me. Thank you.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2014-10-27 18:18:00
October 27 2014 18:14 GMT
#10743
On October 28 2014 02:52 Blisse wrote:
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()


Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.


I know that our compiler will inline most functions if it's called under an arbitrary 5 times. So this sort of abstraction has little actual actual effect on code-size. Personal preference then, I guess. There are valid arguments for both styles.

The only correct argument is to be consistent with the practices of nearby code.

On October 28 2014 03:02 spinesheath wrote:
My opinion on that is: whenever you feel the need to put a comment inside a method, you should extract that code into a new method with "the comment as it's name" (you know what I mean).

Sometimes it doesn't work so nicely because the new method would need too many parameters. In that case there's usually still hope that there is a different, better approach to the whole algorithm, which in turn allows for better method extractions.

I really don't care too much about reuse of my methods. I do avoid duplication, of course. If one of my small methods happens to be reusable, great. I also frequently apply a series of inline and extract method refactorings whenever I see some duplicate behavior but my methods aren't quite ready for the situation yet.


+1 for the "comment as it's name".

My personal favorite coding standard is from https://github.com/contiki-os/contiki/blob/master/doc/code-style.c

functions or variables are prepended with the name of the file it comes from, as well, so you can navigate the code-base without a heavy IDE fairly easily.

Any sufficiently advanced technology is indistinguishable from magic
StatixEx
Profile Blog Joined August 2011
United Kingdom779 Posts
Last Edited: 2014-10-27 19:31:05
October 27 2014 19:28 GMT
#10744
thank you for the advice to a question i posted in page 537.

What im looking for is something graphical and impressive with VERY straight forward to understand code. Ive been programming for the best part of 15 years now and know how long i spent practicing code and exploring and having some of the most accomplished feelings in my life from it. Ive decided to settle down a bit and took a teaching job from games design and stuff like that and feel extremely frustrated with the 11-16yr olds of today. I teach computing of course, our schools most advanced accomplishment is a nested if in excel . . . or a vlookup(rest of staff think they are genius programmers as well when they actually put it up in board and know what the arguments are . . i digress)and even then ive never seen anyone write it out without there being mistakes which ive had to fix in the end anyway.

so ,what am i saying and asking

I tried for years to use flash with them, AS2 was good! AS3 took it all away making it harder to just write scripts into actual objects, now they have to know how to program to an extent and use calls from imports to make anything work the way it did! I personally think its what AS was waiting for, but for the kid . . . nope!

i saw a comp on some site called program a game in a day and some dude made a really good gauntlet/zelda like game using python. I saw the code and thought this isnt so much! kids might get this!

I copied the code, took out everything i didn't need . . infact all i had was a guy running around on some grass and there was a chest you could open. but the [ for x, for y loop into the if x in y ] to draw the tiles threw the kids instantly. they couldnt get their head round a 2d array kind of setup and how the tiles were being drawn to the screen, then came the main entry loop which . . . .well it was over when we got to this bit. Most of the kids had a hard time copying the functions at the start properly.

So, again what am i asking . . not sure but no matter what i try to do, construct of programs seem to get heavy too fast . . .well i say this but i find it insanely straight forward and easy . . .the kids dont get it. All i can do in my lesson in teach them trivial examples, explain whats happening and there we go . . . looks like they are getting it. They cant seem to put this knowledge and practice outside a simple, get a program to output your name, enter a number and display that as your age kind of shit!

Im going to try a class based approach next week and develop something really dumb like a dishwasher . . . god teaching man, kids DONT want to go away and do any extra work or research, if they arent playing COD made by them in 12 minutes its over


spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
Last Edited: 2014-10-27 20:10:32
October 27 2014 20:09 GMT
#10745
"11-16 year olds today" is something countless generations before you have complained about.

Not everyone is talented at or even interested in programming and while I think that most people can learn to program at a moderate level given enough time, I doubt you'll have enough of that. Shouldn't your goal be to teach your students the very basics and then support those who are interested and determined enough to learn more than what can be taught at school?

Just like performance optimization: don't waste development time on random parts of the code. Keep everything reasonable, then profile and focus on the parts that have the most potential for optimization.
If you have a good reason to disagree with the above, please tell me. Thank you.
LaNague
Profile Blog Joined April 2010
Germany9118 Posts
October 27 2014 21:06 GMT
#10746
whats your class, what type of school and is the class mandatory or do they sign up for it?
Important things to know.

mandatory basic excel class in a low education school is different than a course where people can opt out of in a high education school.




In my school we did a program for live predictions for city elections amongst other things, i think a later course did their own browsergame.
But those were optional courses only for people who wanted to learn more about programming, 80% of my school would have been helpless in there, just as i would have been helpless in a painting art class.

Manit0u
Profile Blog Joined August 2004
Poland17743 Posts
Last Edited: 2014-10-27 21:12:53
October 27 2014 21:11 GMT
#10747
Has anyone here ever used the pimcore CMS? I've started working for a new company at the end of last week, got assigned some issues pertaining their project using pimcore, which was set up and solely governed by their former employee. No one knows exactly how it works and I've spent past 2 workdays figuring it all out.

Today I've closed 2 issues, which weren't real issues (client got confused) but found some other things that were disturbing:
1. The code that's on the production server doesn't figure on any of the git branches, it's way ahead of master and all the others and there's no way to push it from the production server...
2. While investigating the code and doing some quick fixes for some real issues (had to do it via vim and ssh console) I've discovered that debug.log file has grown to over 200MB over the course of past 2 weeks, most of which were SQL-based errors (majority of them seemed to be state 2002 - unable to open connection to socket), obviously the file was too large for me to parse thoroughly. Investigating php.log revealed some fatal errors involving PDO exceptions... The debug.log is now growing approximately 1MB/hour since people are beginning to fill in content and test it and I have no idea how to handle it in this unfamiliar environment.

Any ideas where to start fixing this? I'm currently looking for a way to disable debug log entirely as the temporary measure, until I figure out what the next course of action should be.
Time is precious. Waste it wisely.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
October 27 2014 21:33 GMT
#10748
--- Nuked ---
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2014-10-27 22:38:16
October 27 2014 21:35 GMT
#10749
On October 28 2014 06:11 Manit0u wrote:
Has anyone here ever used the pimcore CMS? I've started working for a new company at the end of last week, got assigned some issues pertaining their project using pimcore, which was set up and solely governed by their former employee. No one knows exactly how it works and I've spent past 2 workdays figuring it all out.

Today I've closed 2 issues, which weren't real issues (client got confused) but found some other things that were disturbing:
1. The code that's on the production server doesn't figure on any of the git branches, it's way ahead of master and all the others and there's no way to push it from the production server...
2. While investigating the code and doing some quick fixes for some real issues (had to do it via vim and ssh console) I've discovered that debug.log file has grown to over 200MB over the course of past 2 weeks, most of which were SQL-based errors (majority of them seemed to be state 2002 - unable to open connection to socket), obviously the file was too large for me to parse thoroughly. Investigating php.log revealed some fatal errors involving PDO exceptions... The debug.log is now growing approximately 1MB/hour since people are beginning to fill in content and test it and I have no idea how to handle it in this unfamiliar environment.

Any ideas where to start fixing this? I'm currently looking for a way to disable debug log entirely as the temporary measure, until I figure out what the next course of action should be.


put up a cron job to compress the log (it should compress very nicely) and archive it for later viewing.

Save the most recent few logs from the last week or so into a folder, and remove older logs if the overall folder gets too big. Every week or so, save a log as an archived log to make sure you have snapshots from common errors around that time from production.


Just some ideas. If you don't think the log will be useful at all, you can just remove the file and pipe it straight to /dev/null by putting a link there.


EDIT: And if you're asking how to actually deal with the errors that come up, start with the most egregious sounding error and slowly but surely work your way until the debug log actually has meaning.

EDIT #2: It is probably worth mentioning that the server generating the logs should probably get a good profiling to make sure all that logging isn't slowing down it's response.
Any sufficiently advanced technology is indistinguishable from magic
Shenghi
Profile Joined August 2010
167 Posts
October 27 2014 21:57 GMT
#10750
On October 28 2014 02:52 Blisse wrote:
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()


Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.

In the second case, the functions should be ordered so they can be read from top to bottom; the first one to be called at the top, and then whichever comes next. In this case: main, followed by readCases, followed by doCase.
People are not born stupid, they choose to be stupid. If you made that choice, please change your mind.
Days
Profile Joined February 2010
United States219 Posts
October 28 2014 00:16 GMT
#10751
My fellow nerds! I need your help. I have a C# application due soon and I am stumped. Basically I have to make a simplified version of Paint. What i'm doing for now is drawing Ellipses, Rectangles, and Triangles based on mouse movement. It works pretty nicely, except that when one shape is drawn, the one before that disappears. I know i'm suppose to be using some type of ArrayList to store shapes, but since I have 3 different shapes I am confused as to how to go about that. Here is my code where I add rectangles to the array. (The rectangle is working, but the ellipse and triangles are not being stored)

P.S. Just to not confuse you guys, any code inside the Using(graphics) in my MouseDown event is just to check if a right click falls inside of a created shape. This is in order to open a Properties Dialog that is suppose to be able to change the rotation, location, text, size,width of each drawn shape. I have yet to pass the values, that will be my next monster to tackle.


private void mainFrm_MouseDown(object sender, MouseEventArgs e)
{

initialY = e.Y;
initialX = e.X;
isMoving = true;
using (Graphics g = this.CreateGraphics())
{
if (e.Button == MouseButtons.Right)
{
Point[] ptClick = new Point[] { new Point(e.X, e.Y) };
g.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, ptClick);
foreach (Rectangle rect in rectangles)
{
if (rect.Contains(ptClick[0]))
{
options.Show();
}
}
}
}
//this.Invalidate();
}

private void mainFrm_MouseUp(object sender, MouseEventArgs e)
{
isMoving = false;
count++;
//rectangle = new Rectangle(movedX, movedY, width, height);
if (rectangleToolStripMenuItem.Checked == true)
{
rectangles.Add(new Rectangle(movedX, movedY, width, height));
//rectangle = new Rectangle(movedX, movedY, width, height);
}
else if (ellipseToolStripMenuItem.Checked == true)
{
ellipses.Add(new Rectangle(movedX, movedY, width, height));

}
this.Invalidate();
}

private void mainFrm_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving == true)
{
//movedX = e.X - initialX;
//movedY = e.Y - initialY;
triangleX = e.X;
triangleY = e.Y;
movedY = Math.Min(initialY, e.Y);
movedX = Math.Min(initialX, e.X);
width = Math.Max(initialX, e.X) - Math.Min(initialX, e.X);
height = Math.Max(initialY, e.Y) - Math.Min(initialY, e.Y);
rectangles.Insert(0, new Rectangle(movedX, movedY, width, height));

}
this.Invalidate();
}
We buy things we don't need, with money we don't have, to impress people we don't like.
delHospital
Profile Blog Joined December 2010
Poland261 Posts
Last Edited: 2014-10-28 00:42:21
October 28 2014 00:17 GMT
#10752
On October 28 2014 02:52 Blisse wrote:
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)

+ Show Spoiler +


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()



Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.
My advice would be this:

When you split up a function, do it bottom up, not top down. That is, don't cut the function in half, and
def big_f():
f1()
f2()
That doesn't really achieve anything. Instead, try to find small small chunks of functionality that can be meaningful on their own, and extract them from the big function. Those will usually take arguments, process them, and return the result, possibly doing a little bit of IO along the way. If you went with the "cut in half" approach, you would have arrived at two weird looking procedures that take nothing, and return nothing.

To elaborate a bit more:

If you refactor
# does this and that
def main():
...
into
# does this
def this():
...

# (possibly hundreds of lines below, or in a different file)
# does that
def that():
...

def main():
this()
that()

Then it becomes easier to see what main() is about, but it comes with a price: that() becomes harder to reason about, as it depends on the state set up by this().

If you instead refactor it like this:
def f(a, b):
# (play with a and b to get a result)
return result

def g(a, b):
...
return result

def main():
# (code that uses f() and g())

Then, as long as f() and g() are sensible, everything becomes smaller and easier to grasp.
Manit0u
Profile Blog Joined August 2004
Poland17743 Posts
Last Edited: 2014-10-28 08:47:09
October 28 2014 08:46 GMT
#10753
On October 28 2014 09:17 delHospital wrote:
Show nested quote +
On October 28 2014 02:52 Blisse wrote:
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)

+ Show Spoiler +


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()



Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.
My advice would be this:

When you split up a function, do it bottom up, not top down. That is, don't cut the function in half, and
def big_f():
f1()
f2()
That doesn't really achieve anything. Instead, try to find small small chunks of functionality that can be meaningful on their own, and extract them from the big function. Those will usually take arguments, process them, and return the result, possibly doing a little bit of IO along the way. If you went with the "cut in half" approach, you would have arrived at two weird looking procedures that take nothing, and return nothing.

To elaborate a bit more:

If you refactor
# does this and that
def main():
...
into
# does this
def this():
...

# (possibly hundreds of lines below, or in a different file)
# does that
def that():
...

def main():
this()
that()

Then it becomes easier to see what main() is about, but it comes with a price: that() becomes harder to reason about, as it depends on the state set up by this().

If you instead refactor it like this:
def f(a, b):
# (play with a and b to get a result)
return result

def g(a, b):
...
return result

def main():
# (code that uses f() and g())

Then, as long as f() and g() are sensible, everything becomes smaller and easier to grasp.


I think that's bad. It's way more convenient to have your main function definition at the top and all the helper function definitions below that. The reason is, while inspecting the file you instantly get to the gist of it and if functions have good names you can instantly tell what it does and only have to look up the helper functions if you really need the specifics. Reading random chunks of code doesn't help you understand what the program is actually doing and what its end goal is, you only get that from reading the main function definition. That's why I think it should be the topmost thing.

TL;DR - abstract --> specific is the way to go, not the other way around.
Time is precious. Waste it wisely.
delHospital
Profile Blog Joined December 2010
Poland261 Posts
Last Edited: 2014-10-28 09:28:06
October 28 2014 09:27 GMT
#10754
On October 28 2014 17:46 Manit0u wrote:
Show nested quote +
On October 28 2014 09:17 delHospital wrote:
On October 28 2014 02:52 Blisse wrote:
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)

+ Show Spoiler +


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()



Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.
My advice would be this:

When you split up a function, do it bottom up, not top down. That is, don't cut the function in half, and
def big_f():
f1()
f2()
That doesn't really achieve anything. Instead, try to find small small chunks of functionality that can be meaningful on their own, and extract them from the big function. Those will usually take arguments, process them, and return the result, possibly doing a little bit of IO along the way. If you went with the "cut in half" approach, you would have arrived at two weird looking procedures that take nothing, and return nothing.

To elaborate a bit more:

If you refactor
# does this and that
def main():
...
into
# does this
def this():
...

# (possibly hundreds of lines below, or in a different file)
# does that
def that():
...

def main():
this()
that()

Then it becomes easier to see what main() is about, but it comes with a price: that() becomes harder to reason about, as it depends on the state set up by this().

If you instead refactor it like this:
def f(a, b):
# (play with a and b to get a result)
return result

def g(a, b):
...
return result

def main():
# (code that uses f() and g())

Then, as long as f() and g() are sensible, everything becomes smaller and easier to grasp.


I think that's bad. It's way more convenient to have your main function definition at the top and all the helper function definitions below that. The reason is, while inspecting the file you instantly get to the gist of it and if functions have good names you can instantly tell what it does and only have to look up the helper functions if you really need the specifics. Reading random chunks of code doesn't help you understand what the program is actually doing and what its end goal is, you only get that from reading the main function definition. That's why I think it should be the topmost thing.

TL;DR - abstract --> specific is the way to go, not the other way around.

I'm not talking about how functions should be grouped into modules or about the ordering of function definitions in a file. Yes, putting the helper functions below is probably a good idea, but that's not what my post was about. I was saying that you should primarily extract pure-ish, reusable-ish functions that can be reasoned about in isolation, and that simply dividing a procedure into several smaller ones gains you much less.
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
October 28 2014 09:37 GMT
#10755
When I want to cut a method into parts, I try to describe what the method does in 2-4 steps and then make those steps my methods. That way I can read the original method as a series of 2-4 steps and then look deeper if I need details about one step. Good steps are usually just those that have a good name and don't require super specific input parameters (so they can work in isolation).

I do think we all mean the same thing here, by the way.
If you have a good reason to disagree with the above, please tell me. Thank you.
Poirier255
Profile Joined July 2013
Canada2 Posts
October 28 2014 20:23 GMT
#10756
On October 28 2014 09:16 Days wrote:
+ Show Spoiler +
My fellow nerds! I need your help. I have a C# application due soon and I am stumped. Basically I have to make a simplified version of Paint. What i'm doing for now is drawing Ellipses, Rectangles, and Triangles based on mouse movement. It works pretty nicely, except that when one shape is drawn, the one before that disappears. I know i'm suppose to be using some type of ArrayList to store shapes, but since I have 3 different shapes I am confused as to how to go about that. Here is my code where I add rectangles to the array. (The rectangle is working, but the ellipse and triangles are not being stored)

P.S. Just to not confuse you guys, any code inside the Using(graphics) in my MouseDown event is just to check if a right click falls inside of a created shape. This is in order to open a Properties Dialog that is suppose to be able to change the rotation, location, text, size,width of each drawn shape. I have yet to pass the values, that will be my next monster to tackle.


private void mainFrm_MouseDown(object sender, MouseEventArgs e)
{

initialY = e.Y;
initialX = e.X;
isMoving = true;
using (Graphics g = this.CreateGraphics())
{
if (e.Button == MouseButtons.Right)
{
Point[] ptClick = new Point[] { new Point(e.X, e.Y) };
g.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, ptClick);
foreach (Rectangle rect in rectangles)
{
if (rect.Contains(ptClick[0])
{
options.Show();
}
}
}
}
//this.Invalidate();
}

private void mainFrm_MouseUp(object sender, MouseEventArgs e)
{
isMoving = false;
count++;
//rectangle = new Rectangle(movedX, movedY, width, height);
if (rectangleToolStripMenuItem.Checked == true)
{
rectangles.Add(new Rectangle(movedX, movedY, width, height));
//rectangle = new Rectangle(movedX, movedY, width, height);
}
else if (ellipseToolStripMenuItem.Checked == true)
{
ellipses.Add(new Rectangle(movedX, movedY, width, height));

}
this.Invalidate();
}

private void mainFrm_MouseMove(object sender, MouseEventArgs e)
{
if (isMoving == true)
{
//movedX = e.X - initialX;
//movedY = e.Y - initialY;
triangleX = e.X;
triangleY = e.Y;
movedY = Math.Min(initialY, e.Y);
movedX = Math.Min(initialX, e.X);
width = Math.Max(initialX, e.X) - Math.Min(initialX, e.X);
height = Math.Max(initialY, e.Y) - Math.Min(initialY, e.Y);
rectangles.Insert(0, new Rectangle(movedX, movedY, width, height));

}
this.Invalidate();
}


I haven't looked at your code but the overall problem you're having could be solved by making a generic 'Shape' class for all your shapes to derive from and then having your container<Shape>. I don't know very much C#, but I take it this shouldn't be too bad?

I hope this helps ^^
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2014-10-28 22:35:35
October 28 2014 22:22 GMT
#10757
On October 28 2014 18:27 delHospital wrote:
Show nested quote +
On October 28 2014 17:46 Manit0u wrote:
On October 28 2014 09:17 delHospital wrote:
On October 28 2014 02:52 Blisse wrote:
What is your stance towards using function names/creating new functions to document the function (when you're creating a new method not necessarily used in other places but makes the intent of the code clearer)

+ Show Spoiler +


def main():
file = getInputFromFile()
n = getCharFromFile(file)

for i to n:
c = getCharFromFile(file)

for j to c:
x[i] = getCharFromFile(file)

doStuff(x)


versus


def doCase(c):
for i to c:
x[i] = getCharFromFile(file)
doStuff(x)

def readCases():
n = getCharFromFile(file)
for i to n:
c = getCharFromFile(file)
doCase(c)

def main():
file = getInputFromFile()
readCases()



Obviously more code but basically that idea. If a group of methods/functions with somewhat complex control flow could be simplified down to a single action, would you prefer wrapping that in a function or prefacing the control flow with a comment.
My advice would be this:

When you split up a function, do it bottom up, not top down. That is, don't cut the function in half, and
def big_f():
f1()
f2()
That doesn't really achieve anything. Instead, try to find small small chunks of functionality that can be meaningful on their own, and extract them from the big function. Those will usually take arguments, process them, and return the result, possibly doing a little bit of IO along the way. If you went with the "cut in half" approach, you would have arrived at two weird looking procedures that take nothing, and return nothing.

To elaborate a bit more:

If you refactor
# does this and that
def main():
...
into
# does this
def this():
...

# (possibly hundreds of lines below, or in a different file)
# does that
def that():
...

def main():
this()
that()

Then it becomes easier to see what main() is about, but it comes with a price: that() becomes harder to reason about, as it depends on the state set up by this().

If you instead refactor it like this:
def f(a, b):
# (play with a and b to get a result)
return result

def g(a, b):
...
return result

def main():
# (code that uses f() and g())

Then, as long as f() and g() are sensible, everything becomes smaller and easier to grasp.


I think that's bad. It's way more convenient to have your main function definition at the top and all the helper function definitions below that. The reason is, while inspecting the file you instantly get to the gist of it and if functions have good names you can instantly tell what it does and only have to look up the helper functions if you really need the specifics. Reading random chunks of code doesn't help you understand what the program is actually doing and what its end goal is, you only get that from reading the main function definition. That's why I think it should be the topmost thing.

TL;DR - abstract --> specific is the way to go, not the other way around.

I'm not talking about how functions should be grouped into modules or about the ordering of function definitions in a file. Yes, putting the helper functions below is probably a good idea, but that's not what my post was about. I was saying that you should primarily extract pure-ish, reusable-ish functions that can be reasoned about in isolation, and that simply dividing a procedure into several smaller ones gains you much less.


The rules I go by (mostly C):
1) Functions ought to be ordered in the same order that their prototypes exist in the header file
+ Show Spoiler +
The alternative is insanity when trying to navigate between the two

2) global (non-static/exported/public) functions go on the top half, then, a comment line seperates that from static/helper functions that go on the bottom half of a file.
+ Show Spoiler +
It should be immediately apparent what the scope of the function you're using is

3) Each file represents precisely one layer of abstraction. No file is too large; no file is too small.
+ Show Spoiler +
When working within a file there shouldn't be a requirement to switch to a different file too often. The exception is when you need to know how that module is meant to be used, or you can use a different module.
If your physical layer definitions reach into thousands of lines of code, then you'll find it's easier to have it all in once place rather than having to search two files. If your intermediary glue code only has one or two functions, then the brevity should make it clear where to look for the actual meat, rather than being confused by having that glue code with the code it's glueing.

4) Length of a function doesn't matter so long as it does exactly what the function definition says it does.
+ Show Spoiler +
If there is no right place to split up a function into helper functions, it shouldn't be split.
Any sufficiently advanced technology is indistinguishable from magic
Manit0u
Profile Blog Joined August 2004
Poland17743 Posts
October 28 2014 22:46 GMT
#10758
On October 28 2014 06:33 Nesserev wrote:
Show nested quote +
On October 28 2014 06:11 Manit0u wrote:
+ Show Spoiler +
Has anyone here ever used the pimcore CMS? I've started working for a new company at the end of last week, got assigned some issues pertaining their project using pimcore, which was set up and solely governed by their former employee. No one knows exactly how it works and I've spent past 2 workdays figuring it all out.

Today I've closed 2 issues, which weren't real issues (client got confused) but found some other things that were disturbing:
1. The code that's on the production server doesn't figure on any of the git branches, it's way ahead of master and all the others and there's no way to push it from the production server...
2. While investigating the code and doing some quick fixes for some real issues (had to do it via vim and ssh console) I've discovered that debug.log file has grown to over 200MB over the course of past 2 weeks, most of which were SQL-based errors (majority of them seemed to be state 2002 - unable to open connection to socket), obviously the file was too large for me to parse thoroughly. Investigating php.log revealed some fatal errors involving PDO exceptions... The debug.log is now growing approximately 1MB/hour since people are beginning to fill in content and test it and I have no idea how to handle it in this unfamiliar environment.

Any ideas where to start fixing this? I'm currently looking for a way to disable debug log entirely as the temporary measure, until I figure out what the next course of action should be.

Sounds like you're really f*cked... good luck with that.


Took me most of the day, but now there are 3 branches on the git (master, development and production), what is on the deployment server now matches perfectly to what's in the git production branch so you can at least work on it externally and pull to the server. I've also managed to get rid of many unnecessary files and got .htaccess to work (previously it wasn't).

I feel like a young god now.
Time is precious. Waste it wisely.
nunez
Profile Blog Joined February 2011
Norway4003 Posts
October 28 2014 23:34 GMT
#10759
my current metastructure(?) is:

abstraction <-> folder <-> namespace
element <-> file <-> (function or class)

most of the abstractions i work with fit on a workspace or two (1~8 vims).
as long as i can switch to a workspace on one screen
to lookup or twiddle with the relevant code, and as long as i'm
not working with too many simultaneously (1~5 abstractions)
it works.
conspired against by a confederacy of dunces.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2014-10-29 01:11:36
October 29 2014 01:11 GMT
#10760
Do you think the future of coding will be 5K to 8K monitors? :3
There is no one like you in the universe.
Prev 1 536 537 538 539 540 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 4h 6m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nina 141
StarCraft: Brood War
Sea 4907
JulyZerg 117
910 62
Shinee 31
GoRush 21
Bale 12
ZergMaN 10
scan(afreeca) 10
Icarus 9
League of Legends
JimRising 740
Counter-Strike
Coldzera 1756
m0e_tv622
Super Smash Bros
hungrybox2408
Mew2King67
Other Games
summit1g7406
WinterStarcraft491
C9.Mang0421
monkeys_forever197
NeuroSwarm64
RuFF_SC241
ViBE27
Organizations
Other Games
gamesdonequick829
Dota 2
PGL Dota 2 - Main Stream53
StarCraft: Brood War
lovetv 12
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 14 non-featured ]
StarCraft 2
• practicex 41
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Rush1412
• Lourlo1229
• Stunt390
Upcoming Events
Sparkling Tuna Cup
4h 6m
Afreeca Starleague
4h 6m
Snow vs Flash
WardiTV Invitational
5h 6m
SHIN vs Nicoract
Solar vs Nice
PiGosaur Cup
18h 6m
GSL
1d 3h
Classic vs Cure
Maru vs Rogue
GSL
2 days
SHIN vs Zoun
ByuN vs herO
OSC
2 days
OSC
2 days
Replay Cast
2 days
Escore
3 days
[ Show More ]
The PondCast
3 days
WardiTV Invitational
3 days
Zoun vs Ryung
Lambo vs ShoWTimE
OSC
3 days
Replay Cast
3 days
CranKy Ducklings
4 days
RSL Revival
4 days
SHIN vs Bunny
ByuN vs Shameless
WardiTV Invitational
4 days
Krystianer vs TriGGeR
Cure vs Rogue
uThermal 2v2 Circuit
4 days
BSL
4 days
Replay Cast
4 days
Sparkling Tuna Cup
5 days
RSL Revival
5 days
Cure vs Zoun
Clem vs Lambo
WardiTV Invitational
5 days
BSL
5 days
GSL
6 days
Afreeca Starleague
6 days
Liquipedia Results

Completed

Proleague 2026-05-02
WardiTV TLMC #16
Nations Cup 2026

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
KCM Race Survival 2026 Season 2
Acropolis #4
SCTL 2026 Spring
RSL Revival: Season 5
2026 GSL S1
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026

Upcoming

YSL S3
Escore Tournament S2: W6
KK 2v2 League Season 1
BSL 22 Non-Korean Championship
Escore Tournament S2: W7
Escore Tournament S2: W8
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
2026 GSL S2
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.